Пример #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
        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);
        }
Пример #3
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);
        }