Beispiel #1
0
        public static T GetAssetBundleFormCache <T>(string resName, string assetBundleFilePath, bool isInstantiate = false, bool isForever = false) where T : UnityEngine.Object
        {
            T result = null;

            try
            {
                C_AssetBundleRef bundle = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().GetAssetBundleRefList(assetBundleFilePath);

                if (bundle != null)
                {
                    result = bundle.LoadAsset <T>(resName);

                    if (isInstantiate && result != null)
                    {
                        result = GameObject.Instantiate(result);
                    }
                }
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("GetAssetBundleFormCache : " + e);
            }

            return(result);
        }
Beispiel #2
0
        //加载资源,所有加载之后的bundle都添加到m_AssetBundleRefList
        public C_AssetBundleRef LoadFromFile(string filePath, bool isCache)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(null);
            }

            //使用缓存AB 数据
            C_AssetBundleRef listAssetBundleRef = GetAssetBundleRefList(filePath);

            if (listAssetBundleRef != null)
            {
                if (isCache)
                {
                    listAssetBundleRef.RefCacheCount++;
                }
                else
                {
                    listAssetBundleRef.RefCount++;
                }

                return(listAssetBundleRef);
            }

            //Debug.LogError("filePath "+ filePath+" is loading local...");
            //通过 LoadFromFile类获取数据

            AssetBundle assetBundle = AssetBundle.LoadFromFile(filePath);

            if (assetBundle != null)
            {
                C_AssetBundleRef assetBundleRef = new C_AssetBundleRef();
                assetBundleRef.FilePath = filePath;

                if (isCache)
                {
                    assetBundleRef.RefCacheCount = 1;
                }
                else
                {
                    assetBundleRef.RefCount = 1;
                }

                assetBundleRef.Bundle = assetBundle;

                m_AssetBundleRefList.Add(assetBundleRef);

                return(assetBundleRef);
            }

            return(null);
        }
Beispiel #3
0
        public void ForceCacheUnload(C_AssetBundleRef assetBundleRef)
        {
            ClearInvalidAssetBundle();

            for (int i = 0; i < m_ForceDirtyAssetBundleRefList.Count; i++)
            {
                if (m_ForceDirtyAssetBundleRefList[i] != null && m_AssetBundleRefList[i].FilePath == assetBundleRef.FilePath)
                {
                    return;
                }
            }

            m_ForceDirtyAssetBundleRefList.Add(assetBundleRef);
        }
Beispiel #4
0
        public static T LoadAssetBundle <T>(string resName, string assetBundleFilePath, List <string> dpsList, bool isInstantiate = false, bool isForever = false) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(resName) || string.IsNullOrEmpty(assetBundleFilePath))
            {
                return(null);
            }

            T result = null;

            try
            {
                C_AssetBundleRef[] abs = new C_AssetBundleRef[dpsList.Count];
                for (int i = 0; i < abs.Length; i++)
                {
                    C_DebugHelper.LogFormat("LoadResource abs[{0}]: {1}", i, dpsList[i]);

                    abs[i] = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().LoadFromFile(dpsList[i], isForever);
                }

                C_AssetBundleRef bundle = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().LoadFromFile(assetBundleFilePath, isForever);

                if (bundle != null)
                {
                    result = bundle.LoadAsset <T>(resName);

                    if (isInstantiate && result != null)
                    {
                        result = GameObject.Instantiate(result);
                    }

                    bundle.AutoUnload();
                }

                for (int i = 0; i < abs.Length; i++)
                {
                    if (abs[i] != null)
                    {
                        abs[i].AutoUnload();
                    }
                }
            }
            catch (Exception e)
            {
                C_DebugHelper.LogError("LoadAssetBundle : " + e);
            }

            return(result);
        }
Beispiel #5
0
        public C_AssetBundleRef AddAssetBundle(string filePath, AssetBundle bundle, bool isCache)
        {
            C_AssetBundleRef listAssetBundleRef = GetAssetBundleRefList(filePath);

            if (listAssetBundleRef != null)
            {
                if (isCache)
                {
                    listAssetBundleRef.RefCacheCount++;
                }
                else
                {
                    listAssetBundleRef.RefCount++;
                }

                return(listAssetBundleRef);
            }

            C_AssetBundleRef assetBundleRef = new C_AssetBundleRef();

            assetBundleRef.FilePath = filePath;

            if (isCache)
            {
                assetBundleRef.RefCacheCount = 1;
            }
            else
            {
                assetBundleRef.RefCount = 1;
            }

            assetBundleRef.Bundle = bundle;

            m_AssetBundleRefList.Add(assetBundleRef);

            return(assetBundleRef);
        }
Beispiel #6
0
        private IEnumerator ExecuteLoader()
        {
            if (m_DependenciesFilePathList.Count > loadedCount)
            {
                string strDependenciesFilePath = m_DependenciesFilePathList[loadedCount];

                C_AssetBundleRef aref = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().GetAssetBundleRefList(strDependenciesFilePath);

                if (aref == null)
                {
                    AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(strDependenciesFilePath);

                    yield return(request);

                    aref = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().AddAssetBundle(strDependenciesFilePath, request.assetBundle, m_IsForever);
                }
                else
                {
                    aref.RefCount++;
                }

                m_DependenciesAssetBundleList.Add(aref);

                loadedCount++;

                ExecuteLoader();
            }
            else
            {
                C_AssetBundleRef aref = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().GetAssetBundleRefList(m_strAssetBundleFilePath);

                if (aref == null)
                {
                    AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(m_strAssetBundleFilePath);

                    yield return(request);

                    aref = C_MonoSingleton <C_AssetBundleMgr> .GetInstance().AddAssetBundle(m_strAssetBundleFilePath, request.assetBundle, m_IsForever);
                }
                else
                {
                    aref.RefCount++;
                }

                T tempObject = null;

                if (!string.IsNullOrEmpty(m_strResName))
                {
                    AssetBundleRequest assetBundleRequest = aref.Bundle.LoadAssetAsync <T>(m_strResName);

                    yield return(assetBundleRequest);

                    tempObject = assetBundleRequest.asset as T;

                    if (m_IsInstantiate && tempObject != null)
                    {
                        tempObject = GameObject.Instantiate(tempObject);
                    }
                }

                if (m_Callback != null)
                {
                    m_Callback(tempObject);
                }

                aref.AutoUnload();

                for (int i = 0; i < m_DependenciesAssetBundleList.Count; i++)
                {
                    if (m_DependenciesAssetBundleList[i] != null)
                    {
                        m_DependenciesAssetBundleList[i].AutoUnload();
                    }
                }
            }
        }
Beispiel #7
0
        void Update()
        {
            try
            {
                //强制卸载
                if (m_ForceDirtyAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_ForceDirtyAssetBundleRefList.Count; i++)
                    {
                        // Debug.Log("--ab name:" + m_ForceDirtyAssetBundleRefList[i].Bundle.name + "start unload.........");
                        if (m_ForceDirtyAssetBundleRefList[i].Bundle != null)
                        {
                            m_ForceDirtyAssetBundleRefList[i].Bundle.Unload(true);
                        }
                    }

                    m_ForceDirtyAssetBundleRefList.Clear();
                }

                if (m_UnloadAssetBundleRefList.Count > 0)
                {
                    for (int i = 0; i < m_UnloadAssetBundleRefList.Count; i++)
                    {
                        if (m_UnloadAssetBundleRefList[i].Bundle != null)
                        {
                            m_UnloadAssetBundleRefList[i].Bundle.Unload(false);
                        }
                    }

                    m_UnloadAssetBundleRefList.Clear();

                    //黄志龙,后续修改到loading界面去释放
                    Resources.UnloadUnusedAssets();
                }

                for (int i = m_DirtyAssetBundleRefList.Count - 1; i >= 0; i--)
                {
                    if (m_DirtyAssetBundleRefList[i] == null || m_DirtyAssetBundleRefList[i].Bundle == null)
                    {
                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                    else
                    {
                        m_UnloadAssetBundleRefList.Add(m_DirtyAssetBundleRefList[i]);

                        m_DirtyAssetBundleRefList.RemoveAt(i);
                    }
                }
                if (m_NeedLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_NeedLoadCacheDict.Keys);

                    for (int i = m_NeedLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_NeedLoadCacheDict[keyList[i]];

                        if (valueList.Count > 4)
                        {
                            LoadFromFile(valueList[valueList.Count - 1], true);
                            LoadFromFile(valueList[valueList.Count - 2], true);
                            LoadFromFile(valueList[valueList.Count - 3], true);
                            LoadFromFile(valueList[valueList.Count - 4], true);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else if (valueList.Count > 0)
                        {
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "start");

                            LoadFromFile(valueList[valueList.Count - 1], true);
                            C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + valueList[valueList.Count - 1] + "end");

                            m_NeedLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_NeedLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }



                if (m_UnLoadCacheDict.Count > 0)
                {
                    List <string> keyList = new List <string>(m_UnLoadCacheDict.Keys);

                    for (int i = m_UnLoadCacheDict.Count - 1; i >= 0; i--)
                    {
                        List <string> valueList = m_UnLoadCacheDict[keyList[i]];

                        /* if (valueList.Count > 4)
                         * {
                         *  C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 2]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 3]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *  abr = GetAssetBundleRefList(valueList[valueList.Count - 4]);
                         *  if (abr != null)
                         *      abr.CacheUnload();
                         *
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 2);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 3);
                         *  m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 4);
                         *
                         * }  else*/if (valueList.Count > 0)
                        {
                            C_AssetBundleRef abr = GetAssetBundleRefList(valueList[valueList.Count - 1]);
                            if (abr != null)
                            {
                                abr.CacheUnload();
                                C_DebugHelper.Log("id:" + valueList.Count + "--ab name:" + abr.Bundle.name);
                            }

                            m_UnLoadCacheDict[keyList[i]].RemoveAt(valueList.Count - 1);
                        }
                        else
                        {
                            m_UnLoadCacheDict.Remove(keyList[i]);
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                C_DebugHelper.LogError("NeedLoadCache error is :" + e);
            }
        }