DownloadVideoFromServer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System;
  5. using System.IO;
  6. using SimpleJSON;
  7. using UnityEngine.UI;
  8. /*[Serializable]
  9. public class VideoData
  10. {
  11. private string URLData;
  12. private float TimeSec;
  13. }*/
  14. public class DownloadVideoFromServer : MonoBehaviour
  15. {
  16. #region Unity Singleton
  17. public enum States
  18. {
  19. Idle,
  20. LoadConfig,
  21. ConfigLoadDone,
  22. ConfigLoadFailed,
  23. Loading,
  24. LoadFailed,
  25. LoadDone,
  26. Empty,
  27. HasVideo,
  28. }
  29. public List<ConfigUnit> _configs = new List<ConfigUnit>();
  30. public class ConfigUnit
  31. {
  32. public static bool Loading { get; private set; }
  33. public string VideoUrl { get; set; }
  34. public Texture PlaceholderImage { get; private set; }
  35. public Texture PlaceholderImageNew { get; private set; }
  36. public bool IsLoaded { get; private set; }
  37. public bool Broken { get; private set; }
  38. public ConfigUnit(JSONNode source)
  39. {
  40. VideoUrl = source["VideoURL"];
  41. IsLoaded = false;
  42. }
  43. public IEnumerator Download(DownloadVideoFromServer parent)
  44. {
  45. parent.State = States.Loading;
  46. Loading = true;
  47. var www = new WWW(VideoUrl);
  48. yield return www;
  49. if (string.IsNullOrEmpty(www.error))
  50. {
  51. IsLoaded = true;
  52. www.Dispose();
  53. parent.State = States.LoadDone;
  54. }
  55. else
  56. {
  57. Debug.LogError("Video:[" + www.error + "|" + www.url + "]");
  58. parent.State = States.LoadFailed;
  59. Broken = true;
  60. }
  61. if (!string.IsNullOrEmpty(VideoUrl))
  62. {
  63. www = new WWW(VideoUrl);
  64. yield return www;
  65. if (string.IsNullOrEmpty(www.error))
  66. {
  67. parent.State = States.HasVideo;
  68. }
  69. }
  70. Loading = false;
  71. }
  72. private string ExtractFileName(string url)
  73. {
  74. var arr = url.ToLower().Replace("\\", "/").Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  75. return arr[arr.Length - 1];
  76. }
  77. public IEnumerator DownloadToList(DownloadVideoFromServer parent)
  78. {
  79. parent.State = States.Loading;
  80. Loading = true;
  81. var www = new WWW(VideoUrl);
  82. yield return www;
  83. if (string.IsNullOrEmpty(www.error))
  84. {
  85. Debug.Log("!!!");
  86. IsLoaded = true;
  87. www.Dispose();
  88. parent.State = States.LoadDone;
  89. }
  90. else
  91. {
  92. Debug.LogError("Banner:[" + www.error + "|" + www.url + "]");
  93. parent.State = States.LoadFailed;
  94. Broken = true;
  95. }
  96. if (!string.IsNullOrEmpty(VideoUrl))
  97. {
  98. www = new WWW(VideoUrl);
  99. yield return www;
  100. if (string.IsNullOrEmpty(www.error))
  101. {
  102. DownloadVideoFromServer.Instance.listVideoBanner.Add(www.url);
  103. parent.State = States.HasVideo;
  104. }
  105. }
  106. Loading = false;
  107. }
  108. }
  109. public bool Empty
  110. {
  111. get { return _configs.Count == 0; }
  112. }
  113. private readonly List<string> listVideoBanner = new List<string>();
  114. private void RequesNextAd()
  115. {
  116. if (Empty)
  117. {
  118. State = States.Empty;
  119. return;
  120. }
  121. if (!ConfigUnit.Loading)
  122. {
  123. currentAdIndex++;
  124. if (currentAdIndex >= _configs.Count)
  125. {
  126. currentAdIndex = 0;
  127. }
  128. //Debug.Log(_configs[Adware.Instance.currentAdBannerIndex].PlaceholderUrl);
  129. StartCoroutine(_configs[currentAdIndex].Download(this));
  130. }
  131. }
  132. /// <summary>
  133. /// Path to server config
  134. /// </summary>
  135. public string ConfigURL;
  136. public static DownloadVideoFromServer Instance { get; private set; }
  137. public void DownloadNextImage()
  138. {
  139. StartCoroutine(LoadConfig());
  140. }
  141. public States State;
  142. public IEnumerator LoadConfig()
  143. {
  144. State = States.LoadConfig;
  145. var www = new WWW(ConfigURL);
  146. yield return www;
  147. if (string.IsNullOrEmpty(www.error))
  148. {
  149. var source = www.text;
  150. Debug.Log(source);
  151. var json = SimpleJSON.JSON.Parse(www.text)["Config"].AsArray;
  152. foreach (JSONNode media in json)
  153. {
  154. _configs.Add(new ConfigUnit(media));
  155. }
  156. State = States.ConfigLoadDone;
  157. AddAddToList();
  158. VideoManager.Instance.SetURLVideo(_configs[0].VideoUrl);
  159. RequesNextAd();
  160. }
  161. else
  162. {
  163. Debug.LogError("Bunner:[" + www.error + "]");
  164. State = States.ConfigLoadFailed;
  165. }
  166. }
  167. private void AddAddToList()
  168. {
  169. for (int i = 0; i < _configs.Count; i++)
  170. {
  171. StartCoroutine(_configs[i].DownloadToList(this));
  172. }
  173. }
  174. private ConfigUnit Currnent
  175. {
  176. get { return _configs[currentAdIndex]; }
  177. }
  178. void Start()
  179. {
  180. ConfigURL = GameConstants.VIDEO_ADS;
  181. if (Instance != null && Instance != this)
  182. {
  183. Destroy(gameObject);
  184. return;
  185. }
  186. if (Instance)
  187. {
  188. Destroy(gameObject);
  189. }
  190. else
  191. {
  192. _configs = new List<ConfigUnit>();
  193. Instance = this;
  194. StartCoroutine(LoadConfig());
  195. }
  196. }
  197. #endregion
  198. private Hashtable _loadedImages = new Hashtable();
  199. public static void ClearImagesInMemory()
  200. {
  201. Instance._loadedImages = new Hashtable();
  202. }
  203. private IEnumerator LoadImageFromCache(string textureName, Action<Texture2D> onComplete)
  204. {
  205. string directoryPath = Application.persistentDataPath + "/imageCache";
  206. WWW w = new WWW("file://" + Path.GetFullPath(string.Format(@"{0}/{1}.png", directoryPath, textureName)));
  207. yield return w;
  208. onComplete(w.texture);
  209. }
  210. private int count;
  211. private int currentAdIndex;
  212. public void GetVideo()
  213. {
  214. Debug.Log(_configs.Count);
  215. if (_configs.Count > 0)
  216. {
  217. currentAdIndex = UnityEngine.Random.Range(0, _configs.Count - 1);
  218. Debug.Log(_configs[currentAdIndex].VideoUrl);
  219. //VideoManager.Instance.VideoPlayer.url = _configs[currentAdIndex].VideoUrl;
  220. }
  221. }
  222. public Texture texture;
  223. }