Ejemplo n.º 1
0
 /// <summary>
 /// 加载AB 包 并加载所有的依赖项
 /// </summary>
 /// <param name="assetName"></param>
 /// <returns></returns>
 AssetBundle LoadAssetBundleAndDependenciesByAsset(AssetRecordInfo assetRecord)
 {
     if (assetRecord == null)
     {
         return(null);
     }
     // 加载所有的依赖项
     return(LoadAssetBundleAndDependencies(assetRecord.AssetBundleName, false));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步加载一个资源
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="assetName"></param>
        /// <param name="unloadAssetBundle"></param>
        /// <param name="unloadAllLoadedObjects"></param>
        /// <returns></returns>
        public LoadAssetAsyncOperation LoadAssetAsync <T>(string assetName, bool unloadAssetBundle = true, bool unloadAllLoadedObjects = false) where T : UnityEngine.Object
        {
            try
            {
                if (!IsReady)
                {
                    return(null);
                }

                LoadAssetAsyncOperation operation   = null;
                AssetRecordInfo         assetRecord = GetAssetRecordInfoByAsset(assetName);
                if (assetRecord == null)
                {
                    return(null);
                }

#if UNITY_EDITOR
                if (SimulateAssetBundleInEditor)
                {
                    T asset = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(assetRecord.RelativePath);
                    operation = new LoadAssetAsyncOperation(asset);
                }
                else
#endif
                {
                    AssetBundle        assetBundle = LoadAssetBundleAndDependenciesByAsset(assetRecord);
                    AssetBundleRequest request     = null;
                    if (assetBundle != null)
                    {
                        request = assetBundle.LoadAssetAsync <T>(assetRecord.AssetName);
                        DisposeTemporaryAssetBundles(unloadAssetBundle, unloadAllLoadedObjects);
                    }
                    operation = new LoadAssetAsyncOperation(request);
                }
                return(operation);
            }
            catch (System.Exception ex)
            {
                Debug.LogErrorFormat("AssetBundleManager.LoadAsset is falid!\n{0}", ex.Message);
            }
            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 加载资源记录文件
        /// </summary>
        void LoadAssetRecord()
        {
#if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
                string[] assetBundleNameArray = UnityEditor.AssetDatabase.GetAllAssetBundleNames();
                for (int index = 0; index < assetBundleNameArray.Length; index++)
                {
                    string[] assetNameArray = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleNameArray[index]);
                    for (int i = 0; i < assetNameArray.Length; i++)
                    {
                        string assetName = assetNameArray[i];
                        if (
                            // 预知件
                            assetName.EndsWith(".prefab")
                            // 二进制文件和 Lua 文件
                            || assetName.EndsWith(".bytes")
                            // 音效文件
                            || assetName.EndsWith(".wav") || assetName.EndsWith(".map") || assetName.EndsWith(".ogg")
                            )
                        {
                            string assetRecordKey = AssetRecordInfo.ToAssetRecordKey(assetNameArray[i]);
                            if (m_AssetRecordDict.ContainsKey(assetRecordKey))
                            {
                                Debug.LogErrorFormat("Some asset with the same assetrecordkey[{0}], please check and fix it! in path:\n{1}\n{2}", assetRecordKey, m_AssetRecordDict[assetRecordKey].RelativePath, assetNameArray[i]);
                            }
                            else
                            {
                                m_AssetRecordDict.Add(assetRecordKey, new AssetRecordInfo(assetRecordKey, assetBundleNameArray[index], assetNameArray[i]));
                            }
                        }
                    }
                }
            }
            else
#endif
            {
                byte[] contentBytes = FileUtility.ReadFileBytes(GetAssetBundleFileFullPath(AppDefine.AssetRecordsFileName));

                if (AppDefine.IsEncrypted)
                {
                    contentBytes = Utility.AESDecrypt(contentBytes, AppDefine.AssetSecretKey);
                }

                string fileContent = Utility.BytesToUTF8String(contentBytes);
                if (string.IsNullOrEmpty(fileContent))
                {
                    Debug.LogError("Load Asset Reocrds file error, please check and fix it!");
                    return;
                }

                string[] assetRecordInfoArray = fileContent.Split(new string[] { "#$" }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < assetRecordInfoArray.Length; index++)
                {
                    string[] recordItems = assetRecordInfoArray[index].Split(new string[] { "#@" }, StringSplitOptions.None);
                    if (recordItems.Length > 1)
                    {
                        string assetRecordKey  = recordItems[0].Trim();
                        string assetBundleName = recordItems[1].Trim();
                        if (m_AssetRecordDict.ContainsKey(assetRecordKey))
                        {
                            Debug.LogErrorFormat("Some asset with the same assetrecordkey[{0}], please check and fix it! in assetBundleName:\n{1}\n{2}", assetRecordKey, m_AssetRecordDict[assetRecordKey].AssetBundleName, assetBundleName);
                        }
                        else
                        {
                            m_AssetRecordDict.Add(assetRecordKey, new AssetRecordInfo(assetRecordKey, assetBundleName));
                        }
                    }
                }
            }
        }