Esempio n. 1
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);
        }
Esempio n. 2
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();
                    }
                }
            }
        }