Exemple #1
0
        /// <summary>
        /// 异步加载位于本机的AssetBundle
        /// </summary>
        /// <returns>The load loacl.</returns>
        /// <param name="task">Task.</param>
        private IEnumerator AsyncLoadLoacl(AssetTask task)
        {
            string assetBundleName = task.AssetBundleName;

            // 添加引用
            RefAsset(assetBundleName);

            string fullPath = Path.Combine(DeviceInfo.PersistRootPath, GetPlatformName());

            fullPath = Path.Combine(fullPath, assetBundleName);

            WWW www = new WWW("file:///" + fullPath);

            www.threadPriority = ThreadPriority.High;
            yield return(www);

            if (www.error != null)
            {
                ConsoleEx.DebugLog("WWW download:" + www.error);
            }
            else
            {
                AssetBundleRequest req = www.assetBundle.LoadAsync(task.PrefabName, task.UType);
                yield return(req);

                dicAsset.Add(assetBundleName, req.asset);
                task.Obj = req.asset;
                dicLoadingReq.Remove(assetBundleName);
                www.assetBundle.Unload(false);
                www.Dispose();
                www = null;

                //本地加载完成
                LoadEnd();
                if (task.LoadFinished != null)
                {
                    task.LoadFinished(task);
                }
            }
        }
Exemple #2
0
        public IEnumerator AsyncLoadAll(AssetTask task)
        {
            string assetBundleName = task.AssetBundleName;
            string fullPath        = Path.Combine(DeviceInfo.PersistRootPath, GetPlatformName());

            fullPath = Path.Combine(fullPath, assetBundleName);

            WWW www = new WWW("file:///" + fullPath);

            www.threadPriority = ThreadPriority.High;
            yield return(www);

            if (www.error != null)
            {
                ConsoleEx.DebugLog("WWW download:" + www.error);
            }
            else
            {
                StringBuilder sb = new StringBuilder();

                UObj[] objs = www.assetBundle.LoadAll();
                foreach (UObj obj in objs)
                {
                    sb.Append("name = " + obj.name + "\t type = " + obj.GetType().ToString()).Append("\n");
                }
                ConsoleEx.DebugLog(sb.ToString());

                www.assetBundle.Unload(true);
                www.Dispose();
                www = null;
                //本地加载完成
                LoadEnd();
                if (task.LoadFinished != null)
                {
                    task.LoadFinished(task);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// According to LoadType, it will download or load local sync
        /// 把AssetBundle载入内存,之后再异步Load出来,之后再次删除Assetbundle内存镜像.
        /// </summary>
        /// <returns>The load coroutine.</returns>
        /// <param name="task">Task.</param>
        private IEnumerator AsyncLoadCoroutine(AssetTask task)
        {
            bool errorOcurred = false;

            string assetBundleName = task.AssetBundleName;


            string url    = ResourceSetting.ConverToFtpPath(assetBundleName);
            int    verNum = 1;//Core.Data.sourceManager.getNewNum(assetBundleName + ".unity3d");

            // 添加引用
            RefAsset(assetBundleName);

            if (Caching.IsVersionCached(url, verNum) == false)
            {
                //ConsoleEx.DebugLog("Version Is not Cached, which will download from net!");
            }

            WWW www = null;

            try {
                www = WWW.LoadFromCacheOrDownload(url, verNum);
            } catch (Exception ex) {
                errorOcurred = true;
                // ConsoleEx.DebugLog("LoadFromCacheOrDownload Error : " + ex.ToString());
                if (task.LoadError != null)
                {
                    task.LoadError("[" + assetBundleName + "]:" + ex.ToString());
                }
            }

            if (!errorOcurred)
            {
                dicLoadingReq.Add(assetBundleName, www);
                while (www.isDone == false)
                {
                    if ((task.LoadType == AssetTask.loadType.Only_Download || task.LoadType == AssetTask.loadType.Both_Download_loadlocal) &&
                        task.reportProgress != null)
                    {
                        task.reportProgress(www.progress);
                    }
                    yield return(null);
                }

                // Print the error to the console
                if (!String.IsNullOrEmpty(www.error))
                {
                    //ConsoleEx.DebugLog("["+assetBundleName+"]"+www.error);
                    Debug.LogError("[" + assetBundleName + "]" + www.error);

                    if (task.LoadError != null)
                    {
                        task.LoadError(www.error);
                    }
                    dicLoadingReq.Remove(assetBundleName);
                    errorOcurred = true;
                }

                bool TaskEnd = false;

                if (task.LoadType == AssetTask.loadType.Only_loadlocal || task.LoadType == AssetTask.loadType.Both_Download_loadlocal)
                {
                    if (!errorOcurred)
                    {
                        AssetBundleRequest req = www.assetBundle.LoadAsync(task.PrefabName, task.UType);
                        while (req.isDone == false)
                        {
                            yield return(null);
                        }

                        dicAsset.Add(assetBundleName, req.asset);
                        task.Obj = req.asset;
                        dicLoadingReq.Remove(assetBundleName);
                        www.assetBundle.Unload(false);

                        //--- load local finished ---
                        TaskEnd = true;
                    }
                }
                else
                {
                    dicLoadingReq.Remove(assetBundleName);

                    //--- Downloading finished ---
                    TaskEnd = true;
                }

                //release memeory
                if (www != null)
                {
                    www.Dispose();
                    www = null;
                }

                //start to callback
                if (TaskEnd)
                {
                    LoadEnd();
                    if (task.LoadFinished != null)
                    {
                        task.LoadFinished(task);
                    }
                }
            }
        }