public AssetBundle LoadAB(string name) { ABItem item = null; uint crc = Crc32.GetCrc32(name); if (!abItemDict.TryGetValue(crc, out item)) { AssetBundle ab = null; string fullPath = Application.streamingAssetsPath + $"/{name}"; ab = AssetBundle.LoadFromFile(fullPath); if (ab == null) { throw new Exception($"ab 加载失败:{fullPath}"); } item = abItemPool.Spawn(); item.AB = ab; item.Retain(); abItemDict.Add(crc, item); } else { item.Retain(); } return(item.AB); }
private IEnumerator LoadAssetBundleAsync(string abName, Action <ABItem> successCall, Action failCall = null) { Debug.Log("异步加载AB包: " + abName); // AB包的依赖项 string[] dependence = null; m_ABToDependence.TryGetValue(abName, out dependence); if (dependence != null && dependence.Length > 0) { for (int i = 0; i < dependence.Length; i++) { GameMgr.StartCoroutine(LoadAssetBundleAsync(dependence[i], null)); } } // AB包本身 ABItem abItem = null; m_loadedABs.TryGetValue(abName, out abItem); if (abItem != null) { abItem.Retain(); Debug.Log(string.Format("异步加载AB包完毕: {0} 引用 {1}", abName, abItem.ReferencedCount)); successCall?.Invoke(abItem); } else { string abPath = ABUtility.ABAbsolutePath + abName; // 加载中 if (m_loadingABNames.Contains(abName)) { yield return(m_loadedABs.ContainsKey(abName)); abItem = m_loadedABs[abName]; abItem.Retain(); Debug.Log(string.Format("异步加载AB包完毕: {0} 引用 {1}", abName, abItem.ReferencedCount)); successCall?.Invoke(abItem); } else { // 从头加载 Debug.Log("Path:" + abPath); m_loadingABNames.Add(abName); var abRequest = AssetBundle.LoadFromFileAsync(abPath); yield return(abRequest.isDone); abItem = GameMgr.m_ObjectMgr.Spawn <ABItem>(); abItem.Init(abRequest.assetBundle, abName); m_loadedABs[abName] = abItem; m_loadingABNames.Remove(abName); Debug.Log(string.Format("异步加载AB包完毕: {0} 引用 {1}", abName, abItem.ReferencedCount)); successCall?.Invoke(abItem); } } }
private ABItem LoadAssetBundle(string abName) { Debug.Log("同步加载AB包: " + abName); Assert.IsFalse(m_loadingABNames.Contains(abName), "在异步加载尚未结束时不能再同步加载AB包:" + abName); // AB包的依赖项 string[] dependence = null; m_ABToDependence.TryGetValue(abName, out dependence); if (dependence != null && dependence.Length > 0) { for (int i = 0; i < dependence.Length; i++) { LoadAssetBundle(dependence[i]); } } // AB包本身 ABItem abItem = null; m_loadedABs.TryGetValue(abName, out abItem); if (abItem != null) { abItem.Retain(); } else { string abPath = ABUtility.ABAbsolutePath + abName; Debug.Log("Path:" + abPath); AssetBundle ab = AssetBundle.LoadFromFile(abPath); abItem = GameMgr.m_ObjectMgr.Spawn <ABItem>(); abItem.Init(ab, abName); m_loadedABs[abName] = abItem; } Debug.Log(string.Format("同步加载AB包完毕: {0} 引用 {1}", abName, abItem.ReferencedCount)); return(abItem); }