internal void AddBundle(string name, string asset = null) { foreach (string res in mListRes) { if (res == name) { return; } } if (name.Length == 0) { Logger.GetFile(LogFile.Res).LogError(" ResList:AddBundle name can't empty"); return; } GOEBundle bundle = ResourceMgr.Instance().GetBundle(name); if (bundle == null) { UnityEngine.Debug.LogError("Cannot find bundle " + name); } mListRes.Add(name); if (asset == null) { asset = bundle.Files.ElementAt(bundle.Files.Count - 1); } mListAsset.Add(asset); foreach (var i in bundle.DependsOn) { GOEBundle dep = ResourceMgr.Instance().GetBundle(i); mListRes.Add(i); mListAsset.Add(dep.FirstAsset); } }
public void GetAssetInCache(string name, Action <string, UnityEngine.Object> callback) { var bName = ResourceMgr.Instance().GetBundleName(name); GOEBundleResource b; if (cachedBundles.TryGetValue(bName, out b)) { var asset = b[name]; b.Touch(); if (asset != null) { asset.GetAsset(callback); } else { if (addedAssets.TryGetValue(name, out asset)) { asset.GetAsset(callback); } } } else { GOEAsset asset; if (addedAssets.TryGetValue(name, out asset)) { asset.GetAsset(callback); } } }
internal void Update() { #if DEBUG_BUNDLE_CACHE Profiler.BeginSample("GOEAssetBundleCacheManager.Update"); #endif while (assetLoading.Count < MaximalLoadingAssetBundles && pendingBundle.Count > 0) { var info = pendingBundle.Dequeue(); 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 GOEBundleResource res = new GOEBundleResource(); res.AssetBundle = info.AssetBundle; res.BundleInfo = ResourceMgr.Instance().GetBundle(info.BundleName); cache.CacheAssetBundle(res); bool isAsync = info.IsAsync; for (int j = 0; j < info.Requests.Count; j++) { AssetRequestInfo req = info.Requests[j]; UnityEngine.Object obj = isAsync ? req.Request.asset : req.Asset; if (obj) { string assetName = req.AssetName; var asset = OnGotAsset(assetName, obj, info.BundleName); res.AddAsset(assetName, asset); InvokeAssetLoaded(asset); } } InvokeBundleLoaded(info); MarkBundleDependency(res); assetLoading.RemoveAt(i); #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 7); Profiler.EndSample(); #endif return;//一个Update只删除一个已完成Bundle } } //不在加载资源时GC bool hasLoading = assetLoading.Count > 0 || pendingBundle.Count > 0; if (!hasLoading) { cache.DoGC(); } #if DEBUG_BUNDLE_CACHE Profiler.EndSample(); #endif }
public override void GetScene(string name, Action callback, LoadPriority priority = LoadPriority.Default) { if (ResourceMgr.Instance().GetBundleName(name) == string.Empty) { ResourceMgr.Instance().RegisterBundleIdx(name, name + EngineFileUtil.m_sceneExt); } base.GetScene(name, callback); }
public static ResourceMgr Instance() { if (resourceMgr == null) { resourceMgr = new ResourceMgr(); } return(resourceMgr); }
internal void AddAsset(string name) { string bundleName = ResourceMgr.Instance().GetBundleName(name); if (bundleName != string.Empty) { this.AddBundle(bundleName, name); } }
protected override Resource CreateResource(string name, LoadPriority priority) { Resource res = base.CreateResource(name, priority); #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(name, 1); #endif res.AddGotBundleCallback(this.OnLoadAssetBundle); return(res); }
public bool AppendRead(string stream) { StringReader sr = new StringReader(stream); string line = sr.ReadLine(); while (null != line) { if (line.StartsWith(BUNDLE_INFO_PREFIX)) { byte[] buf = Convert.FromBase64String(line.Substring(BUNDLE_INFO_PREFIX.Length + 1)); MemoryStream ms = new MemoryStream(buf); BinaryReader br = new BinaryReader(ms); int bundleCnt = br.ReadInt32(); for (int i = 0; i < bundleCnt; i++) { string name = br.ReadString(); int size = br.ReadInt32(); int assetCnt = br.ReadInt32(); HashSet <string> assets = new HashSet <string>(); string firstAsset = null; for (int j = 0; j < assetCnt; j++) { string assetName = br.ReadString(); if (firstAsset == null) { firstAsset = assetName; } ResourceMgr.Instance().RegisterBundleIdx(assetName, name, size); } GOEBundle bundle = ResourceMgr.Instance().GetBundle(name); bundle.FirstAsset = firstAsset; int dependCnt = br.ReadInt32(); for (int j = 0; j < dependCnt; j++) { string depName = br.ReadString(); bundle.DependsOn.Add(depName); } } } else { GOEBundleMapItem bmi = new GOEBundleMapItem(); bmi.FromString(line); if (!_bundleMap.ContainsKey(bmi.Name)) { _bundleMap.Add(bmi.Name, bmi); } } line = sr.ReadLine(); } return(true); }
public void LoadRes() { if (mResLoader.State == LoaderState.None) { if (!DependencyResource) { mResLoader.IsRawResource = true; } mResLoader.Name = mName; ResourceMgr.Instance().GOELoaderMgr.AddLoader(mResLoader); } }
public bool HasLoadedAsset(string asset) { var bName = ResourceMgr.Instance().GetBundleName(asset); GOEBundleResource b; if (cachedBundles.TryGetValue(bName, out b)) { return(b.ContainsAsset(asset)); } else { return(addedAssets.ContainsKey(asset)); } }
internal void GetAsset(string name, Action <string, UnityEngine.Object> callback, LoadPriority priority = LoadPriority.Default) { if (name == null || name == string.Empty) { return; } if (HasLoaded(name)) { if (callback != null) { GetAssetInCache(name, callback); } return; } #if UNITY_EDITOR UnityEngine.Object obj = LoadFromPrefab(name, typeof(UnityEngine.Object)); if (obj != null) { SetAsset(name, obj).Reference = 1; if (callback != null) { callback(name, GOEAsset.InstanceAsset(obj, name)); } return; } #endif string bundleName = ResourceMgr.GetBundleName(name); if (bundleName == string.Empty) { Logger.GetFile(LogFile.Res).LogError("can not find asset: " + name); return; } if (!cacheMgr.HasLoadingInfo(bundleName)) { Resource res = this.GetDownloadResource(bundleName); if (res == null) { res = this.CreateResource(bundleName, priority); res.LoadRes(); } //逻辑加载时,提高优先级// if (res.Loader.Priority < priority) { this.ResourceMgr.GOELoaderMgr.SetLoaderPriority(res.Loader, priority); } } cacheMgr.AddCallbackToDic(name, callback); }
public static GOEBundleMapItem GetFinalBundle(string name, bool fromWeb = false) { GOEBundleMapItem item = null; if (!fromWeb) { item = ResourceMgr.Instance().BundleRename.GetBundleItemFromOriginalName(name); } if (null == item) { item = new GOEBundleMapItem(); item.FinalName = name; } return(item); }
public bool ReleaseAssetReference(string name, UnityEngine.Object obj) { var bName = ResourceMgr.Instance().GetBundleName(name); GOEBundleResource b; if (cachedBundles.TryGetValue(bName, out b)) { var asset = b[name]; if (asset != null) { asset.ReleaseAssetReference(obj); return(true); } } return(false); }
internal void GetScene(string name, Action callBack, LoadPriority priority = LoadPriority.Default) { if (GOERoot.IsEditor || HasLoaded(name) || ResourceMgr.IsBundleCached(name + ".bundle")) { if (mCurScene != name) { LoadScene(name); if (callBack != null) { callBack(); } return; } else { removeBundle(name); } } string bundleName = this.ResourceMgr.GetBundleName(name); if (string.IsNullOrEmpty(bundleName)) { bundleName = this.ResourceMgr.GetBundleName(name + ".unity"); } if (bundleName == string.Empty) { Logger.GetFile(LogFile.Res).LogError("can not find scene: " + name); return; } mCurScene = name; Resource res = this.GetDownloadResource(bundleName); if (res == null) { res = this.CreateResource(bundleName, priority); res.LoadRes(); } //逻辑加载时,提高优先级// if (res.Loader.Priority < priority) { this.ResourceMgr.GOELoaderMgr.SetLoaderPriority(res.Loader, priority); } res.AddGotSceneCallback(callBack); }
public void BeginLoad() { if (mWWWType == GOEngine.WWWType.AssetBundle) { loadScale = 0.9f; } mRealName = mName; #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(mName, 2); #endif GOEBundleMapItem item = GetFinalBundle(mName, IsFromWeb); mRealName = item.FinalName; string name = mRealName; loadWWW = WWWUtil.CreateWWW(name, IsRawResource, IsFromStream, IsFromWeb); mBeginLoadTime = 0; }
void BeginPreload() { GOEBundle bundle = ResourceMgr.Instance().GetBundle(mName); pendingDependencies = null; if (bundle != null) { foreach (var dep in bundle.DependsOn) { if (pendingDependencies == null) { pendingDependencies = new HashSet <string>(); } pendingDependencies.Add(dep); GOERoot.ResMgr.PreloadBundle(dep, OnPreloadBundle, Priority, true); } } }
public void Update() { if (mState == MultiLoaderState.None) { BeginPreload(); NextState(); } else if (mState == MultiLoaderState.Preload) { if (pendingDependencies == null || pendingDependencies.Count == 0) { BeginLoad(); NextState(); } } if (mState == MultiLoaderState.Load) { if (IsAllDone()) { #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(mName, 3); #endif NextState(); } float pro = Progress; if (pro != mLastProgress) { mLastProgress = pro; mBeginLoadTime = 0; } else { mBeginLoadTime += Time.deltaTime; } if (mBeginLoadTime >= MaxLoadTime) { mState = MultiLoaderState.None; Logger.GetFile(LogFile.Res).LogError("dead link: " + mName); } } }
public void SampleLoadEvent(string bn, int eventID) { GOEAssetBundleProfileInfo pi = null; float time = Time.realtimeSinceStartup; if (info.TryGetValue(bn, out pi)) { switch (eventID) { case 2: pi.WWWStartTime = time; break; case 3: pi.WWWFinishTime = time; break; case 4: pi.ResourceReadyTime = time; break; case 5: pi.AssetLoadStartTime = time; break; case 6: pi.AssetLoadFinishTime = time; break; case 7: pi.EndTime = time; break; } } else if (eventID == 1) { pi = new GOEAssetBundleProfileInfo(); pi.BundleName = bn; GOEBundle bundle = ResourceMgr.Instance().GetBundle(bn); pi.BundleSize = bundle.Size; pi.StartTime = time; info[bn] = pi; } }
protected override void OnLoadAssetBundle(string name, AssetBundle www, bool dependencyResource) { if (www == null) { Logger.GetFile(LogFile.Res).LogError(name + " assetBundel == null"); return; } string sceneName = GetSceneName(name); cachedBundles.Add(www); cachedNames.Add(sceneName); GOEBundle bundle = ResourceMgr.GetBundle(name); ResourceMgr.CacheSceneBundleDependency(bundle); if (sceneName == mCurScene) { this.LoadScene(sceneName); } }
protected virtual Resource CreateResource(string name, LoadPriority priority) { Resource res = new Resource(); res.Name = name; res.Loader.Priority = priority; mDownResource.Add(res); GOEBundle bundle = ResourceMgr.GetBundle(name); if (bundle != null) { res.Size = bundle.Size; } res.AddGotWWWCallback(this.OnLoadWWW); return(res); }
internal ResourceState PreloadBundle(string bundleName, Action <string, AssetBundle> callback, LoadPriority priority = LoadPriority.Default, bool dependencyResource = false) { GOEBundle bundle = ResourceMgr.Instance().GetBundle(bundleName); if (cacheMgr.Cache[bundleName] != null) { if (callback != null) { callback(bundleName, null); } return(ResourceState.OK); } Resource res = this.GetDownloadResource(bundleName); if (res == null) { if (!cacheMgr.HasLoadingInfo(bundleName)) { res = this.CreateResource(bundleName, priority); res.DependencyResource = dependencyResource; res.LoadRes(); } cacheMgr.AddCallbackToDic(bundleName, callback); } else if (res.ResOK) { if (callback != null) { callback(bundleName, res.Loader.AssetBundle); } return(ResourceState.OK); } else { cacheMgr.AddCallbackToDic(bundleName, callback); if (res.Loader.Priority < priority) { this.ResourceMgr.GOELoaderMgr.SetLoaderPriority(res.Loader, priority); } } return(ResourceState.Wait); }
internal override void Start() { foreach (string name in mListRes) { GOEBundle bundle = ResourceMgr.Instance().GetBundle(name); if (bundle == null) { Logger.GetFile(LogFile.Res).LogError(name + " not find in FireBundle"); } else { mTotalSize += bundle.Size; } progressMap[name] = 0; } if (mListRes.Count == 0) { Logger.GetFile(LogFile.Res).LogError(" mListRes.Count == 0"); } }
public void OnLoadAssetBundle(string name, AssetBundle bundle, bool dependencyResource) { #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(name, 4); #endif AssetBundleAssetLoadingInfo info = new AssetBundleAssetLoadingInfo(); info.AssetBundle = bundle; info.BundleName = name; if (!dependencyResource) { pendingBundle.Enqueue(info); } else { GOEBundleResource res = new GOEBundleResource(); res.AssetBundle = info.AssetBundle; res.BundleInfo = ResourceMgr.Instance().GetBundle(info.BundleName); cache.CacheAssetBundle(res); InvokeBundleLoaded(info); MarkBundleDependency(res); } }
void StartLoadAssets(AssetBundleAssetLoadingInfo info) { try { GOEBundle gBundle = ResourceMgr.Instance().GetBundle(info.BundleName); var bundle = info.AssetBundle; if (!bundle) { Debug.LogError(string.Format("AssetBundle {0} is null", gBundle.mName)); return; } info.Requests = new List <AssetRequestInfo>(); bool shouldAsync = true; info.IsAsync = shouldAsync; #if DEBUG_BUNDLE_CACHE ResourceMgr.Instance().SampleLoadEvent(info.BundleName, 5); #endif foreach (string str in gBundle.Files) { AssetRequestInfo ar = new AssetRequestInfo(); ar.AssetName = str; if (shouldAsync) { ar.Request = bundle.LoadAssetAsync(str); } else { ar.Asset = bundle.LoadAsset(str); } info.Requests.Add(ar); } assetLoading.Add(info); } catch { Debug.LogError("Cannot load assetbundle:" + info.BundleName); } }
internal void EnsureDependencies(string name) { string bundleName = ResourceMgr.GetBundleName(name); if (!string.IsNullOrEmpty(bundleName)) { GOEBundle bundle = ResourceMgr.GetBundle(bundleName); if (bundle != null) { foreach (var dep in bundle.DependsOn) { GOEBundle depBundle = ResourceMgr.GetBundle(dep); if (depBundle == null) { continue; } if (!HasLoaded(depBundle.FirstAsset)) { GOERoot.ResMgr.PreloadBundle(dep, null); } } } } }
internal virtual void Start() { resourceMgr = ResourceMgr.Instance(); resourceMgr.Start(); }
internal override void Update() { base.Update(); if (mListRes.Count == 0) { return; } if (mDone) { Action endf = OnEnd; Clear(); if (endf != null) { endf(); } } int downloadsize = 0; foreach (string name in mListRes) { GOEBundle bundle = ResourceMgr.Instance().GetBundle(name); if (bundle == null) { continue; } if (this.IsBundleCached(name)) { downloadsize += bundle.Size; UpdateProgress(name, 1f); } else { Resource res = this.GetRes(name); if (res != null) { float prog = res.Loader.Progress; UpdateProgress(name, prog); downloadsize += (int)((prog) * bundle.Size); } else { float prog; if (progressMap.TryGetValue(name, out prog)) { downloadsize += (int)((prog) * bundle.Size); } } } } if (mTotalSize == 0) { mProgerss = 0; mDone = true; } else if (downloadsize == mTotalSize) { mProgerss = 1.0f; mDone = true; } else { mProgerss = downloadsize * 1.0f / mTotalSize; } if (ProgressHandler != null) { ProgressHandler(mProgerss); } }
private bool IsBundleCached(string name) { return(ResourceMgr.Instance().IsBundleCached(name)); }
protected Resource GetRes(string name) { return(ResourceMgr.Instance().GetDownloadResource(name)); }
/// <summary> /// 尝试进行垃圾回收 /// </summary> /// <param name="suppress">是否忽视最佳缓存尺寸释放所有可释放资源</param> public void DoGC(bool suppress = false, bool force = false) { #if DEBUG_BUNDLE_CACHE Profiler.BeginSample("GOEAssetBundleCache.DoGC"); #endif if (needGC && ResourceMgr.Instance().GOELoaderMgr.IsFree) { bool shouldDoGC = cachedSize > preferedCacheSize || suppress; if (Time.realtimeSinceStartup - lastGCTime > minimalGCInterval) { #if DEBUG_BUNDLE_CACHE int oldCacheSize = cachedSize; int oldCacheCount = CachedBundleCount; #endif bool needUnloadUnused = false; List <GOEBundleResource> bundleCanRelease = null; do { bundleCanRelease = null; foreach (var i in cachedBundles) { var bundle = i.Value; /*#if DEBUG_BUNDLE_CACHE * bool hasBundle = bundle.AssetBundle; #endif*/ bundle.ReleaseAssetBundle(); /*#if DEBUG_BUNDLE_CACHE * if (hasBundle && !bundle.AssetBundle) * Debug.LogWarning(string.Format("{2:0.##}:Bundle {0} released webstream, new CacheSize={1:0.##}MB", bundle.Name, cachedSize / 1024f / 1024f, Time.realtimeSinceStartup)); #endif*/ if (shouldDoGC && bundle.CanRelease) { if (bundleCanRelease == null) { bundleCanRelease = new List <GOEBundleResource>(); } bundleCanRelease.Add(bundle); } } if (!shouldDoGC) { needGC = false; #if DEBUG_BUNDLE_CACHE Profiler.EndSample(); #endif return; } if (bundleCanRelease != null) { if (bundleCanRelease.Count > 0) { bundleCanRelease.Sort((a, b) => Mathf.CeilToInt((a.HitTime - b.HitTime) * 10)); HashSet <string> bundleRemoved = new HashSet <string>(); for (int i = 0; i < bundleCanRelease.Count; i++) { var bundle = bundleCanRelease[i]; bundle.Release(false); if (bundle.Released) { needUnloadUnused = true; cachedSize -= bundle.Size; /*#if DEBUG_BUNDLE_CACHE * Debug.LogWarningFormat("{2:0.##}:Bundle {0} released, new CacheSize={1:0.##}MB", bundle.Name, cachedSize / 1024f / 1024f, Time.realtimeSinceStartup); #endif*/ bundleRemoved.Add(bundle.Name); } if (cachedSize < preferedCacheSize * 0.8f && !force) { bundleCanRelease = null; break; } } foreach (var i in bundleRemoved) { cachedBundles.Remove(i); } //GC.Collect(); } } }while (bundleCanRelease != null && bundleCanRelease.Count > 0); if (needUnloadUnused) { #if DEBUG_BUNDLE_CACHE Profiler.BeginSample("Resources.UnloadUnusedAssets"); #endif Resources.UnloadUnusedAssets(); #if DEBUG_BUNDLE_CACHE Profiler.EndSample(); #endif } needGC = false; lastGCTime = Time.realtimeSinceStartup; #if DEBUG_BUNDLE_CACHE Debug.LogFormat("{0:0.##}:Cache GC, released {1} bundles, freed {2:0.##} MB", Time.realtimeSinceStartup, oldCacheCount - CachedBundleCount, (oldCacheSize - cachedSize) / 1024f / 1024f); #endif } } #if DEBUG_BUNDLE_CACHE Profiler.EndSample(); #endif }