Exemplo n.º 1
0
        /// <summary>
        /// 进行垃圾回收
        /// </summary>
        internal static void DoGarbageCollect(float curTime)
        {
            foreach (var kv in UnuseLoaders)
            {
                if (curTime - kv.Value >= LoaderDisposeTime)
                {
                    CacheLoaderToRemoveFromUnUsed.Add(kv.Key);
                }
            }

            for (var i = CacheLoaderToRemoveFromUnUsed.Count - 1; i >= 0; i--)
            {
                BaseLoader loader = CacheLoaderToRemoveFromUnUsed[i];
                CacheLoaderToRemoveFromUnUsed.RemoveAt(i);

                UnuseLoaders.Remove(loader);

                loader.Dispose();
            }

            if (CacheLoaderToRemoveFromUnUsed.Count > 0)
            {
                Debuger.LogError(LOG_TAG, "[DoGarbageCollect] CacheLoaderToRemoveFromUnUsed muse be empty!!");
            }
        }
Exemplo n.º 2
0
        public override void Init(string assetBundlePath, string assetPath, LoadMode loadMode, params object[] args)
        {
            base.Init(assetBundlePath, assetPath, loadMode, args);

            LoaderCallback callback = (isOk, asset) =>
            {
                if (IsReadyDisposed)  // 中途释放
                {
                    OnFinish(null);
                    return;
                }

                if (!isOk)
                {
                    OnFinish(null);
                    Debuger.LogError(LOG_TAG, "Error on AssetLoader loaded... {0}|{1}", assetBundlePath, assetPath);
                    return;
                }

                Object prefab   = asset as Object;
                Object instance = Object.Instantiate(prefab);
                instance.name = prefab.name;

                if (Application.isEditor)
                {
                    LoadedAssetDebugger.Create("InstanceAsset", GetUniqueKey(), instance);
                }

                OnFinish(instance);
            };

            _assetLoader = BaseLoader.Load <AssetLoader>(assetBundlePath, assetPath, loadMode, callback, true);
        }
Exemplo n.º 3
0
 // 资源不用时,释放BaseLoader的引用计数,有加载资源就必须调用该方法.
 public void ReleaseLoader(BaseLoader loader)
 {
     if (loader != null)
     {
         loader.Release();
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Loads the instance asset async.
        /// 'assetBundlePath',只需要填相对路径,不用加后缀,如 UI/Panel/MenuPanel,除非ab不是按AssetConfig里面配置的后缀来设置的.
        /// 'assetName',只需要填相对路径,需要加后缀,如 UI/Panel/MenuPanel.prefab
        /// </summary>
        public InstanceAssetLoader LoadInstanceAssetAsync(string assetBundleRelativePath, string assetName, OnLoadInstanceAsset callback)
        {
            // 如果是加载ab中的资源,需要填完成的路径,这跟 BuildAssetBundleOptions 有关,参考BundleBuilder.BuildAssetBundle()
            if (!string.IsNullOrEmpty(assetBundleRelativePath))
            {
                assetName = AssetConfig.GetAssetFullPathInAB(assetName);
            }

            string fullRelativePath    = AssetConfig.GetAssetBundleFullRelativePath(assetBundleRelativePath);
            string assetBundleFullPath = AssetConfig.GetGameAssetFullPath(fullRelativePath);

            LoaderCallback internelHandler = (bool isOk, object resultObject) =>
            {
                if (isOk)
                {
                    callback.Invoke(resultObject as Object);
                }
                else
                {
                    callback.Invoke(null);
                }
            };

            return(BaseLoader.Load <InstanceAssetLoader>(assetBundleFullPath, assetName, callback: internelHandler));
        }
Exemplo n.º 5
0
        public static BaseLoaderDebugger Create(string type, string uniqueKey, BaseLoader loader)
        {
            if (IsApplicationQuit)
            {
                return(null);
            }

            //simplified uniqueKey
            uniqueKey = uniqueKey.Replace(AssetConfig.GetWritablePath(), "").Replace(AssetConfig.GetStreamingAssetsPath(), "").Replace(AssetConfig.GameAssetsFolder, "");

            // create a BaseLoaderDebugger
            GameObject newHelpGameObject = new GameObject(uniqueKey);
            var        newHelp           = newHelpGameObject.AddComponent <BaseLoaderDebugger>();

            newHelp.loader = loader;

            loader.DisposeEvent += () =>
            {
                DebuggerObjectTool.RemoveFromParent(bigType, type, newHelpGameObject);
            };

            // add to hierarchy
            DebuggerObjectTool.SetParent(bigType, type, newHelpGameObject);

            return(newHelp);
        }
Exemplo n.º 6
0
        public static void AddUnusedLoader(BaseLoader loader)
        {
            if (loader == null)
            {
                return;
            }

            UnuseLoaders[loader] = GetCurrentTime();
        }
Exemplo n.º 7
0
        public override void Init(string assetBundlePath, string assetPath, LoadMode loadMode, params object[] args)
        {
            base.Init(assetBundlePath, assetPath, loadMode, args);

            _assetLoader = BaseLoader.Load <AssetLoader>(assetBundlePath, assetPath, loadMode, (isOk, asset) =>
            {
                OnFinish(asset);
            });
        }
Exemplo n.º 8
0
        public static bool RemoveLoader(BaseLoader loader)
        {
            if (loader == null)
            {
                return(false);
            }

            var typeDict = GetTypeDict(loader.GetType());

            return(typeDict.Remove(loader.GetUniqueKey()));   //NOTE: add to remove loader都用 loader._uniqueKey作为key
        }
Exemplo n.º 9
0
        /// <summary>
        /// 统一的对象工厂
        /// </summary>

        public static T AutoNew <T>(string assetBundlePath, string assetPath, LoadMode loadMode, LoaderCallback callback, bool forceCreateNew = false,
                                    params object[] initArgs) where T : BaseLoader, new()
        {
            // 唯一key.
            string uniqueKey = BaseLoader.GenerateKey(assetBundlePath, assetPath);

            Dictionary <string, BaseLoader> typesDict = GetTypeDict(typeof(T));

            BaseLoader loader;

            if (forceCreateNew || !typesDict.TryGetValue(uniqueKey, out loader))
            {
                loader = new T();

                if (!forceCreateNew)
                {
                    typesDict[uniqueKey] = loader;  //NOTE: add to remove loader都用 loader._uniqueKey作为key
                }
                loader.IsForceNew = forceCreateNew;
                loader.Init(assetBundlePath, assetPath, loadMode, initArgs);

                // 创建新的BaseLoader
                if (Application.isEditor)
                {
                    BaseLoaderDebugger.Create(typeof(T).Name, uniqueKey, loader);
                }
            }
            else
            {
                if (loader.RefCount < 0)
                {
                    // 转死回生的可能
                    Debuger.LogError(LOG_TAG, "Error RefCount!");
                }
            }

            // 引用计数+1
            loader.AddRef();

            // RefCount++了,重新激活,在队列中准备清理的Loader
            if (UnuseLoaders.ContainsKey(loader))
            {
                UnuseLoaders.Remove(loader);
                loader.Revive();
            }

            loader.AddCallback(callback);

            return(loader as T);
        }
Exemplo n.º 10
0
        IEnumerator Start()
        {
            string abPath = AssetBundlePath;

            //load dependence
            var deps = GetAllDependencies(abPath);

            if (deps.Length > 0)
            {
                _depLoaders = new AssetBundleLoader[deps.Length];
                for (int i = 0; i < deps.Length; i++)
                {
                    _depLoaders[i] = BaseLoader.Load <AssetBundleLoader>(deps[i], "", loadMode, null);
                }

                for (var l = 0; l < _depLoaders.Length; l++)
                {
                    while (!_depLoaders[l].IsCompleted)
                    {
                        yield return(null);
                    }
                }
            }

            // load self
            AssetBundle ab = null;

            if (loadMode == LoadMode.Async)
            {
                var request = AssetBundle.LoadFromFileAsync(abPath);
                while (!request.isDone)
                {
                    yield return(null);
                }

                ab = request.assetBundle;
            }
            else
            {
                ab = AssetBundle.LoadFromFile(abPath);
            }

            if (ab == null)
            {
                Debuger.LogError(LOG_TAG, "Load assetBundle from {0} failed", abPath);
            }

            OnFinish(ab);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Loads the asset bundle async.
        /// 'assetBundlePath',只需要填相对路径,不用填后缀,如 UI/Panel/MenuPanel,除非ab不是按AssetConfig里面配置的后缀来设置的.
        /// </summary>

        public AssetBundleLoader LoadAssetBundleAsync(string assetBundleRelativePath, OnLoadAssetBundle callback)
        {
            string fullRelativePath    = AssetConfig.GetAssetBundleFullRelativePath(assetBundleRelativePath);
            string assetBundleFullPath = AssetConfig.GetGameAssetFullPath(fullRelativePath);

            LoaderCallback internelHandler = (bool isOk, object resultObject) =>
            {
                if (isOk)
                {
                    callback.Invoke(resultObject as AssetBundle);
                }
                else
                {
                    callback.Invoke(null);
                }
            };

            return(BaseLoader.Load <AssetBundleLoader>(assetBundleFullPath, "", callback: internelHandler));
        }
Exemplo n.º 12
0
        IEnumerator Start()
        {
            Object getAsset = null;

#if UNITY_EDITOR
            if (IsEditorLoadAsset)
            {
                getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath <Object>(AssetConfig.GetEditorAssetPathRoot() + AssetPath);
                if (getAsset == null)
                {
                    Debuger.LogError("Asset is NULL(from {0} Folder): {1}", AssetConfig.GetEditorAssetPathRoot(), AssetPath);
                }


                OnFinish(getAsset);
            }
            else
#endif
            if (!IsLoadAssetBundle)
            {
                string extension = Path.GetExtension(AssetPath);
                string path      = AssetPath.Substring(0, AssetPath.Length - extension.Length); // remove extensions

                // 去掉 "GameAssets/"
                if (path.StartsWith(AssetConfig.GameAssetsFolder))
                {
                    path = path.Replace(AssetConfig.GameAssetsFolder, "");
                }

                getAsset = Resources.Load <Object>(path);
                if (getAsset == null)
                {
                    Debuger.LogError("Asset is NULL(from Resources Folder): {0}", path);
                }
                OnFinish(getAsset);
            }
            else
            {
                _bundleLoader = BaseLoader.Load <AssetBundleLoader>(AssetBundlePath, "", loadMode, null);

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

                if (!_bundleLoader.IsSuccess)
                {
                    Debuger.LogError(LOG_TAG, "Load bundle Failed(Error) when Finished: {0}", AssetBundlePath);
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                var assetBundle = _bundleLoader.assetBundle;

                var assetName = AssetPath;
                if (!assetBundle.isStreamedSceneAssetBundle)
                {
                    if (loadMode == LoadMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(assetName);

                        if (getAsset != null)
                        {
                            _bundleLoader.PushLoadedAsset(assetName, getAsset);
                        }
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(assetName);
                        while (!request.isDone)
                        {
                            yield return(null);
                        }

                        getAsset = request.asset;

                        if (getAsset != null)
                        {
                            _bundleLoader.PushLoadedAsset(assetName, getAsset);
                        }
                    }
                }
                else
                {
                    // if it's a scene in asset bundle, do nothing
                    // but set a default Object as the result

                    //TODO:
                    Debuger.LogWarning(LOG_TAG, "Can't load any assets from A scene asset bundle");
                    getAsset = null;
                }

                if (getAsset == null)
                {
                    Debuger.LogError(LOG_TAG, "Asset is NULL(From asset bundle): {0}", AssetPath);
                }
            }

            if (Application.isEditor)
            {
                if (getAsset != null)
                {
                    LoadedAssetDebugger.Create(getAsset.GetType().Name, GetUniqueKey(), getAsset);
                }
            }

            OnFinish(getAsset);
        }