Adware.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. using SimpleJSON;
  8. public class Adware : MonoBehaviour
  9. {
  10. public enum States
  11. {
  12. Idle,
  13. LoadConfig,
  14. ConfigLoadDone,
  15. ConfigLoadFailed,
  16. Loading,
  17. LoadFailed,
  18. LoadDone,
  19. HasVideo,
  20. Empty,
  21. }
  22. public States State;
  23. public Transform _root;
  24. public bool Empty
  25. {
  26. get { return _configs.Count == 0; }
  27. }
  28. public bool Available
  29. {
  30. get
  31. {
  32. if (Empty)
  33. {
  34. return false;
  35. }
  36. if (_configs[_currentAdIndex].Broken)
  37. {
  38. RequesNextAd();
  39. return false;
  40. }
  41. return _configs[_currentAdIndex].IsLoaded;
  42. }
  43. }
  44. public delegate void OnAdCloseEventHandler();
  45. public static event OnAdCloseEventHandler OnClose;
  46. public int currentAdBannerIndex = -1;
  47. public class ConfigUnit
  48. {
  49. public static bool Loading { get; private set; }
  50. public string VideoUrl { get; set; }
  51. private string PlaceholderUrl { get; set; }
  52. public Texture2D PlaceholderImage { get; private set; }
  53. public bool IsLoaded { get; private set; }
  54. public string Movie { get; private set; }
  55. public bool Broken { get; private set; }
  56. public ConfigUnit(JSONNode source)
  57. {
  58. VideoUrl = source["VideoURL"];
  59. PlaceholderUrl = source["PlaceholderURL"];
  60. IsLoaded = false;
  61. }
  62. public IEnumerator Download(Adware parent)
  63. {
  64. parent.State = States.Loading;
  65. Loading = true;
  66. var www = new WWW(VideoUrl);
  67. yield return www;
  68. if (string.IsNullOrEmpty(www.error))
  69. {
  70. IsLoaded = true;
  71. Movie = Application.persistentDataPath + "/Data/" + ExtractFileName(VideoUrl);
  72. File.WriteAllBytes(Movie, www.bytes);
  73. www.Dispose();
  74. parent.State = States.LoadDone;
  75. }
  76. else
  77. {
  78. Debug.LogError("ERROR LOAD");
  79. Debug.LogError("adware:[" + www.error + "|" + www.url + "]");
  80. parent.State = States.LoadFailed;
  81. Broken = true;
  82. }
  83. if (!string.IsNullOrEmpty(PlaceholderUrl))
  84. {
  85. www = new WWW(PlaceholderUrl);
  86. yield return www;
  87. if (string.IsNullOrEmpty(www.error))
  88. {
  89. PlaceholderImage = www.texture;
  90. parent.State = States.HasVideo;
  91. }
  92. }
  93. Loading = false;
  94. }
  95. private string ExtractFileName(string url)
  96. {
  97. var arr = url.ToLower().Replace("\\", "/").Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  98. return arr[arr.Length - 1];
  99. }
  100. public void Show(MediaPlayerCtrl adInstance)
  101. {
  102. adInstance.m_bFullScreen = true;
  103. Debug.Log("file://" + Movie);
  104. adInstance.Load("file://" + Movie);
  105. adInstance.Play();
  106. }
  107. }
  108. private List<ConfigUnit> _configs = new List<ConfigUnit>();
  109. public static Adware Instance { get; private set; }
  110. /// <summary>
  111. /// Path to server config
  112. /// </summary>
  113. public string ConfigURL;
  114. /// <summary>
  115. /// Video manager prefab
  116. /// </summary>
  117. public GameObject AdPrefab;
  118. private MediaPlayerCtrl _adInstance;
  119. private int _currentAdIndex;
  120. private ConfigUnit Currnent
  121. {
  122. get { return _configs[_currentAdIndex]; }
  123. }
  124. private string _currebtVisitUrl;
  125. void Update()
  126. {
  127. if (_root == null)
  128. {
  129. _root = GameObject.Find("UI Root (2D)/Camera/Anchor_center").transform;
  130. }
  131. }
  132. internal void Start()
  133. {
  134. _root = GameObject.Find("UI Root (2D)/Camera/Anchor_center").transform;
  135. State = States.Idle;
  136. ConfigURL = GameConstants.VEDO_ADS;
  137. if (Instance)
  138. {
  139. Destroy(gameObject);
  140. }
  141. else
  142. {
  143. if (!Directory.Exists(Application.persistentDataPath + "/Data"))
  144. {
  145. Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  146. }
  147. _configs = new List<ConfigUnit>();
  148. _currentAdIndex = -1;
  149. Instance = this;
  150. StartCoroutine(LoadConfig());
  151. DontDestroyOnLoad(gameObject);
  152. }
  153. //StartCoroutine(Test());
  154. }
  155. private IEnumerator Test()
  156. {
  157. do
  158. {
  159. yield return new WaitForSeconds(1);
  160. } while (!Available);
  161. Show(() => { });
  162. }
  163. private IEnumerator LoadConfig()
  164. {
  165. State = States.LoadConfig;
  166. var www = new WWW(ConfigURL);
  167. yield return www;
  168. if (string.IsNullOrEmpty(www.error))
  169. {
  170. var source = www.text;
  171. var json = SimpleJSON.JSON.Parse(www.text)["Config"].AsArray;
  172. foreach (JSONNode media in json)
  173. {
  174. _configs.Add(new ConfigUnit(media));
  175. }
  176. State = States.ConfigLoadDone;
  177. RequesNextAd();
  178. }
  179. else
  180. {
  181. Debug.LogError("adware:[" + www.error + "]");
  182. State = States.ConfigLoadFailed;
  183. }
  184. }
  185. private void RequesNextAd()
  186. {
  187. if (Empty)
  188. {
  189. State = States.Empty;
  190. return;
  191. }
  192. if (!ConfigUnit.Loading)
  193. {
  194. _currentAdIndex++;
  195. if (_currentAdIndex >= _configs.Count)
  196. {
  197. _currentAdIndex = 0;
  198. }
  199. StartCoroutine(_configs[_currentAdIndex].Download(this));
  200. }
  201. }
  202. public GameObject CurAdPanel;
  203. public void Show(Action onFinished)
  204. {
  205. if (Empty)
  206. {
  207. Debug.Log("Empty");
  208. return;
  209. }
  210. if (Currnent.Broken)
  211. {
  212. Debug.Log("Broken");
  213. RequesNextAd();
  214. return;
  215. }
  216. if (!Currnent.IsLoaded)
  217. {
  218. return;
  219. }
  220. ButtonsHandler handler = null;
  221. if (_adInstance == null)
  222. {
  223. CurAdPanel = (GameObject)Instantiate(AdPrefab.gameObject);
  224. Debug.Log("CurAdPanel");
  225. _adInstance = CurAdPanel.GetComponentInChildren<MediaPlayerCtrl>();
  226. handler = _adInstance.GetComponent<ButtonsHandler>();
  227. handler.RootObject.transform.parent = _root;
  228. handler.RootObject.transform.localScale = new Vector3(1.8f, 1.8f, 1);
  229. if (_root.root.GetChild(0) != null)
  230. {
  231. var cam = _root.root.GetChild(0).GetComponent<Camera>();
  232. var stretch = handler.RootObject.GetComponentsInChildren<UIStretch>();
  233. var anchor = handler.RootObject.GetComponentsInChildren<UIAnchor>();
  234. foreach (var uiStretch in stretch)
  235. {
  236. uiStretch.uiCamera = cam;
  237. }
  238. foreach (var uiAnchor in anchor)
  239. {
  240. uiAnchor.uiCamera = cam;
  241. }
  242. }
  243. }
  244. else
  245. {
  246. handler = _adInstance.gameObject.GetComponent<ButtonsHandler>();
  247. }
  248. handler.CloseAdware.gameObject.SetActive(false);
  249. if (handler.Header)
  250. {
  251. handler.Header.SetActive(false);
  252. }
  253. handler.Placeholder.gameObject.SetActive(false);
  254. SoundManagerCS.Instance.PlayVideoAd();
  255. handler.Placeholder.mainTexture = Currnent.PlaceholderImage;
  256. Debug.Log("Currnent.Show");
  257. Currnent.Show(_adInstance);
  258. _adInstance.OnEnd += () =>
  259. {
  260. handler.CloseAdware.gameObject.SetActive(true);
  261. if (handler.Header)
  262. {
  263. handler.Header.SetActive(true);
  264. }
  265. handler.Placeholder.gameObject.SetActive(true);
  266. onFinished();
  267. RequesNextAd();
  268. SoundManagerCS.Instance.EndVideoAd();
  269. };
  270. #if UNITY_EDITOR
  271. handler.CloseAdware.gameObject.SetActive(true);
  272. if (handler.Header)
  273. {
  274. handler.Header.SetActive(true);
  275. }
  276. handler.Placeholder.gameObject.SetActive(true);
  277. onFinished();
  278. RequesNextAd();
  279. SoundManagerCS.Instance.EndVideoAd();
  280. #endif
  281. }
  282. internal void OnApplicationQuit()
  283. {
  284. if (Directory.Exists(Application.persistentDataPath + "/Data"))
  285. Directory.Delete(Application.persistentDataPath + "/Data", true);
  286. }
  287. public void Close()
  288. {
  289. if (OnClose != null)
  290. {
  291. OnClose();
  292. }
  293. SoundManagerCS.Instance.EndVideoAd();
  294. Destroy(CurAdPanel);
  295. }
  296. }