/// <summary> /// 获取资源所在Bundle信息 /// </summary> /// <param name="assetName">资源名或Bundle名</param> /// <returns></returns> public RuntimeBundleInfo GetBundleInfo(string assetName) { RuntimeBundleInfo assetBelongBundleInfo = BundleMapAdapter.GetBundleIndexItemInfo(assetName); if (assetBelongBundleInfo == null) { Debug.LogError($"can't find asset :{assetName} belong bundle"); } return(assetBelongBundleInfo); }
/// <summary> /// 获取资源所属Bundle信息 /// </summary> /// <param name="assetName">Asset名</param> /// <param name="isRegisterAssetIndex">是否注册Asset索引信息</param> /// <returns></returns> public RuntimeBundleInfo GetBundleIndexItemInfo(string assetName, bool isRegisterAssetIndex = true) { int assetNameHash = assetName.GetHashCode(); RuntimeBundleInfo assetBelongBundleItemInfo = null; if (!this.m_cachedRuntimeBundleInfoDict.TryGetValue(assetNameHash, out assetBelongBundleItemInfo)) { BundleIndexItemInfo rawAssetBelongBundleItemInfo = this.m_bundlemapHolder.GetBundleItemInfoByAssetName(assetName); if (rawAssetBelongBundleItemInfo == null) { Debug.LogError($"can't find {assetName} belong bundle"); } else { assetBelongBundleItemInfo = RuntimeBundleInfo.AdapterToRuntimeBundleInfo(rawAssetBelongBundleItemInfo); try { //注册Bundle名,防止多次访问 if (!assetName.EndsWithFast(BUNDLE_POSTFIX)) { this.m_cachedRuntimeBundleInfoDict.Add(assetBelongBundleItemInfo.BundleName.GetHashCode(), assetBelongBundleItemInfo); } this.m_cachedRuntimeBundleInfoDict.Add(assetNameHash, assetBelongBundleItemInfo); if (isRegisterAssetIndex) { foreach (string assetInBundle in assetBelongBundleItemInfo.BundleAssetsArray) { int assetInBundleHash = assetInBundle.GetHashCode(); if (assetInBundleHash != assetNameHash) { this.m_cachedRuntimeBundleInfoDict.Add(assetInBundleHash, assetBelongBundleItemInfo); } } } } catch (ArgumentException ex) { Debug.LogWarning($"dulpicate asset {assetName}"); } } } return(assetBelongBundleItemInfo); }
internal void EnsureDependencies(string name) { RuntimeBundleInfo assetBelongBundleInfo = ResourceMgr.GetBundleInfo(name); if (assetBelongBundleInfo == null) { Debug.LogError($"can't find asset {name} belong bundle"); return; } for (int i = 0; i < assetBelongBundleInfo.BundleDependencyArray.Length; ++i) { string dependencyBundleName = assetBelongBundleInfo.BundleDependencyArray[i]; if (!HasLoaded(dependencyBundleName)) { ResourceModule.Instance.PreloadBundle(dependencyBundleName, null); } } }
public void OnLoadAssetBundle(string name, AssetBundle bundle, bool dependencyResource, LoadPriority priority) { #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(name, 4); #endif RuntimeBundleInfo resourceBelongBundleInfo = ResourceMgr.Instance.GetBundleInfo(name); AssetBundleAssetLoadingInfo info = new AssetBundleAssetLoadingInfo(resourceBelongBundleInfo); info.AssetBundle = bundle; info.Priority = priority; if (!dependencyResource) { EnqueuePendingAssetBunlde(info); } else { BundleInfoResource res = new BundleInfoResource(resourceBelongBundleInfo); res.AssetBundle = info.AssetBundle; cache.CacheAssetBundle(res); InvokeBundleLoaded(info); MarkBundleDependency(res); } }
/// <summary> /// Adapter Raw BundleIndexItemInfo to Runtime /// </summary> /// <param name="rawBundleIndexItemInfo"></param> /// <returns></returns> public static RuntimeBundleInfo AdapterToRuntimeBundleInfo(BundleIndexItemInfo rawBundleIndexItemInfo) { RuntimeBundleInfo runtimeBundleInfo = new RuntimeBundleInfo(rawBundleIndexItemInfo); return(runtimeBundleInfo); }
public BundleInfoResource(RuntimeBundleInfo resourceBelongBundleInfo) { this.m_resourceBelongBundleInfo = resourceBelongBundleInfo; }
internal void Update() { #if DEBUG_BUNDLE_CACHE Profiler.BeginSample("GOEAssetBundleCacheManager.Update"); #endif while (assetLoading.Count < MaximalLoadingAssetBundles && pendingBundle.Count > 0) { var info = pendingBundle.First.Value; pendingBundle.RemoveFirst(); StartLoadAssets(info); } for (int i = 0; i < assetLoading.Count; i++) { AssetBundleAssetLoadingInfo info = assetLoading[i]; if (info.AllDone) { #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 6); #endif BundleInfoResource res = cache[info.BundleName]; bool isNewBundle = false; if (res == null) { RuntimeBundleInfo resourceBelongBundleInfo = ResourceMgr.Instance.GetBundleInfo(info.BundleName); res = new BundleInfoResource(resourceBelongBundleInfo); res.AssetBundle = info.AssetBundle; cache.CacheAssetBundle(res); isNewBundle = true; } else { if (!res.AssetBundle) { res.AssetBundle = info.AssetBundle; } } bool isAsync = info.IsAsync; for (int j = 0; j < info.Requests.Count; j++) { AssetRequestInfo req = info.Requests[j]; for (int k = 0; k < req.Assets.Length; ++k) { UnityEngine.Object obj = req.Assets[k]; string assetName = req.RequestAssetsName[k]; if (!obj) { Debug.LogError($"can't load asset: {assetName}"); continue; } /*if (!assetName.StartsWithFast(obj.name)) * Debug.LogError($"get object not match objname:{obj.name},request name:{assetName}"); * else * {*/ AssetInfo asset = res[assetName]; if (asset == null) { asset = OnGotAsset(assetName, obj, info.BundleName); res.AddAsset(assetName, asset); } if (asset.AssetValid && obj != asset.Asset) { throw new Exception($"{assetName} not valid"); } if (obj) { asset.Asset = obj; } asset.Priority = info.Priority; if (obj) { if (asset.NeedInstance && asset.PoolCount == 0) { if (assetLoadedCallbacks.ContainsKey(assetName)) { EnqueueInstantiateAsset(asset); } } else { InvokeAssetLoaded(asset); } } /*}*/ } } if (isNewBundle) { InvokeBundleLoaded(info); MarkBundleDependency(res); } assetLoading.RemoveAt(i); #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 7); Profiler.EndSample(); #endif break;//一个Update只删除一个已完成Bundle } } if (instantiationQueue.Count > 0) { var asset = instantiationQueue.First.Value; if (!InvokeAssetLoaded(asset)) { instantiationQueue.RemoveFirst(); } } //不在加载资源时GC bool hasLoading = assetLoading.Count > 0 || pendingBundle.Count > 0 || instantiationQueue.Count > 0; if (!hasLoading) { cache.DoGC(); } #if DEBUG_BUNDLE_CACHE Profiler.EndSample(); #endif }