123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Security.Cryptography;
- using SimpleJSON;
- public class Adware : MonoBehaviour
- {
- public enum States
- {
- Idle,
- LoadConfig,
- ConfigLoadDone,
- ConfigLoadFailed,
- Loading,
- LoadFailed,
- LoadDone,
- HasVideo,
- Empty,
- }
- public States State;
- public Transform _root;
- public bool Empty
- {
- get { return _configs.Count == 0; }
- }
- public bool Available
- {
- get
- {
- if (Empty)
- {
- return false;
- }
- if (_configs[_currentAdIndex].Broken)
- {
- RequesNextAd();
- return false;
- }
- return _configs[_currentAdIndex].IsLoaded;
- }
- }
- public delegate void OnAdCloseEventHandler();
- public static event OnAdCloseEventHandler OnClose;
- public int currentAdBannerIndex = -1;
- public class ConfigUnit
- {
- public static bool Loading { get; private set; }
- public string VideoUrl { get; set; }
- private string PlaceholderUrl { get; set; }
- public Texture2D PlaceholderImage { get; private set; }
- public bool IsLoaded { get; private set; }
- public string Movie { get; private set; }
- public bool Broken { get; private set; }
- public ConfigUnit(JSONNode source)
- {
- VideoUrl = source["VideoURL"];
- PlaceholderUrl = source["PlaceholderURL"];
- IsLoaded = false;
- }
- public IEnumerator Download(Adware parent)
- {
- parent.State = States.Loading;
- Loading = true;
- var www = new WWW(VideoUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- IsLoaded = true;
- Movie = Application.persistentDataPath + "/Data/" + ExtractFileName(VideoUrl);
- File.WriteAllBytes(Movie, www.bytes);
- www.Dispose();
- parent.State = States.LoadDone;
- }
- else
- {
- Debug.LogError("ERROR LOAD");
- Debug.LogError("adware:[" + www.error + "|" + www.url + "]");
- parent.State = States.LoadFailed;
- Broken = true;
- }
- if (!string.IsNullOrEmpty(PlaceholderUrl))
- {
- www = new WWW(PlaceholderUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- PlaceholderImage = www.texture;
- parent.State = States.HasVideo;
- }
- }
- Loading = false;
- }
- private string ExtractFileName(string url)
- {
- var arr = url.ToLower().Replace("\\", "/").Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
- return arr[arr.Length - 1];
- }
- public void Show(MediaPlayerCtrl adInstance)
- {
- adInstance.m_bFullScreen = true;
- Debug.Log("file://" + Movie);
- adInstance.Load("file://" + Movie);
- adInstance.Play();
- }
- }
- private List<ConfigUnit> _configs = new List<ConfigUnit>();
- public static Adware Instance { get; private set; }
- /// <summary>
- /// Path to server config
- /// </summary>
- public string ConfigURL;
- /// <summary>
- /// Video manager prefab
- /// </summary>
- public GameObject AdPrefab;
- private MediaPlayerCtrl _adInstance;
- private int _currentAdIndex;
- private ConfigUnit Currnent
- {
- get { return _configs[_currentAdIndex]; }
- }
- private string _currebtVisitUrl;
- void Update()
- {
- if (_root == null)
- {
- _root = GameObject.Find("UI Root (2D)/Camera/Anchor_center").transform;
- }
- }
- internal void Start()
- {
- _root = GameObject.Find("UI Root (2D)/Camera/Anchor_center").transform;
- State = States.Idle;
- ConfigURL = GameConstants.VEDO_ADS;
- if (Instance)
- {
- Destroy(gameObject);
- }
- else
- {
- if (!Directory.Exists(Application.persistentDataPath + "/Data"))
- {
- Directory.CreateDirectory(Application.persistentDataPath + "/Data");
- }
- _configs = new List<ConfigUnit>();
- _currentAdIndex = -1;
- Instance = this;
- StartCoroutine(LoadConfig());
- DontDestroyOnLoad(gameObject);
- }
- //StartCoroutine(Test());
- }
- private IEnumerator Test()
- {
- do
- {
- yield return new WaitForSeconds(1);
- } while (!Available);
- Show(() => { });
- }
- private IEnumerator LoadConfig()
- {
- State = States.LoadConfig;
- var www = new WWW(ConfigURL);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- var source = www.text;
- var json = SimpleJSON.JSON.Parse(www.text)["Config"].AsArray;
- foreach (JSONNode media in json)
- {
- _configs.Add(new ConfigUnit(media));
- }
- State = States.ConfigLoadDone;
- RequesNextAd();
- }
- else
- {
- Debug.LogError("adware:[" + www.error + "]");
- State = States.ConfigLoadFailed;
- }
- }
- private void RequesNextAd()
- {
- if (Empty)
- {
- State = States.Empty;
- return;
- }
- if (!ConfigUnit.Loading)
- {
- _currentAdIndex++;
- if (_currentAdIndex >= _configs.Count)
- {
- _currentAdIndex = 0;
- }
- StartCoroutine(_configs[_currentAdIndex].Download(this));
- }
- }
- public GameObject CurAdPanel;
- public void Show(Action onFinished)
- {
- if (Empty)
- {
- Debug.Log("Empty");
- return;
- }
- if (Currnent.Broken)
- {
- Debug.Log("Broken");
- RequesNextAd();
- return;
- }
- if (!Currnent.IsLoaded)
- {
- return;
- }
- ButtonsHandler handler = null;
- if (_adInstance == null)
- {
- CurAdPanel = (GameObject)Instantiate(AdPrefab.gameObject);
- Debug.Log("CurAdPanel");
- _adInstance = CurAdPanel.GetComponentInChildren<MediaPlayerCtrl>();
- handler = _adInstance.GetComponent<ButtonsHandler>();
- handler.RootObject.transform.parent = _root;
- handler.RootObject.transform.localScale = new Vector3(1.8f, 1.8f, 1);
- if (_root.root.GetChild(0) != null)
- {
- var cam = _root.root.GetChild(0).GetComponent<Camera>();
- var stretch = handler.RootObject.GetComponentsInChildren<UIStretch>();
- var anchor = handler.RootObject.GetComponentsInChildren<UIAnchor>();
- foreach (var uiStretch in stretch)
- {
- uiStretch.uiCamera = cam;
- }
- foreach (var uiAnchor in anchor)
- {
- uiAnchor.uiCamera = cam;
- }
- }
- }
- else
- {
- handler = _adInstance.gameObject.GetComponent<ButtonsHandler>();
- }
- handler.CloseAdware.gameObject.SetActive(false);
- if (handler.Header)
- {
- handler.Header.SetActive(false);
- }
- handler.Placeholder.gameObject.SetActive(false);
- SoundManagerCS.Instance.PlayVideoAd();
- handler.Placeholder.mainTexture = Currnent.PlaceholderImage;
- Debug.Log("Currnent.Show");
- Currnent.Show(_adInstance);
- _adInstance.OnEnd += () =>
- {
- handler.CloseAdware.gameObject.SetActive(true);
- if (handler.Header)
- {
- handler.Header.SetActive(true);
- }
- handler.Placeholder.gameObject.SetActive(true);
- onFinished();
- RequesNextAd();
- SoundManagerCS.Instance.EndVideoAd();
- };
- #if UNITY_EDITOR
- handler.CloseAdware.gameObject.SetActive(true);
- if (handler.Header)
- {
- handler.Header.SetActive(true);
- }
- handler.Placeholder.gameObject.SetActive(true);
- onFinished();
- RequesNextAd();
- SoundManagerCS.Instance.EndVideoAd();
- #endif
- }
- internal void OnApplicationQuit()
- {
- if (Directory.Exists(Application.persistentDataPath + "/Data"))
- Directory.Delete(Application.persistentDataPath + "/Data", true);
- }
- public void Close()
- {
- if (OnClose != null)
- {
- OnClose();
- }
- SoundManagerCS.Instance.EndVideoAd();
- Destroy(CurAdPanel);
- }
- }
|