Beispiel #1
0
        public void ReleaseOneUIBundle(string bundleName)
        {
            bundleName = bundleName.ToLower();
            UIBundleCache cache = null;

            m_UIBundleCache.TryGetValue(bundleName, out cache);
            if (cache != null)
            {
                if (cache.dependencyList != null)
                {
                    foreach (var v in cache.dependencyList)
                    {
                        ReleaseOneUIBundle(v);
                    }
                }
                cache.DecreaseUIBundleAssetsRef(bundleName);

                if (cache.GetRefCount <= 0 && !m_BundleUILoadingList.Contains(bundleName))
                {
                    cache.Unload();

                    if (m_UIBundleCache.ContainsKey(bundleName))
                    {
                        m_UIBundleCache.Remove(bundleName);
                    }
                    else
                    {
#if UNITY_EDITOR
                        if (!PlayerSetting.SimulateAssetBundleInEditor)
                        {
                            Debug.LogError("DecreaseUIBundleAssetsRef Is not Exist The uiGroupName Name is" + bundleName);
                        }
#endif
                    }
                }
            }
            else
            {
#if UNITY_EDITOR
                if (!PlayerSetting.SimulateAssetBundleInEditor)
                {
                    Debug.LogError("Unload  bundle Is not Exist The  Name is" + bundleName);
                }
#endif
            }
        }
Beispiel #2
0
        //创建并且添加到Cache中
        public static UIBundleCache NewAndAddUIBundleInCache(AssetBundle CachedAssetBundle, string bundleName, List <string> dependency)
        {
            if (CachedAssetBundle == null || !string.IsNullOrEmpty(bundleName))
            {
                UIBundleCache BundleCache = New();
                BundleCache.UICachedAssetBundle = CachedAssetBundle;
                BundleCache.IncreaseUIBundleAssetsRef();
                BundleCache.dependencyList = dependency;
                return(BundleCache);
            }
            else
            {
#if UNITY_EDITOR
                if (!PlayerSetting.SimulateAssetBundleInEditor)
                {
                    Debug.LogError("NewAndAddUIBundleInCache bundle is null" + bundleName);
                }
#endif
                return(null);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 加载UIBundle
        /// </summary>
        /// <param name="bundleName">bundle的名字</param>
        /// <param name="needDependency">是否是AssetBundle的依赖加载</param>
        /// <param name="isAync">是否是异步加载 true为异步加载,false为同步加载</param>
        /// <returns></returns>
        public IEnumerator LoadPrefabUI(string bundleName, bool needDependency, bool isAync = true)
        {
            //保证bundle不会被同时加载
            while (m_BundleUILoadingList.Contains(bundleName))
            {
                yield return(null);
            }

            //因同时加载卡在协程的继续走会发现已经加载好,直接返回
            if (m_UIBundleCache.ContainsKey(bundleName))
            {
                m_UIBundleCache[bundleName].IncreaseUIBundleAssetsRef();
                string[] dependencies = manifest.GetAllDependencies(Utils.Utils.Text.Format("prefab/{0}.assetbundle", bundleName));
                for (int i = 0; i < dependencies.Length; ++i)
                {
                    string dependencyPath = dependencies[i];
                    if (!dependencyPath.Contains("commondata"))
                    {
                        string name = dependencyPath.Substring(0, dependencyPath.Length - 12);
                        name = name.Substring(7);
                        UIBundleCache cache = null;
                        m_UIBundleCache.TryGetValue(name, out cache);
                        if (cache != null)
                        {
                            cache.IncreaseUIBundleAssetsRef();
                        }
                        else
                        {
                            Debug.LogWarning("Add RefCount failed: " + name);
                        }
                    }
                }
                yield break;
            }

            m_BundleUILoadingList.Add(bundleName);

            List <string> list = null;

            if (needDependency)
            {
                //先判断是否有依赖项,有则加载之
                string[] dependencies = manifest.GetAllDependencies(CloverGame.Utils.Utils.Text.Format("prefab/{0}.assetbundle", bundleName));
                for (int i = 0; i < dependencies.Length; ++i)
                {
                    string dependencyPath = dependencies[i];
                    if (!dependencyPath.Contains("commondata"))
                    {
                        if (list == null)
                        {
                            list = new List <string>();
                        }
                        string name = dependencyPath.Substring(0, dependencyPath.Length - 12);
                        name = name.Substring(7);
                        list.Add(name);
                        yield return(LoadPrefabUI(name, false));
                    }
                }
            }

            string loadPath = GetBundleLoadUrl(PathUIPrefab, bundleName.ToLower(), m_bundleExt);

            AssetBundle loadedBundle = null;

            if (isAync)
            {
                AssetBundleCreateRequest req = GetBundleRequest(loadPath);
                yield return(req);

                loadedBundle = req.assetBundle;
            }
            else
            {
                loadedBundle = AssetBundle.LoadFromFile(loadPath);
            }

            if (loadedBundle != null)
            {
                if (!m_UIBundleCache.ContainsKey(bundleName))
                {
                    UIBundleCache bundleCache = UIBundleCache.NewAndAddUIBundleInCache(loadedBundle, bundleName, list);
                    if (bundleCache != null)
                    {
                        m_UIBundleCache.Add(bundleName, bundleCache);
                    }
                }
                else
                {
#if UNITY_EDITOR
                    if (!PlayerSetting.SimulateAssetBundleInEditor)
                    {
                        Debug.LogError("Load UIBundle Is Not Exist The Bundle Name is" + bundleName);
                    }
#endif
                }
                m_BundleUILoadingList.Remove(bundleName);
            }
        }