Adware.cs 8.8 KB

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