Пример #1
0
        private void SelectLoaderAndPath <T>(string path, out BaseLoader loader, out string realpath) where T : class
        {
            if (AssetPathUtil.IsUrl(path))
            {
                loader   = GetOrCreateNetworkLoader();
                realpath = path;
            }
            else
            {
                //如果是双路径
                if (path.IndexOf('|') >= 0)
                {
#if UNITY_EDITOR
                    //编辑器下直接加载源路径
                    if (!isUseBundleLoader)
                    {
                        loader = GetOrCreateEditorLoader();
                        string[] pathPairs = path.Split('|');
                        string   assetName = pathPairs[1];
                        realpath = assetName;
                    }
                    else
                    {
                        loader   = GetOrCreateBundleLoader();
                        realpath = path;
                    }
#else
                    loader   = GetOrCreateBundleLoader();
                    realpath = path;
#endif
                }
                else
                {
                    //是否是Asset开头的
                    if (typeof(T) == typeof(byte[]))   //二进制加载器
                    {
                        loader   = GetOrCreateBinaryLoader();
                        realpath = path;
                        return;
                    }
#if UNITY_EDITOR
                    else if (path.StartsWith("assets", StringComparison.OrdinalIgnoreCase))
                    {
                        loader   = GetOrCreateEditorLoader();
                        realpath = path;
                        return;
                    }
#endif
                    //最后采用ResourceLoader
                    loader   = GetOrCreateResourceeLoader();
                    realpath = path;
                }
            }
        }
Пример #2
0
        protected override LoadMethod OnLoadMethod(AssetLoadHandler handler)
        {
            //如果缓冲池有,则下一帧回调,否则协程回调
            string assetPath = handler.path;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out _);

            if (GetBundleObject(assetPath) != null)
            {
                return(LoadMethod.Nextframe);
            }
            return(LoadMethod.Coroutine);
        }
Пример #3
0
        protected override void OnUnLoad(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                string assetPath = path;
                AssetPathUtil.SpliteBundlePath(path, out assetPath, out _);

                if (m_bundlesMap.TryGetValue(assetPath, out var bundleObj))
                {
                    UnloadBundleObject(bundleObj);
                }
            }
        }
Пример #4
0
        public int LoadAssetSync <T>(string path, Action <T> onSuccess = null, Action <int> onFailed = null) where T : class
        {
            path = AssetPathUtil.NormalizePath(path);
            AssetLoadHandler handler = LoadAssetHandler <T>(path, (AssetLoadResult result) =>
            {
                if (result.isDone && result.asset != null)
                {
                    onSuccess?.Invoke(result.GetAsset <T>());
                }
                else
                {
                    onFailed?.Invoke(AssetLoadStatus.LOAD_ERROR);
                }
            });

            return(handler.id);
        }
Пример #5
0
        public int LoadAssetAsync <T>(string path, Action <T> onSuccess = null, Action <int> onFailed = null) where T : class
        {
            path = AssetPathUtil.NormalizePath(path);
            var handler = LoadAssetHandler <T>(path);

            handler.OnCompleted((AssetLoadResult result) =>
            {
                if (handler.status == AssetLoadStatus.LOAD_FINISHED)
                {
                    onSuccess?.Invoke(result.GetAsset <T>());
                }
                else
                {
                    onFailed?.Invoke(handler.status);
                }
            });
            return(handler.id);
        }
Пример #6
0
        protected override void OnStartLoad(AssetLoadHandler handler)
        {
            var    mainHandler = handler;
            string assetPath   = mainHandler.path;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out _);


            //加载顺序决定是否能完全卸载,如果先加载依赖,在加载自己,就能够完全释放(这个与释放顺序无关
            var mainDependencies = GetBundleDependencies(assetPath, false);

            if (mainDependencies != null && mainDependencies.Length > 0)
            {
                foreach (var subDependence in mainDependencies)
                {
                    var subHandler = GetOrCreateHandler(subDependence);
                    mainHandler.AddChild(subHandler);

                    StartLoadWithHandler(subHandler);
                }
            }

            base.OnStartLoad(mainHandler);
        }
Пример #7
0
        private void LoadAssetPrimitiveSync(AssetLoadHandler handler)
        {
            //同步的方法
            if (string.IsNullOrEmpty(handler.path))
            {
                LoadAssetPrimitiveCallback(handler, AssetLoadResult.EMPTY_RESULT);
                return;
            }

            string assetPath = handler.path;
            string assetName = null;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out assetName);

            Object asset  = null;
            bool   isDone = false;

            //是否已经在加载池中,如果是就直接返回,引用数加1
            var bundleObject = GetBundleObject(assetPath);

            if (bundleObject != null)
            {
                asset  = bundleObject.assetBundle;
                isDone = true;
            }
            else
            {
                //不支持同步的网络下载
                if (AssetPathUtil.IsUrl(assetPath))
                {
                    asset  = null;
                    isDone = false;
                }
                else
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_LOCAL_STAY_TIME;    //本地Handler超时时间
                    var assetObj = AssetBundle.LoadFromFile(assetPath);

                    asset  = assetObj;
                    isDone = true;
                }

                LoadAssetBundleCallback(handler, asset as AssetBundle);
            }

            ////////////////////////////////
            var assetBundle = asset as AssetBundle;

            if (assetBundle != null)
            {
                if (!string.IsNullOrEmpty(assetName))
                {
                    asset  = assetBundle.LoadAsset(assetName);
                    isDone = true;
                }
            }

            var result = new AssetLoadResult(asset, isDone);

            LoadAssetPrimitiveCallback(handler, result);
        }
Пример #8
0
        //加载元操作
        private IEnumerator LoadAssetPrimitiveAsync(AssetLoadHandler handler)
        {
            if (string.IsNullOrEmpty(handler.path))
            {
                LoadAssetPrimitiveCallback(handler, AssetLoadResult.EMPTY_RESULT);
                yield break;
            }

            string assetPath = handler.path;
            string assetName = null;

            AssetPathUtil.SpliteBundlePath(handler.path, out assetPath, out assetName);

            Object asset  = null;
            bool   isDone = false;

            //是否已经在加载池中,如果是就直接返回,引用数加1
            var bundleObject = GetBundleObject(assetPath);

            if (bundleObject != null)
            {
                asset  = bundleObject.assetBundle;
                isDone = true;
            }
            else
            {
                if (AssetPathUtil.IsUrl(assetPath))                                     //是否为网络路径
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_NETWORK_STAY_TIME; //网络Handler超时时间
                    var request = UnityWebRequestAssetBundle.GetAssetBundle(assetPath);
                    request.timeout = (int)handler.timeoutChecker.stayTime;
                    m_handlerWithRequestMap[handler.id] = new RequestObj()
                    {
                        id         = handler.id,
                        webRequest = request,
                    };
                    yield return(request.SendWebRequest());

                    asset  = DownloadHandlerAssetBundle.GetContent(request);
                    isDone = request.isDone;
                }
                else
                {
                    handler.timeoutChecker.stayTime = HANDLER_BUNDLE_LOCAL_STAY_TIME;    //本地Handler超时时间
                    var request = AssetBundle.LoadFromFileAsync(assetPath);
                    m_handlerWithRequestMap[handler.id] = new RequestObj()
                    {
                        id        = handler.id,
                        abRequest = request,
                    };
                    yield return(request);

                    asset  = request.assetBundle;
                    isDone = request.isDone;
                }

                //先把加载到的AssetBundle加入记录缓存,并且标记引用次数+1
                //不记录Bundle为空的项
                LoadAssetBundleCallback(handler, asset as AssetBundle);
            }

            ////////////////////////////////
            var assetBundle = asset as AssetBundle;

            if (assetBundle != null)
            {
                if (!string.IsNullOrEmpty(assetName))
                {
                    var loadRequest = assetBundle.LoadAssetAsync(assetName);
                    yield return(loadRequest);

                    asset  = loadRequest.asset;
                    isDone = loadRequest.isDone;
                }
            }

            var result = new AssetLoadResult(asset, isDone);

            LoadAssetPrimitiveCallback(handler, result);
        }