示例#1
0
        public void RemovePackage(string type)
        {
            if (RemovePackageRef(type))
            {
                var p = UIPackage.GetByName(type);

                if (p != null)
                {
                    UIPackage.RemovePackage(type);
                }
            }

            /*UIPackage package;
             *
             * if(packages.TryGetValue(type, out package))
             * {
             *  var p = UIPackage.GetByName(package.name);
             *
             *  if (p != null)
             *  {
             *      UIPackage.RemovePackage(package.name);
             *  }
             *
             *  packages.Remove(package.name);
             * }*/

            if (!Define.IsEditorMode)
            {
                string uiBundleDesName = AssetBundleHelper.StringToAB($"{type}_fui");
                string uiBundleResName = AssetBundleHelper.StringToAB(type);
                Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle(uiBundleDesName);
                Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle(uiBundleResName);
            }
        }
示例#2
0
        // 加载ab包中的all assets
        private static async ETTask LoadOneBundleAllAssets(this ResourcesComponent self, ABInfo abInfo)
        {
            CoroutineLock coroutineLock = null;

            try
            {
                coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode());

                if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets)
                {
                    return;
                }

                if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle)
                {
                    // 异步load资源到内存cache住
                    var assets = await AssetBundleHelper.UnityLoadAssetAsync(abInfo.AssetBundle);

                    foreach (UnityEngine.Object asset in assets)
                    {
                        self.AddResource(abInfo.Name, asset.name, asset);
                    }
                }

                abInfo.AlreadyLoadAssets = true;
            }
            finally
            {
                coroutineLock?.Dispose();
            }
        }
示例#3
0
        public async ETTask AddPackageAsync(string type)
        {
            if (Define.IsEditorMode)
            {
                await ETTask.CompletedTask;
                if (AddPackageRef(type))
                {
                    UIPackage.AddPackage($"{FGUI_PACKAGE_DIR}/{type}");
                }
                //UIPackage uiPackage = UIPackage.AddPackage($"{FGUI_PACKAGE_DIR}/{type}");

                //packages.Add(type, uiPackage);
            }
            else
            {
                if (AddPackageRef(type))
                {
                    string             uiBundleDesName    = AssetBundleHelper.StringToAB($"{type}_fui");
                    string             uiBundleResName    = AssetBundleHelper.StringToAB(type);
                    ResourcesComponent resourcesComponent = Game.Scene.GetComponent <ResourcesComponent>();
                    await resourcesComponent.LoadBundleAsync(uiBundleDesName);

                    await resourcesComponent.LoadBundleAsync(uiBundleResName);

                    AssetBundle desAssetBundle = resourcesComponent.GetAssetBundle(uiBundleDesName);
                    AssetBundle resAssetBundle = resourcesComponent.GetAssetBundle(uiBundleResName);
                    UIPackage.AddPackage(desAssetBundle, resAssetBundle);
                }
                //UIPackage uiPackage = UIPackage.AddPackage(desAssetBundle, resAssetBundle);

                //packages.Add(type, uiPackage);
            }
        }
示例#4
0
        public AssetBundle GetAssetBundle(string abName)
        {
            ABInfo abInfo;

            if (!bundles.TryGetValue(AssetBundleHelper.BundleNameToLower(abName), out abInfo))
            {
                throw new Exception($"not found bundle: {abName}");
            }
            return(abInfo.AssetBundle);
        }
示例#5
0
        public void UnloadBundle(string assetBundleName)
        {
            assetBundleName = assetBundleName.BundleNameToLower();

            string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName);

            //Log.Debug($"-----------dep unload {assetBundleName} dep: {dependencies.ToList().ListToString()}");
            foreach (string dependency in dependencies)
            {
                this.UnloadOneBundle(dependency);
            }
        }
示例#6
0
 /// <summary>
 /// 异步加载assetbundle
 /// </summary>
 /// <param name="assetBundleName"></param>
 /// <returns></returns>
 public async ETTask LoadBundleAsync(string assetBundleName)
 {
     assetBundleName = assetBundleName.ToLower();
     string[] dependencies = AssetBundleHelper.GetSortedDependencies(assetBundleName);
     // Log.Debug($"-----------dep load {assetBundleName} dep: {dependencies.ToList().ListToString()}");
     foreach (string dependency in dependencies)
     {
         if (string.IsNullOrEmpty(dependency))
         {
             continue;
         }
         await this.LoadOneBundleAsync(dependency);
     }
 }
示例#7
0
        // 加载ab包中的all assets
        private async ETTask LoadOneBundleAllAssets(ABInfo abInfo)
        {
            using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, abInfo.Name.GetHashCode()))
            {
                if (abInfo.IsDisposed || abInfo.AlreadyLoadAssets)
                {
                    return;
                }

                if (abInfo.AssetBundle != null && !abInfo.AssetBundle.isStreamedSceneAssetBundle)
                {
                    // 异步load资源到内存cache住
                    var assets = await AssetBundleHelper.UnityLoadAssetAsync(abInfo.AssetBundle);

                    foreach (UnityEngine.Object asset in assets)
                    {
                        AddResource(abInfo.Name, asset.name, asset);
                    }
                }

                abInfo.AlreadyLoadAssets = true;
            }
        }
示例#8
0
        private static async ETTask <ABInfo> LoadOneBundleAsync(this ResourcesComponent self, string assetBundleName)
        {
            assetBundleName = assetBundleName.BundleNameToLower();
            ABInfo abInfo;

            if (self.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                return(null);
            }
            string      p           = "";
            AssetBundle assetBundle = null;

            if (!Define.IsAsync)
            {
                if (Define.IsEditor)
                {
                    string[] realPath = Define.GetAssetPathsFromAssetBundle(assetBundleName);
                    foreach (string s in realPath)
                    {
                        string             assetName = Path.GetFileNameWithoutExtension(s);
                        UnityEngine.Object resource  = Define.LoadAssetAtPath(s);
                        self.AddResource(assetBundleName, assetName, resource);
                    }

                    if (realPath.Length > 0)
                    {
                        abInfo = self.AddChild <ABInfo, string, AssetBundle>(assetBundleName, null);
                        self.bundles[assetBundleName] = abInfo;
                        //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                    }
                    else
                    {
                        Log.Error("Bundle not exist! BundleName: " + assetBundleName);
                    }

                    // 编辑器模式也不能同步加载
                    await TimerComponent.Instance.WaitAsync(100);

                    return(abInfo);
                }
            }
            p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
            if (!File.Exists(p))
            {
                p = Path.Combine(PathHelper.AppResPath, assetBundleName);
            }
            Log.Debug("Async load bundle BundleName : " + p);

            // if (!File.Exists(p))
            // {
            //     Log.Error("Async load bundle not exist! BundleName : " + p);
            //     return null;
            // }
            assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p);

            if (assetBundle == null)
            {
                // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                Log.Warning($"assets bundle not found: {assetBundleName}");
                return(null);
            }
            abInfo = self.AddChild <ABInfo, string, AssetBundle>(assetBundleName, assetBundle);
            self.bundles[assetBundleName] = abInfo;
            return(abInfo);
            //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
        }
示例#9
0
        private async ETTask <ABInfo> LoadOneBundleAsync(string assetBundleName)
        {
            assetBundleName = assetBundleName.BundleNameToLower();
            ABInfo abInfo;

            if (this.bundles.TryGetValue(assetBundleName, out abInfo))
            {
                ++abInfo.RefCount;
                //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                return(null);
            }

            string      p           = "";
            AssetBundle assetBundle = null;

            if (!Define.IsAsync)
            {
#if UNITY_EDITOR
                string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                foreach (string s in realPath)
                {
                    string             assetName = Path.GetFileNameWithoutExtension(s);
                    UnityEngine.Object resource  = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s);
                    AddResource(assetBundleName, assetName, resource);
                }

                if (realPath.Length > 0)
                {
                    abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, null);
                    this.bundles[assetBundleName] = abInfo;
                    //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
                }
                else
                {
                    Log.Error("Bundle not exist! BundleName: " + assetBundleName);
                }

                // 编辑器模式也不能同步加载
                await TimerComponent.Instance.WaitAsync(100);
#endif
                return(abInfo);
            }

            p = Path.Combine(PathHelper.AppHotfixResPath, assetBundleName);
            if (!File.Exists(p))
            {
                p = Path.Combine(PathHelper.AppResPath, assetBundleName);
            }

            if (!File.Exists(p))
            {
                Log.Error("Async load bundle not exist! BundleName : " + p);
                return(null);
            }

            assetBundle = await AssetBundleHelper.UnityLoadBundleAsync(p);

            if (assetBundle == null)
            {
                // 获取资源的时候会抛异常,这个地方不直接抛异常,因为有些地方需要Load之后判断是否Load成功
                Log.Warning($"assets bundle not found: {assetBundleName}");
                return(null);
            }

            abInfo = EntityFactory.CreateWithParent <ABInfo, string, AssetBundle>(this, assetBundleName, assetBundle);
            this.bundles[assetBundleName] = abInfo;
            return(abInfo);
            //Log.Debug($"---------------load one bundle {assetBundleName} refcount: {abInfo.RefCount}");
        }