コード例 #1
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
        /// <summary>
        /// 从streamingAssetPath中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadAssetBundleFromStreaming(string strABName)
        {
            string path = itfStreamingPath + strABName;

            Debug.Log("streamingAssetsPath: " + path);
            AssetBundle assetBundle = AssetBundle.LoadFromFile(path);

            if (assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedAB = null;
                if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB))
                {
                    loadedAB.assetBundle      = assetBundle;
                    loadedAB.nReferencedCount = 1;
                }
                else
                {
                    Debug.LogError("LoadAssetBundleFromStreaming error, tagLoadedAssetBundle is null!");
                }

                Debug.Log("load " + strABName + " bundle from streming succeed!");
            }
            else
            {
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + "from streaming is invalid!");
            }
        }
コード例 #2
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
        /// <summary>
        /// 移除已经不再使用的AB包
        /// </summary>
        public void UnloadUnusedAssetBundles()
        {
            if (m_dictLoadedAssetBundles != null &&
                m_dictLoadedAssetBundles.Count > 0)
            {
                List <tagLoadedAssetBundle> temp = new List <tagLoadedAssetBundle>();
                Dictionary <string, tagLoadedAssetBundle> .Enumerator itor = m_dictLoadedAssetBundles.GetEnumerator();
                while (itor.MoveNext())
                {
                    tagLoadedAssetBundle loadedAB = itor.Current.Value;
                    if (loadedAB != null)
                    {
                        if (loadedAB.nReferencedCount < 0)
                        {
                            Debug.LogError("UnloadUnusedAssetBundles error, assetBundle name: " + loadedAB.assetBundle.name + ", referenced count: " + loadedAB.nReferencedCount);
                        }

                        if (loadedAB.nReferencedCount <= 0)
                        {
                            temp.Add(loadedAB);
                        }
                    }
                }

                // 卸载
                for (int i = 0; i < temp.Count; i++)
                {
                    _RealUnloadAB(temp[i]);
                }
            }
        }
コード例 #3
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
        /// <summary>
        /// 从streamingAssetPath中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _LoadAssetBundleAsycFromStreaming(string strABName)
        {
            string path = itfStreamingPath + strABName;

            Debug.Log("streamingAssetsPath: " + path);
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

            yield return(request);

            if (request != null &&
                request.assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedBundle = new tagLoadedAssetBundle();
                loadedBundle.Init(request.assetBundle);
                m_dictLoadedAssetBundles.Add(strABName, loadedBundle);

                Debug.Log("load " + strABName + " bundle from streming succeed!");
            }
            else
            {
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + "from streaming is invalid!");
            }
        }
コード例 #4
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
        /// <summary>
        /// 加载依赖
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadDependencies(string strABName)
        {
            //从manifest获取依赖包名称
            if (m_assetBundleManifest == null)
            {
                Debug.LogError("Manifest file has not been loaded");
                return;
            }

            string[] dependencies = m_assetBundleManifest.GetDirectDependencies(strABName);
            if (dependencies != null &&
                dependencies.Length == 0)
            {
                return;
            }

            // 记录依赖
            tagLoadedAssetBundle loadedAB = null;

            if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB))
            {
                loadedAB.arrDependencies = dependencies;
            }

            // 加载依赖包
            for (int i = 0; i < dependencies.Length; i++)
            {
                _LoadAssetBundle(dependencies[i]);
            }
        }
コード例 #5
0
        /// <summary>
        /// 卸载AB包,不会实际释放,只是操作引用计数
        /// </summary>
        /// <param name="strABName"></param>
        private void _UnloadAssetBundleInternal(string strABName, bool bUnloadAB)
        {
            if (strABName == null
                || strABName.Length == 0)
            {
                Debug.LogError("assetBundleName cannot be null!");
                return;
            }

            //卸载其依赖包
            _UnloadDependencies(strABName, bUnloadAB);

            //获得加载的包
            tagLoadedAssetBundle loadedBundle = null;
            if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedBundle))
            {
                //引用计数减一
                loadedBundle.nReferencedCount--;

                if (bUnloadAB == true
                    && loadedBundle.nReferencedCount <= 0)
                {
                    if (loadedBundle.nReferencedCount < 0)
                        Debug.LogError("UnloadUnusedAssetBundles error, assetBundle name: " + loadedBundle.assetBundle.name + ", referenced count: " + loadedBundle.nReferencedCount);

                    _RealUnloadAB(loadedBundle);
                }
            }
            else
                Debug.LogError("the bundle " + strABName + " to unload is not exist!");
        }
コード例 #6
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
        /// <summary>
        /// 从cache中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _LoadAssetBundleAsycFromCache(string strABName)
        {
            //检查manifest
            if (m_assetBundleManifest == null ||
                AssetBundleInfoManager.IsExits() ||
                AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath) == null ||
                AssetBundleInfoManager.GetSingel().GetBundleInfo(itfDownloadPath).IsCached(strABName) == false)
            {
                Debug.LogError("LoadAssetBundleFromCache error!");
                yield break;
            }

            //加载包
            string path = itfDownloadPath + strABName;
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);

            yield return(request);

            if (request.isDone &&
                request.assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedBundle = new tagLoadedAssetBundle();
                loadedBundle.Init(request.assetBundle);
                m_dictLoadedAssetBundles.Add(strABName, loadedBundle);

                Debug.Log("load " + strABName + " bundle from cache succeed!");
            }
            else
            {
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + "from cache is invalid!");
            }
        }
コード例 #7
0
        /// <summary>
        /// 从cache中加载包
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadAssetBundleFromCache(string strABName)
        {
            //检查manifest
            if (m_assetBundleManifest == null
                || AssetBundleInfoManager.IsExits() == false
                || AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) == null
                || AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == false)
            {
                Debug.LogError("LoadAssetBundleFromCache error!");
                return;
            }

            //加载包
            string path = itfDownloadPath + strABName;
            AssetBundle assetBundle = AssetBundle.LoadFromFile(path);

            if (assetBundle != null)
            {
                //包加载成功
                tagLoadedAssetBundle loadedAB = null;
                if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB))
                {
                    loadedAB.assetBundle = assetBundle;
                    loadedAB.nReferencedCount = 1;
                }
                else
                    Debug.LogError("LoadAssetBundleFromCache error, tagLoadedAssetBundle is null!");

                Debug.Log("load " + strABName + " bundle from cache succeed!");
            }
            else
                //包加载失败
                Debug.Log("load assetBundle:" + strABName + " from cache fail!");
        }
コード例 #8
0
        /// <summary>
        /// 加载AssetBundle,先查找cache里面,没有就查找streamingAssetPath
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private void _LoadAssetBundle(string strABName)
        {
            // 检察是否已经加载
            tagLoadedAssetBundle loadedAB = null;
            m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB);
            if (loadedAB != null)
            {
                // 所有资源都要先加载依赖包
                _LoadDependencies(strABName);

                //已经加载后又再次加载,引用+1
                loadedAB.nReferencedCount++;
                Debug.Log("load " + strABName + " success, and already downloaded just add reference count!");
            }
            else
            {
                // 提前生成,后面再填充数据
                loadedAB = new tagLoadedAssetBundle();
                m_dictLoadedAssetBundles.Add(strABName, loadedAB);

                // 所有资源都要先加载依赖包
                _LoadDependencies(strABName);

                if (AssetBundleInfoManager.IsExits() == true
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) != null
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == true)
                {
                    _LoadAssetBundleFromCache(strABName);
                }
                else
                {
                    _LoadAssetBundleFromStreaming(strABName);
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="strABName"></param>
        /// <param name="strAssetName"></param>
        /// <param name="bMainAsset">主资源,包中的第一个资源</param>
        /// <returns></returns>
        private Object _LoadAssetInternal(string strABName, string strAssetName = null, bool bMainAsset = false)
        {
            if (strABName != null
                && strABName.Length != 0)
            {
                tagLoadedAssetBundle loadedBundle = null;
                if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedBundle))
                {
                    if (loadedBundle.assetBundle != null)
                    {
                        // 加载主资源
                        if (bMainAsset)
                        {
                            string[] names = loadedBundle.assetBundle.GetAllAssetNames();
                            if (names != null
                                && names[0] != null)
                            {
                                return loadedBundle.assetBundle.LoadAsset(names[0]);
                            }
                        }
                        if (strAssetName == null) {
                            int index = strABName.LastIndexOf('/');
                            strAssetName = strABName.Substring(index + 1);
                        }
                        return loadedBundle.assetBundle.LoadAsset(strAssetName);
                    }
                    else
                        Debug.LogError("the bundle of loaded asset bundle:" + strABName + " is null!");
                }
                else
                    Debug.LogError("the asset bundle:" + strABName + " has not been loaded!");
            }

            return null;
        }
コード例 #10
0
        /// <summary>
        /// 加载AssetBundle,先查找cache里面,没有就查找streamingAssetPath
        /// </summary>
        /// <param name="strABName"></param>
        /// <returns></returns>
        private IEnumerator _LoadAssetBundleAsyc(string strABName)
        {
            //if (m_gameobject == null)
            //{
            //    Debug.LogError("GameMainObj not exist!");
            //    yield break;
            //}

            // 检察是否已经加载
            tagLoadedAssetBundle bundle = null;
            m_dictLoadedAssetBundles.TryGetValue(strABName, out bundle);
            if (bundle != null)
            {
                //已经加载后又再次加载,引用+1
                bundle.nReferencedCount++;
                Debug.Log("download " + strABName + " success, and already downloaded just add reference count!");

                yield break;
            }

            if (AssetBundleInfoManager.IsExits()
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath) != null
                && AssetBundleInfoManager.GetSingel().GetBundleInfo(AppConst.BundleInfoPath).IsCached(strABName) == true)
            {
                Coroutine routine = StartCoroutine(_LoadAssetBundleAsycFromCache(strABName));
                m_listCoroutine.Add(routine);
                yield return routine;
            }
            else
            {
                Coroutine routine = StartCoroutine(_LoadAssetBundleAsycFromStreaming(strABName));
                m_listCoroutine.Add(routine);
                yield return routine;
            }
        }
コード例 #11
0
ファイル: AssetBundleManager.cs プロジェクト: dreamirror/ab
 /// <summary>
 /// 卸载AB
 /// </summary>
 /// <param name="loadedAB"></param>
 private void _RealUnloadAB(tagLoadedAssetBundle loadedAB)
 {
     if (m_dictLoadedAssetBundles != null &&
         m_dictLoadedAssetBundles.ContainsKey(loadedAB.assetBundle.name) &&
         loadedAB.assetBundle != null)
     {
         string abName = loadedAB.assetBundle.name;
         loadedAB.assetBundle.Unload(false);
         m_dictLoadedAssetBundles.Remove(abName);
         Debug.Log("Unload " + abName + " success!");
     }
 }
コード例 #12
0
        /// <summary>
        /// 卸载依赖包
        /// </summary>
        /// <param name="strABName"></param>
        private void _UnloadDependencies(string strABName, bool bUnloadAB)
        {
            if (strABName == null
                || strABName.Length == 0)
            {
                Debug.LogError("assetBundleName cannot be null!");
                return;
            }

            // 获取依赖包名称
            tagLoadedAssetBundle loadedAB = null;
            if (m_dictLoadedAssetBundles.TryGetValue(strABName, out loadedAB))
            {
                if (loadedAB.arrDependencies != null
                    && loadedAB.arrDependencies.Length > 0)
                {
                    for (int i = 0; i < loadedAB.arrDependencies.Length; i++)
                    {
                        _UnloadAssetBundleInternal(loadedAB.arrDependencies[i], bUnloadAB);
                    }
                }
            }
        }