| | |
| | | using System.Collections; |
| | | using System.Collections.Generic; |
| | | using UnityEngine; |
| | | |
| | | using Cysharp.Threading.Tasks; |
| | | |
| | | public class AssetBundleUtility : SingletonMonobehaviour<AssetBundleUtility> |
| | | { |
| | |
| | | assetBundle = null; |
| | | } |
| | | |
| | | public IEnumerator Initialize() |
| | | public async UniTask Initialize() |
| | | { |
| | | yield return StartCoroutine(Co_LoadMainfestFile("audio")); |
| | | yield return StartCoroutine(Co_LoadMainfestFile("video")); |
| | | yield return StartCoroutine(Co_LoadMainfestFile("mobeffectshader")); |
| | | yield return StartCoroutine(Co_LoadMainfestFile("config")); |
| | | yield return StartCoroutine(Co_LoadMainfestFile("maps")); |
| | | yield return StartCoroutine(Co_LoadMainfestFile("ui")); |
| | | await Co_LoadMainfestFile("audio"); |
| | | // await Co_LoadMainfestFile("video"); |
| | | await Co_LoadMainfestFile("mobeffectshader"); |
| | | // await Co_LoadMainfestFile("config"); |
| | | await Co_LoadMainfestFile("maps"); |
| | | await Co_LoadMainfestFile("ui"); |
| | | |
| | | yield return StartCoroutine(Co_LoadAssetBundle(ResourcesPath.windowFileBundleName)); |
| | | yield return StartCoroutine(Co_LoadAssetBundle(ResourcesPath.uiprefabFileBundleName)); |
| | | await Co_LoadAssetBundle(ResourcesPath.windowFileBundleName); |
| | | await Co_LoadAssetBundle(ResourcesPath.uiprefabFileBundleName); |
| | | |
| | | initializedUIAssetBundle = true; |
| | | initialized = true; |
| | | } |
| | | |
| | | private IEnumerator Co_LoadMainfestFile(string _category) |
| | | private async UniTask Co_LoadMainfestFile(string _category) |
| | | { |
| | | var path = AssetVersionUtility.GetAssetFilePath(StringUtility.Contact(AssetVersionUtility.EncodeFileName(_category), "_assetbundle"), false); |
| | | var _assetBundle = AssetBundle.LoadFromFile(path); |
| | |
| | | if (_assetBundle == null) |
| | | { |
| | | Debug.LogErrorFormat("AssetBundleManifest的包文件为空或者加载出错. Path:{0}", path); |
| | | yield break; |
| | | return; |
| | | } |
| | | |
| | | AssetBundleManifest _assetBundleManifest = _assetBundle.LoadAsset<AssetBundleManifest>(ResourcesPath.AssetDependentFileAssetName); |
| | | if (_assetBundleManifest == null) |
| | | { |
| | | Debug.LogErrorFormat("AssetBundleManifest的包文件为空或者加载出错. Path:{0}", path); |
| | | yield break; |
| | | return; |
| | | } |
| | | |
| | | string[] _assetBundleNames = _assetBundleManifest.GetAllAssetBundles(); |
| | |
| | | return; |
| | | } |
| | | |
| | | StartCoroutine(Co_DoLoadAsset(assetBundleName, assetName, callBack)); |
| | | Co_DoLoadAsset(assetBundleName, assetName, callBack).Forget(); |
| | | } |
| | | |
| | | private IEnumerator Co_LoadAssetBundle(string assetBundleName) |
| | | private async UniTask<AssetBundle> Co_LoadAssetBundle(string assetBundleName) |
| | | { |
| | | #if UNITY_5||UNITY_5_3_OR_NEWER |
| | | assetBundleName = assetBundleName.ToLower(); |
| | |
| | | |
| | | if (JudgeExistAssetBundle(assetBundleName)) |
| | | { |
| | | yield break; |
| | | return m_AssetBundleDict[assetBundleName]; |
| | | } |
| | | |
| | | if (m_LoadingAssetBundleList.Contains(assetBundleName)) |
| | |
| | | while (!m_AssetBundleDict.ContainsKey(assetBundleName)) |
| | | { |
| | | // Debug.Log(Time.frameCount + " ] 正在加载AssetBundle: " + assetBundleName + ", 请等待..."); |
| | | yield return null; |
| | | await UniTask.Yield(); |
| | | } |
| | | yield break; |
| | | return m_AssetBundleDict[assetBundleName]; |
| | | } |
| | | |
| | | m_LoadingAssetBundleList.Add(assetBundleName); |
| | |
| | | if (_assetBundleInfo == null) |
| | | { |
| | | Debug.LogErrorFormat("Co_LoadAssetBundle(): {0}出现错误 => 不存在AssetBundleInfo. ", assetBundleName); |
| | | yield break; |
| | | m_LoadingAssetBundleList.Remove(assetBundleName); |
| | | return null; |
| | | } |
| | | |
| | | if (_assetBundleInfo.dependentBundles.Length > 0) |
| | | { |
| | | yield return Co_LoadAssetBundleDependenice(_assetBundleInfo); |
| | | await Co_LoadAssetBundleDependenice(_assetBundleInfo); |
| | | } |
| | | |
| | | var isBuiltin = assetBundleName.Contains("builtin"); |
| | |
| | | Debug.LogFormat("Co_LoadAssetBundle(): 将要加载的assetBundle包路径 => {0}", filePath); |
| | | |
| | | var _request = AssetBundle.LoadFromFileAsync(filePath); |
| | | while (!_request.isDone) |
| | | { |
| | | yield return null; |
| | | } |
| | | await _request; |
| | | |
| | | CacheAssetBundle(assetBundleName, _request.assetBundle); |
| | | |
| | | m_LoadingAssetBundleList.Remove(assetBundleName); |
| | | return _request.assetBundle; |
| | | } |
| | | |
| | | private IEnumerator Co_LoadAssetBundleDependenice(AssetBundleInfo assetBundleInfo) |
| | | private async UniTask Co_LoadAssetBundleDependenice(AssetBundleInfo assetBundleInfo) |
| | | { |
| | | AssetBundle _assetBundle = null; |
| | | |
| | | if (assetBundleInfo.dependentBundles == null |
| | | || assetBundleInfo.dependentBundles.Length == 0) |
| | | { |
| | | yield break; |
| | | return; |
| | | } |
| | | |
| | | for (int i = 0; i < assetBundleInfo.dependentBundles.Length; ++i) |
| | | { |
| | | |
| | | if (m_AssetBundleDict.TryGetValue(assetBundleInfo.dependentBundles[i], out _assetBundle) == false) |
| | | { |
| | | yield return Co_LoadAssetBundle(assetBundleInfo.dependentBundles[i]); |
| | | await Co_LoadAssetBundle(assetBundleInfo.dependentBundles[i]); |
| | | } |
| | | else |
| | | { |
| | | if (_assetBundle == null) |
| | | { |
| | | yield return Co_LoadAssetBundle(assetBundleInfo.dependentBundles[i]); |
| | | await Co_LoadAssetBundle(assetBundleInfo.dependentBundles[i]); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private IEnumerator Co_DoLoadAsset(string assetBundleName, string assetName, Action<bool, UnityEngine.Object> callBack = null) |
| | | private async UniTask Co_DoLoadAsset(string assetBundleName, string assetName, Action<bool, UnityEngine.Object> callBack = null) |
| | | { |
| | | #if UNITY_5||UNITY_5_3_OR_NEWER |
| | | assetBundleName = assetBundleName.ToLower(); |
| | |
| | | RunTimeABLoadLog.AddLog(assetBundleName, assetName, UnityEngine.SceneManagement.SceneManager.GetActiveScene().name); |
| | | #endif |
| | | |
| | | yield return Co_LoadAssetBundle(assetBundleName); |
| | | await Co_LoadAssetBundle(assetBundleName); |
| | | |
| | | if (!m_AssetBundleDict.ContainsKey(assetBundleName)) |
| | | { |
| | |
| | | { |
| | | callBack(false, null); |
| | | } |
| | | yield break; |
| | | return; |
| | | } |
| | | |
| | | string _checkTag = assetBundleName + "@" + assetName; |
| | |
| | | || !m_AssetDict[assetBundleName].ContainsKey(assetName)) |
| | | { |
| | | // Debug.Log(Time.frameCount + " ] 正在加载Asset: " + _checkTag + ", 请等待..."); |
| | | yield return null; |
| | | await UniTask.Yield(); |
| | | } |
| | | |
| | | if (callBack != null) |
| | |
| | | callBack(true, m_AssetDict[assetBundleName][assetName]); |
| | | } |
| | | |
| | | yield break; |
| | | return; |
| | | } |
| | | |
| | | m_LoadingAssetList.Add(_checkTag); |
| | | |
| | | var request = m_AssetBundleDict[assetBundleName].LoadAssetAsync(assetName); |
| | | while (!request.isDone) |
| | | { |
| | | yield return null; |
| | | } |
| | | await request; |
| | | |
| | | if (request.asset != null) |
| | | { |
| | |
| | | m_LoadingAssetList.Remove(_checkTag); |
| | | } |
| | | |
| | | private IEnumerator Co_DoLoadAsset(AssetInfo assetInfo, Action<bool, UnityEngine.Object> callBack = null) |
| | | private async UniTask Co_DoLoadAsset(AssetInfo assetInfo, Action<bool, UnityEngine.Object> callBack = null) |
| | | { |
| | | if (assetInfo == null) |
| | | { |
| | | Debug.LogErrorFormat("Co_DoLoadAsset(): {0}, 出现错误 => 存入的AssetInfo为null. ", assetInfo); |
| | | yield break; |
| | | return; |
| | | } |
| | | yield return Co_DoLoadAsset(assetInfo.assetBundleName, assetInfo.name, callBack); |
| | | await Co_DoLoadAsset(assetInfo.assetBundleName, assetInfo.name, callBack); |
| | | } |
| | | |
| | | #endregion |
| | |
| | | |
| | | var isBuiltin = assetBundleName.Contains("builtin"); |
| | | string _path = isBuiltin ? AssetVersionUtility.GetBuiltInAssetFilePath(assetBundleName) : AssetVersionUtility.GetAssetFilePath(assetBundleName); |
| | | Debug.LogFormat("Sync_LoadAssetBundle(): {0} => {1}", assetBundleName, _path); |
| | | |
| | | AssetBundle _assetBundle = AssetBundle.LoadFromFile(_path); |
| | | |
| | | CacheAssetBundle(assetBundleName, _assetBundle); |