Exemple #1
0
        private System.Collections.Generic.IEnumerator <byte> StartTask <T>(TaskItem task)       /*where T : Object*/
        {
            var isBytesOutput = (typeof(T) == typeof(byte[]));

            #region Load Resource
            if (task.resource.loadableWeb == true)
            {
                if (task.resource.webPath.Contains("://") == false)
                {
                    task.resource.webPath = string.Format("file://{0}", task.resource.webPath);
                }

                WWW www = null;
                if (task.resource.cacheVersion > 0)
                {
                    www = WWW.LoadFromCacheOrDownload(task.resource.webPath, task.resource.cacheVersion);
                }
                else
                {
                    www = new WWW(task.resource.webPath);
                }

                while (www.isDone == false)
                {
                    yield return(0);
                }

                if (string.IsNullOrEmpty(www.error) == true)
                {
                    var type = typeof(T);
                    if (type == typeof(Texture) ||
                        type == typeof(Texture2D))
                    {
                        task.RaiseSuccess(task.resource.readableTexture == true ? www.texture : www.textureNonReadable);
                    }
                    else
                    {
                        task.RaiseSuccess(www.bytes);
                    }
                }
                else
                {
                    Debug.LogError(string.Format("Task WebRequest [{0}] error: {1}", www.url, www.error));
                    task.RaiseFailed();
                }

                www.Dispose();
                www = null;
            }
            else if (task.resource.loadableResource == true || (string.IsNullOrEmpty(task.customResourcePath) == false && task.resource.loadableAssetBundle == false))
            {
                Object data         = null;
                var    resourcePath = task.customResourcePath ?? task.resource.resourcesPath;
                if (task.async == true)
                {
                    var asyncTask = Resources.LoadAsync(resourcePath, isBytesOutput == true ? typeof(TextAsset) : typeof(T));
                    while (asyncTask.isDone == false)
                    {
                        yield return(0);
                    }

                    data      = asyncTask.asset;
                    asyncTask = null;
                }

                if (task.resource.multiObjects == true && task.resource.objectIndex >= 0)
                {
                    task.RaiseSuccess(Resources.LoadAll(resourcePath)[task.resource.objectIndex]);
                }
                else
                {
                    if (isBytesOutput == true)
                    {
                        if (data == null)
                        {
                            data = Resources.Load <TextAsset>(resourcePath);
                        }
                        task.RaiseSuccess(((data as TextAsset).bytes));
                    }
                    else
                    {
                        if (data == null)
                        {
                            data = Resources.Load(resourcePath, typeof(T));
                        }
                        task.RaiseSuccess(data);
                    }
                }
            }
            else if (task.resource.loadableStream == true)
            {
                if (task.resource.IsMovie() == true)
                {
                    Debug.Log("LoadMovie: " + task.component);
                    task.task = MovieSystem.LoadTexture(task.component as IImageComponent);
                    var startTime = Time.realtimeSinceStartup;
                    var timer     = 0f;
                    while (task.task.isDone == false)
                    {
                        timer = Time.realtimeSinceStartup - startTime;

                        if (timer >= 3f)
                        {
                            break;
                        }

                        yield return(0);
                    }

                    if (task.task != null && task.task.isDone == true)
                    {
                        //Debug.Log("Loaded: " + component.GetResource().GetStreamPath() + ", iter: " + iteration + ", type: " + typeof(T).ToString() + ", asset: " + task.asset, graphic);
                        task.RaiseSuccess(task.task.asset);
                    }
                    else
                    {
                        task.RaiseFailed();
                    }
                }
                else
                {
                    WWW www = null;
                    if (task.resource.cacheVersion > 0)
                    {
                        www = WWW.LoadFromCacheOrDownload(task.resource.GetStreamPath(withFile: true), task.resource.cacheVersion);
                    }
                    else
                    {
                        www = new WWW(task.resource.GetStreamPath(withFile: true));
                    }

                    while (www.isDone == false)
                    {
                        yield return(0);
                    }

                    if (string.IsNullOrEmpty(www.error) == true)
                    {
                        var type = typeof(T);
                        //Debug.Log("LOADED: " + type.ToString() + " :: " + task.resource.GetStreamPath(withFile: true));
                        if (type == typeof(Texture) ||
                            type == typeof(Texture2D))
                        {
                            task.RaiseSuccess(task.resource.readableTexture == true ? www.texture : www.textureNonReadable);
                        }
                        else
                        {
                            var data = www.bytes;
                            if (isBytesOutput == true)
                            {
                                task.RaiseSuccess(data);
                            }
                            else
                            {
                                task.RaiseSuccess(null);
                            }
                        }
                    }
                    else
                    {
                        //Debug.Log("NOT LOADED: " + task.resource.GetStreamPath(withFile: true) + " :: " + www.error);
                        task.RaiseFailed();
                    }

                    www.Dispose();
                    www = null;
                }
            }
            else if (task.resource.loadableAssetBundle == true)
            {
                                #if UNITY_IOS
                if (UnityEngine.iOS.OnDemandResources.enabled == true)
                {
                    /*var request = UnityEngine.iOS.OnDemandResources.PreloadAsync(new string[] { odrTag } );
                     * // Wait until request is completed
                     * yield return request;
                     * // Check for errors
                     * if (request.error != null) {
                     *      task.RaiseFailed();
                     *      return;
                     * }
                     * var bundle = AssetBundle.CreateFromFile("res://" + resourceName);*/
                }
                                #endif

                var path = task.resource.GetAssetBundlePath(true);

                WWW www = null;
                if (task.resource.cacheVersion > 0)
                {
                    www = WWW.LoadFromCacheOrDownload(path, task.resource.cacheVersion);
                }
                else
                {
                    www = new WWW(path);
                }

                while (www.isDone == false)
                {
                    yield return(0);
                }

                if (string.IsNullOrEmpty(www.error) == true && www.assetBundle != null)
                {
                    var assets = www.assetBundle.LoadAllAssets();
                    var asset  = assets[task.resource.objectIndexAssetBundle];
                    task.RaiseSuccess(asset);
                    www.assetBundle.Unload(false);
                }
                else
                {
                    task.RaiseFailed();
                }

                www.Dispose();
                www = null;
            }
            #endregion
        }
Exemple #2
0
        public IEnumerator Load <T>(IImageComponent component, Graphic graphic, string customResourcePath, System.Action <T> callback) where T : Object
        {
            if (ResourceBase.iterations == null)
            {
                ResourceBase.iterations = new Dictionary <Graphic, int>();
            }
            if (ResourceBase.colorCache == null)
            {
                ResourceBase.colorCache = new Dictionary <Graphic, Color>();
            }

            customResourcePath      = customResourcePath ?? (string.IsNullOrEmpty(this.customResourcePath) == true ? null : this.customResourcePath);
            this.customResourcePath = customResourcePath;

            var isFade = (WindowSystemResources.GetAsyncLoadFadeTime() > 0f);

            var iterationFailed = false;
            var iteration       = 0;
            var oldColor        = Color.white;

            if (graphic != null)
            {
                var isEmpty = true;
                if (graphic is Image)
                {
                    isEmpty = ((graphic as Image).sprite == null);
                }

                if (isEmpty == true && graphic is RawImage)
                {
                    isEmpty = ((graphic as RawImage).texture == null);
                }

                if (isFade == true)
                {
                    TweenerGlobal.instance.removeTweens(graphic);
                }

                if (ResourceBase.iterations.TryGetValue(graphic, out iteration) == false)
                {
                    ResourceBase.iterations.Add(graphic, iteration);
                }

                if (ResourceBase.colorCache.TryGetValue(graphic, out oldColor) == true)
                {
                    // restoring color
                    graphic.color = oldColor;
                }
                else
                {
                    ResourceBase.colorCache.Add(graphic, graphic.color);
                }

                oldColor = graphic.color;
                if (isEmpty == true)
                {
                    graphic.color = new Color(oldColor.r, oldColor.g, oldColor.b, 0f);
                }

                ++ResourceBase.iterations[graphic];
                iteration = ResourceBase.iterations[graphic];

                //Debug.Log("Loading: " + customResourcePath + ", iter: " + iteration, graphic);
            }

            #region Load Resource
            if (this.loadableResource == true || string.IsNullOrEmpty(customResourcePath) == false)
            {
                var resourcePath = customResourcePath ?? this.resourcesPath;

                if (this.async == true)
                {
                    var task = Resources.LoadAsync <T>(resourcePath);
                    while (task.isDone == false)
                    {
                        yield return(false);
                    }

                    iterationFailed = !(graphic == null || iteration == ResourceBase.iterations[graphic]);
                    if (iterationFailed == false)
                    {
                        if (this.multiObjects == true && this.objectIndex >= 0)
                        {
                            callback.Invoke(Resources.LoadAll(resourcePath)[this.objectIndex] as T);
                        }
                        else
                        {
                            callback.Invoke(task.asset as T);
                        }
                    }

                    task = null;
                }
                else
                {
                    if (this.multiObjects == true && this.objectIndex >= 0)
                    {
                        callback.Invoke(Resources.LoadAll(resourcePath)[this.objectIndex] as T);
                    }
                    else
                    {
                        var asset = Resources.Load <T>(resourcePath);
                        callback.Invoke(asset);
                    }
                }
            }
            else if (this.loadableStream == true)
            {
                var task = MovieSystem.LoadTexture(component);
                while (task.isDone == false)
                {
                    yield return(false);
                }

                iterationFailed = !(graphic == null || iteration == ResourceBase.iterations[graphic]);
                if (iterationFailed == false)
                {
                    //Debug.Log("Loaded: " + customResourcePath + ", iter: " + iteration, graphic);

                    callback.Invoke(task.asset as T);
                }

                task.Dispose();
                task = null;
            }
            #endregion

            if (iterationFailed == false && graphic != null)
            {
                if (isFade == true)
                {
                    TweenerGlobal.instance.addTweenAlpha(graphic, WindowSystemResources.GetAsyncLoadFadeTime(), oldColor.a).tag(graphic).onCancel((g) => { g.color = oldColor; });
                }
                else
                {
                    graphic.color = oldColor;
                }
            }
        }
        private IEnumerator StartTask <T>(TaskItem task) where T : Object
        {
            #region Load Resource
            if (task.resource.loadableResource == true || string.IsNullOrEmpty(task.customResourcePath) == false)
            {
                var resourcePath = task.customResourcePath ?? task.resource.resourcesPath;
                if (task.resource.async == true)
                {
                    var asyncTask = Resources.LoadAsync <T>(resourcePath);
                    while (asyncTask.isDone == false)
                    {
                        yield return(false);
                    }

                    if (task.resource.multiObjects == true && task.resource.objectIndex >= 0)
                    {
                        task.RaiseSuccess(Resources.LoadAll(resourcePath)[task.resource.objectIndex]);
                    }
                    else
                    {
                        task.RaiseSuccess(asyncTask.asset);
                    }

                    asyncTask = null;
                }
                else
                {
                    if (task.resource.multiObjects == true && task.resource.objectIndex >= 0)
                    {
                        task.RaiseSuccess(Resources.LoadAll(resourcePath)[task.resource.objectIndex]);
                    }
                    else
                    {
                        var asset = Resources.Load <T>(resourcePath);
                        task.RaiseSuccess(asset);
                    }
                }
            }
            else if (task.resource.loadableStream == true)
            {
                task.task = MovieSystem.LoadTexture(task.component);
                var startTime = Time.realtimeSinceStartup;
                var timer     = 0f;
                while (task.task.isDone == false)
                {
                    timer = Time.realtimeSinceStartup - startTime;

                    if (timer >= 3f)
                    {
                        break;
                    }

                    yield return(false);
                }

                if (task.task != null && task.task.isDone == true)
                {
                    //Debug.Log("Loaded: " + component.GetResource().GetStreamPath() + ", iter: " + iteration + ", type: " + typeof(T).ToString() + ", asset: " + task.asset, graphic);
                    task.RaiseSuccess(task.task.asset);
                }
                else
                {
                    task.RaiseFailed();
                }
            }
            #endregion
        }