Пример #1
0
        public async UniTask <T> GetAssetAsync <T>(string packageName, string assetName, AssetLoadArgs args = null) where T : UnityEngine.Object
        {
            if (GameEnvironment.Instance.config.LoadAssetPathType == LoadAssetPathType.Editor)
            {
                return(EditorLoadAsset <T>(packageName, assetName));
            }
            else
            {
                AssetPackage package = await LoadAssetBundleAsync(packageName);

                if (package == null)
                {
                    throw new OperationCanceledException();
                }

                T result = await package.LoadAssetAsync <T>(assetName);

                ReleaseAssetBundle(package?.name);

                if (args != null && args.canceled)
                {
                    throw new OperationCanceledException();
                }

                return(result);
            }
        }
Пример #2
0
        private async UniTask m_LoadScene(string packageName, string sceneName)
        {
            AssetPackage ab = await LoadAssetBundleAsync(StringToLower(packageName));

            await SceneManager.LoadSceneAsync(sceneName);

            ReleaseAssetBundle(ab.name);
        }
Пример #3
0
        /// <summary>
        ///  这个函数推荐最开始就加载
        /// </summary>
        /// <param name="shaderPackageName"></param>
        public void CacheAllShader(string packageName)
        {
            AssetPackage ab = LoadAssetBundle(packageName);

            ab?.CacheAllAsset();
            Shader.WarmupAllShaders();
            ReleaseAssetBundle(ab?.name);
        }
Пример #4
0
 public void UnloadAssetBundle(AssetPackage bundle, bool unloadAllLoadedObjects = true)
 {
     if (GameEnvironment.Instance.config.LoadAssetPathType != LoadAssetPathType.Editor)
     {
         HFLog.L("卸载Assetbundle  " + bundle.name);
         bundle.unloading = true;
         ReleaseAssetBundle(bundle.name);
         allAssetBundleDic.Remove(bundle.name);
         bundle.Unload(unloadAllLoadedObjects);
     }
 }
Пример #5
0
 /// <summary>
 ///  卸载某一个 assetbundle 通过名字
 /// </summary>
 /// <param name="name"></param>
 /// <param name="b">是否卸载压出来的的东西</param>
 public void UnloadAssetBundle(string packageName, bool unloadAllLoadedObjects = true)
 {
     if (GameEnvironment.Instance.config.LoadAssetPathType != LoadAssetPathType.Editor)
     {
         AssetPackage bundle = GetAssetBundle(packageName);
         if (bundle != null)
         {
             UnloadAssetBundle(bundle, unloadAllLoadedObjects);
         }
     }
 }
Пример #6
0
        public AssetPackage GetAssetBundle(string packageName)
        {
            packageName = StringToLower(packageName);
            AssetPackage result = null;

            if (!string.IsNullOrEmpty(packageName))
            {
                allAssetBundleDic.TryGetValue(packageName, out result);
            }
            return(result);
        }
Пример #7
0
        private AssetPackage RawLoadAssetBundle(string packageName)
        {
            AssetBundle  bundle  = AssetBundle.LoadFromFile(AutoGetResourcePath(packageName, false));
            AssetPackage package = null;

            if (bundle != null)
            {
                package = new AssetPackage(bundle, packageName);
                allAssetBundleDic.Add(bundle.name, package);
            }
            return(package);
        }
Пример #8
0
        private async UniTask <AssetPackage> RawLoadAssetBundleAsync(string packageName)
        {
            AssetBundle assetbundle = await AssetBundle.LoadFromFileAsync(AutoGetResourcePath(packageName, false));

            AssetPackage package = null;

            if (assetbundle != null)
            {
                package = new AssetPackage(assetbundle, packageName);
                allAssetBundleDic.Add(package.name, package);
            }
            return(package);
        }
Пример #9
0
        /// <summary>
        /// 递归引用计数-1
        /// </summary>
        /// <param name="name"></param>
        public void ReleaseAssetBundle(string packageName)
        {
            AssetPackage bundle = GetAssetBundle(packageName);

            if (bundle != null)
            {
                bundle.Release();
            }
            string[] list = GetAssetBundleDependencies(packageName);
            for (int i = 0; i < list.Length; i++)
            {
                ReleaseAssetBundle(list[i]);
            }
        }
Пример #10
0
        /// <summary>
        ///  加载一系列 的assetbundle
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List <AssetPackage> LoadAssetBundles(string[] list)
        {
            List <AssetPackage> bundles = new List <AssetPackage>();

            for (int i = 0; i < list.Length; i++)
            {
                AssetPackage bundle = LoadAssetBundle(list[i]);
                if (bundle != null)
                {
                    bundles.Add(bundle);
                }
            }
            return(bundles);
        }
Пример #11
0
 /// <summary>
 ///  通用的以同步方式获取一个资源  封装了编辑器 和 ab 两种加载方法
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="packageName"></param>
 /// <param name="assetName"></param>
 /// <returns></returns>
 public T GetAsset <T>(string packageName, string assetName) where T : UnityEngine.Object
 {
     if (GameEnvironment.Instance.config.LoadAssetPathType == LoadAssetPathType.Editor)
     {
         return(EditorLoadAsset <T>(packageName, assetName));
     }
     else
     {
         AssetPackage ab     = LoadAssetBundle(packageName);
         T            result = ab?.LoadAsset <T>(assetName);
         ReleaseAssetBundle(ab?.name);
         return(result);
     }
 }
Пример #12
0
 /// <summary>
 ///  使用这个方法  如果在编辑器首先需要在 EditorSetting 里把 SpritePacker 设置成Always Enabled 在运行之前build一下图集 才能显示
 /// </summary>
 /// <param name="packageName">assetbundle名字</param>
 /// <param name="atlasName">图集名字</param>
 /// <param name="spriteName">图片名字</param>
 /// <returns></returns>
 public Sprite GetSpriteByAtlas(string packageName, string atlasName, string spriteName)
 {
     if (GameEnvironment.Instance.config.LoadAssetPathType == LoadAssetPathType.Editor)
     {
         SpriteAtlas atlas = EditorLoadAsset <SpriteAtlas>(packageName, atlasName);
         return(atlas.GetSprite(spriteName));
     }
     else
     {
         AssetPackage ab = LoadAssetBundle(packageName);
         Sprite       sp = ab?.LoadSprite(atlasName, spriteName);
         ReleaseAssetBundle(ab.name);
         return(sp);
     }
 }
Пример #13
0
 public void LoadHotFixAssembly(string packageName, string dllName, Action <byte[], byte[]> callback)
 {
     //代码 在编辑器 里默认走streamingAssets 生成dll 运行即可
     if (GameEnvironment.Instance.config.LoadAssetPathType == LoadAssetPathType.Editor)
     {
         EditorLoadHotFixAssembly(packageName, dllName, callback);
     }
     else
     {
         AssetPackage ab   = LoadAssetBundle(packageName);
         TextAsset    text = ab?.LoadAsset <TextAsset>(dllName + ".dll");
         if (callback != null)
         {
             callback(text.bytes, null);
         }
         UnloadAssetBundle(ab, true);
     }
 }
Пример #14
0
        private async UniTaskVoid m_LoadAssetsBundlesAsync(string[] list, Action <float> progressCallback)
        {
            List <AssetPackage> bundles = new List <AssetPackage>();

            for (int i = 0; i < list.Length; i++)
            {
                AssetPackage bundle = await LoadAssetBundleAsync(list[i]);

                if (bundle != null)
                {
                    bundles.Add(bundle);
                }
                if (progressCallback != null)
                {
                    progressCallback((bundles.Count + 0.0f) / list.Length);
                }
            }
        }
Пример #15
0
        /// <summary>
        ///  异步递归加载 assetbundle
        /// </summary>
        /// <param name="assetBundleName"></param>
        /// <param name="finishCallback"></param>
        public async UniTask <AssetPackage> LoadAssetBundleAsync(string packageName)
        {
            packageName = StringToLower(packageName);
            AssetPackage package = null;

            if (!allAssetBundleDic.TryGetValue(packageName, out package))
            {
                string[] list = GetAssetBundleDependencies(packageName);
                for (int i = 0; i < list.Length; i++)
                {
                    await LoadAssetBundleAsync(list[i]);
                }

                package = await RawLoadAssetBundleAsync(packageName);
            }
            package?.Retain();
            return(package);
        }
Пример #16
0
        /// <summary>
        ///  一般来说,尽可能使用AssetBundle.LoadFromFile。该API在速度,磁盘使用率和运行时内存使用方面是最有效的
        /// </summary>
        public AssetPackage LoadAssetBundle(string packageName)
        {
            packageName = StringToLower(packageName);
            AssetPackage package = null;

            //判断是否已经加载bundle
            if (!allAssetBundleDic.TryGetValue(packageName, out package))
            {
                //获取依赖
                string[] list = GetAssetBundleDependencies(packageName);
                for (int i = 0; i < list.Length; i++)
                {
                    //加载依赖
                    LoadAssetBundle(list[i]);
                }

                package = RawLoadAssetBundle(packageName);
            }

            package?.Retain();
            return(package);
        }