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
        }
        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
        }