コード例 #1
0
 /// <summary>
 /// 减少引用计数,立即执行DoGarbageCollect
 /// </summary>
 public virtual void ReleaseImmediate()
 {
     Release();
     ResManager.Collect();
 }
コード例 #2
0
        private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package, Type type, bool loadAll)
        {
            if (type == null)
            {
                type = typeof(UnityEngine.Object);
            }
            object getAsset = null;

            if (ResManager.IsEdiotrMode && Application.isEditor)
            {
#if UNITY_EDITOR
                string projPath = ResourceModuleConfig.BundleResoucesDir + "/" + path;
                if (!string.IsNullOrEmpty(package))
                {
                    string bundleName = UnityEditor.AssetDatabase.GetImplicitAssetBundleName(projPath);
                    if (bundleName != package + ResourceModuleConfig.AssetBundleExt)
                    {
                        Debug.LogErrorFormat("Asset is BundleName set error: {0} {1}\n{2}", package, bundleName, projPath);
                    }
                }

                if (loadAll)
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(projPath);
                }
                else
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(projPath, type);
                }

                if (getAsset == null)
                {
                    Debug.LogErrorFormat("Asset is NULL(from {0} Folder): {1}", ResourceModuleConfig.BundleResoucesDir, path);
                }

                //编辑器状态下模拟异步加载延迟等待指定秒数
                if (loaderMode == LoaderMode.Async)
                {
                    yield return(new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay));
                }
#else
                Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only");
#endif
            }
            else
            {
                DateTime beginTime  = DateTime.Now;
                string   bundlePath = string.IsNullOrEmpty(package) ? path : package;
                _bundleLoader = AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }

                    this.Progress = _bundleLoader.Progress / 2f;
                    yield return(null);
                }

                if (!_bundleLoader.IsSuccess)
                {
                    Debug.LogErrorFormat("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                this.Progress = 0.5f;
                var assetBundle = _bundleLoader.Bundle;
                if (loadAll)
                {
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAllAssets(type);
                    }
                    else
                    {
                        var request = assetBundle.LoadAllAssetsAsync(type);
                        while (!request.isDone)
                        {
                            this.Progress = 0.5f + request.progress / 2f;
                            yield return(null);
                        }
                        getAsset = request.allAssets;
                    }
                }
                else
                {
                    var assetPath = ResourceModuleConfig.BundleResoucesDir + "/" + path;
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(assetPath, type);
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(assetPath, type);
                        while (!request.isDone)
                        {
                            this.Progress = 0.5f + request.progress / 2f;
                            yield return(null);
                        }
                        getAsset = request.asset;
                    }
                }

                ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime);
                if (getAsset == null)
                {
                    Debug.LogErrorFormat("Asset is NULL: {0}", path);
                }
            }

#if UNITY_EDITOR
            if (getAsset != null)
            {
                KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset);
            }
#endif
            OnFinish(getAsset);
        }