123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- using System.Collections.Generic;
- using UnityEngine;
- using System.Collections;
- using System;
- using System.IO;
- using SimpleJSON;
- using UnityEngine.UI;
- /*[Serializable]
- public class VideoData
- {
- private string URLData;
- private float TimeSec;
- }*/
- public class DownloadVideoFromServer : MonoBehaviour
- {
- #region Unity Singleton
-
- public enum States
- {
- Idle,
- LoadConfig,
- ConfigLoadDone,
- ConfigLoadFailed,
- Loading,
- LoadFailed,
- LoadDone,
- Empty,
- HasVideo,
- }
- public List<ConfigUnit> _configs = new List<ConfigUnit>();
- public class ConfigUnit
- {
- public static bool Loading { get; private set; }
- public string VideoUrl { get; set; }
- public Texture PlaceholderImage { get; private set; }
- public Texture PlaceholderImageNew { get; private set; }
- public bool IsLoaded { get; private set; }
- public bool Broken { get; private set; }
- public ConfigUnit(JSONNode source)
- {
- VideoUrl = source["VideoURL"];
- IsLoaded = false;
- }
- public IEnumerator Download(DownloadVideoFromServer parent)
- {
- parent.State = States.Loading;
- Loading = true;
- var www = new WWW(VideoUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- IsLoaded = true;
- www.Dispose();
- parent.State = States.LoadDone;
- }
- else
- {
- Debug.LogError("Video:[" + www.error + "|" + www.url + "]");
- parent.State = States.LoadFailed;
- Broken = true;
- }
- if (!string.IsNullOrEmpty(VideoUrl))
- {
- www = new WWW(VideoUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- 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 IEnumerator DownloadToList(DownloadVideoFromServer parent)
- {
- parent.State = States.Loading;
- Loading = true;
- var www = new WWW(VideoUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- Debug.Log("!!!");
- IsLoaded = true;
- www.Dispose();
- parent.State = States.LoadDone;
- }
- else
- {
- Debug.LogError("Banner:[" + www.error + "|" + www.url + "]");
- parent.State = States.LoadFailed;
- Broken = true;
- }
- if (!string.IsNullOrEmpty(VideoUrl))
- {
- www = new WWW(VideoUrl);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- DownloadVideoFromServer.Instance.listVideoBanner.Add(www.url);
- parent.State = States.HasVideo;
- }
- }
- Loading = false;
- }
- }
- public bool Empty
- {
- get { return _configs.Count == 0; }
- }
- private readonly List<string> listVideoBanner = new List<string>();
- private void RequesNextAd()
- {
- if (Empty)
- {
- State = States.Empty;
- return;
- }
- if (!ConfigUnit.Loading)
- {
- currentAdIndex++;
- if (currentAdIndex >= _configs.Count)
- {
- currentAdIndex = 0;
- }
- //Debug.Log(_configs[Adware.Instance.currentAdBannerIndex].PlaceholderUrl);
- StartCoroutine(_configs[currentAdIndex].Download(this));
- }
- }
- /// <summary>
- /// Path to server config
- /// </summary>
- public string ConfigURL;
- public static DownloadVideoFromServer Instance { get; private set; }
- public void DownloadNextImage()
- {
- StartCoroutine(LoadConfig());
- }
- public States State;
- public IEnumerator LoadConfig()
- {
- State = States.LoadConfig;
- var www = new WWW(ConfigURL);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- var source = www.text;
- Debug.Log(source);
- var json = SimpleJSON.JSON.Parse(www.text)["Config"].AsArray;
- foreach (JSONNode media in json)
- {
- _configs.Add(new ConfigUnit(media));
- }
- State = States.ConfigLoadDone;
- AddAddToList();
- VideoManager.Instance.SetURLVideo(_configs[0].VideoUrl);
- RequesNextAd();
- }
- else
- {
- Debug.LogError("Bunner:[" + www.error + "]");
- State = States.ConfigLoadFailed;
- }
- }
- private void AddAddToList()
- {
- for (int i = 0; i < _configs.Count; i++)
- {
- StartCoroutine(_configs[i].DownloadToList(this));
- }
- }
- private ConfigUnit Currnent
- {
- get { return _configs[currentAdIndex]; }
- }
- void Start()
- {
- ConfigURL = GameConstants.VIDEO_ADS;
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- if (Instance)
- {
- Destroy(gameObject);
- }
- else
- {
- _configs = new List<ConfigUnit>();
- Instance = this;
- StartCoroutine(LoadConfig());
- }
- }
- #endregion
- private Hashtable _loadedImages = new Hashtable();
- public static void ClearImagesInMemory()
- {
- Instance._loadedImages = new Hashtable();
- }
- private IEnumerator LoadImageFromCache(string textureName, Action<Texture2D> onComplete)
- {
- string directoryPath = Application.persistentDataPath + "/imageCache";
- WWW w = new WWW("file://" + Path.GetFullPath(string.Format(@"{0}/{1}.png", directoryPath, textureName)));
- yield return w;
- onComplete(w.texture);
- }
- private int count;
- private int currentAdIndex;
- public void GetVideo()
- {
- Debug.Log(_configs.Count);
- if (_configs.Count > 0)
- {
- currentAdIndex = UnityEngine.Random.Range(0, _configs.Count - 1);
- Debug.Log(_configs[currentAdIndex].VideoUrl);
- //VideoManager.Instance.VideoPlayer.url = _configs[currentAdIndex].VideoUrl;
-
- }
- }
- public Texture texture;
- }
|