Ejemplo n.º 1
0
        /// <summary>
        /// 从资源包中加载目标资源
        /// </summary>
        /// <typeparam name="T">资源类型</typeparam>
        /// <param name="assetBundleLoader">资源包</param>
        /// <param name="assetName">资源名称</param>
        /// <returns>目标资源</returns>
        private AssetBundleRequest LoadAssetAsync(AssetBundleLoader assetBundleLoader, string assetName, Type assettype)
        {
            AssetBundleRequest request = null;

            //加载完成
            if (assetBundleLoader.state == DownLoadState.Loaded)
            {
                if (assetName == null || assetName.Equals(String.Empty))
                {
                    //通过资源名称加载资源
                    int last1 = assetBundleLoader.fileName.LastIndexOf('/');
                    int last2 = assetBundleLoader.fileName.LastIndexOf('.');

                    assetName = assetBundleLoader.fileName.Substring(last1 + 1, last2 - last1 - 1);
                }
                request = assetBundleLoader.assetBundle.LoadAsync(assetName, assettype);
            }

            return(request);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加载并缓存资源包里的所有资源
        /// </summary>
        /// <param name="assetBundleLoader">资源包下载类</param>
        /// <param name="path">路径</param>
        /// <param name="fileName">资源包名</param>
        /// <param name="assetName">资源名</param>
        /// <param name="callBack">加载结束回调</param>
        /// <returns></returns>
        private IEnumerator LoadAndCachedAllAssetInAssetBundle(AssetBundleLoader assetBundleLoader, LoadAssetFinish <UnityEngine.Object> callBack)
        {
            yield return(StartCoroutine(assetBundleLoader.LoadAssetBundle(false, false)));

            //加载完成
            if (assetBundleLoader.state == DownLoadState.Loaded)
            {
                //assetBundleLoader.LoadAllAssets();
                lock (sharedAssets)
                {
                    if (!sharedAssets.ContainsKey(assetBundleLoader.fileName))
                    {
                        sharedAssets.Add(assetBundleLoader.fileName, assetBundleLoader);
                    }
                }
            }
            if (callBack != null)
            {
                callBack(null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载资源里的资源
        /// </summary>
        /// <param name="assetBundleLoader">资源包下载类</param>
        /// <param name="path">路径</param>
        /// <param name="fileName">资源包名</param>
        /// <param name="assetName">资源名</param>
        /// <param name="callBack">加载结束回调</param>
        /// <returns></returns>
        private IEnumerator LoadAssetInAssetBundle(AssetBundleLoader assetBundleLoader)
        {
            string assetName = assetBundleLoader.assetName;
            bool   cache     = assetBundleLoader.cache;
            bool   scene     = assetBundleLoader.isScene;

            string[] depAssetBundles = null;
            //加载依赖资源
            List <AssetBundleLoader> dependAssetBundles = new List <AssetBundleLoader>();

            if (scene)
            {
                //获取依赖描述文件对象
                string            depFileAssetName = getDepAssetFileName(assetBundleLoader.fileName);
                string            path             = getAssetBundleLoaderPath(depFileAssetName);
                AssetBundleLoader dependFileLoader = new AssetBundleLoader(path, depFileAssetName, null);
                yield return(StartCoroutine(dependFileLoader.LoadAssetBundle(false, false)));

                if (dependFileLoader.state == DownLoadState.Loaded)
                {                                                       // 先找出依赖项 名称;
                    TextAsset depTxt = dependFileLoader.assetBundle.mainAsset as TextAsset;
                    depAssetBundles = getDependeAssetFilesName(depTxt); // MapResource/10000/10000qj.assetbundle\r\nMapResource/10000/10000wj.assetbundle
                    dependFileLoader.UnloadAssetBundle();
                }


                if (depAssetBundles != null)
                {
                    // MapResource/10000/10000qj.assetbundle\r\nMapResource/10000/10000wj.assetbundle
                    // 是一些地图 图集图片吧
                    foreach (string depAssetFileName in depAssetBundles)
                    {
                        Log.info(this, "加载" + assetBundleLoader.fileName + "的依赖" + depAssetFileName);
                        string depPath = getAssetBundleLoaderPath(depAssetFileName);
                        dependAssetBundles.Add(new AssetBundleLoader(depPath, depAssetFileName, null));
                    }
                }

                if (dependAssetBundles != null)
                {// 加载 依赖项;
                    foreach (AssetBundleLoader loader in dependAssetBundles)
                    {
                        yield return(StartCoroutine(loader.LoadAssetBundle(false, false)));

                        if (loader.state == DownLoadState.Loaded)
                        {
                            Log.info(this, "成功加载" + loader.path + loader.fileName);
                        }
                    }
                }
            }

            yield return(StartCoroutine(assetBundleLoader.LoadAssetBundle(false, false)));

            string assetKey = getAssetKey(assetBundleLoader.fileName, assetName, assetBundleLoader.assetType);

            //assetKey = "Scene/10001.assetbundle10001GameObject"

            //加载场景
            if (scene)
            {
                //加载场景
                yield return(StartCoroutine(LoadSceneLevel(assetBundleLoader, assetName)));

                //完成
                assetBundleLoader.UnloadAssetBundle();
                foreach (AssetBundleLoader loader in dependAssetBundles)
                {
                    loader.UnloadAssetBundle();
                }
                //加载场景预加载资源
                LoadScenePreAsset(uint.Parse(assetName), assetKey);
                lock (loadingAssetsBundles)
                {
                    loadingAssetsBundles.Remove(assetBundleLoader.fileName);
                }
                System.GC.Collect();
            }
            else // 资源,非场景
            {
                UnityEngine.Object asset = null;
                if (assetBundleLoader.asynLoad)
                {
                    AssetBundleRequest request = LoadAssetAsync(assetBundleLoader, assetName,
                                                                assetBundleLoader.assetType);
                    if (request != null)
                    {
                        yield return(request);

                        asset = request.asset;
                    }
                }
                else
                {
                    asset = LoadAsset(assetBundleLoader, assetName, assetBundleLoader.assetType);
                }
                if (asset == null)
                {
                    if (!assetBundleLoader.fileName.Contains("bust"))
                    {
                        Log.error(this, "asset==null" + assetBundleLoader.fileName);
                    }
                }
                if (cache)
                {
                    cachedAssets[assetKey] = asset;
                }
                if (assetBundleLoader.state == DownLoadState.Loaded && assetBundleLoader.delayUnload != true)
                {
                    assetBundleLoader.UnloadAssetBundle();
                }
                lock (loadingAssetsBundles)
                {
                    loadingAssetsBundles.Remove(assetBundleLoader.fileName);
                }
                callBack(assetKey, asset);
            }

            //加载资源结束后回调
        }