IEnumerator AsyncCreateABReqScene(string path, byte[] bytes) { var abCreateReq = AssetBundle.LoadFromMemoryAsync(bytes); yield return(abCreateReq); var ab = abCreateReq.assetBundle; if (ab != null) //判断AB中是否有该场景 { var scenePaths = ab.GetAllScenePaths(); foreach (var scenePath in scenePaths) { if (Path.GetFileNameWithoutExtension(scenePath) == path) { UpdaterLog.Log("find scene in assetbundle:" + path); CachedSceneBundles.Add(path, ab); break; } } } bool result = Application.CanStreamedLevelBeLoaded(path); //执行回调 Queue <Action <bool> > onDoneQueue = AsyncLoadingSceneQueue[path]; if (onDoneQueue == null) { UpdaterLog.LogException(new Exception("Internal error , the AsyncLoadingQueue have no queue named :" + path)); yield break; } while (onDoneQueue.Count > 0) { onDoneQueue.Dequeue()(result); } }
IEnumerator AsyncResourcesLoad(string path, Type type) { var req = Resources.LoadAsync(path, type); yield return(req); Object obj = req.asset; Queue <Action <Object> > onDoneQueue = AsyncLoadingQueue[path]; if (onDoneQueue == null) { UpdaterLog.LogException(new Exception("internal error , the AsyncLoadingQueue have no queue named :" + path)); yield break; } while (onDoneQueue.Count > 0) { onDoneQueue.Dequeue()(obj); } this.AsyncLoadingQueue.Remove(path); }
IEnumerator AsyncCreateABReqObj(string path, Type type, byte[] bytes) { var abCreateReq = AssetBundle.LoadFromMemoryAsync(bytes); //异步创建ab yield return(abCreateReq); string objectName = Path.GetFileName(path); var abReq = abCreateReq.assetBundle.LoadAssetAsync(objectName, type); //异步读取物体 yield return(abReq); Object obj = abReq.asset; abCreateReq.assetBundle.Unload(false); //一旦加载完毕,立即释放assetbundle,但不释放bundle中的物体,物体会在场景切换时释放 if (obj == null) { UpdaterLog.LogError(string.Format("the resource {0} load async from ab is null", path)); } else { UpdaterLog.Log(string.Format("load async the resource {0} from ab", path)); if (!CachedObjects.ContainsKey(path)) { CachedObjects.Add(path, obj); } } Queue <Action <Object> > onDoneQueue = AsyncLoadingQueue[path]; if (onDoneQueue == null) { UpdaterLog.LogException(new Exception("internal error , the AsyncLoadingQueue have no queue named :" + path)); yield break; } while (onDoneQueue.Count > 0) { onDoneQueue.Dequeue()(obj); } this.AsyncLoadingQueue.Remove(path); }
IEnumerator AsyncResourcesLoad(string path, Type type) { var req = Resources.LoadAsync(path, type); yield return(req); Object obj = req.asset; // if (obj != null) {//对于Resource.load的对象,由于Unity已经做过缓存,因此不加入缓存中,由Unity处理其缓存和释放。 // CachedObjects.Add(path, obj); // } //执行所有回调 Queue <Action <Object> > onDoneQueue = AsyncLoadingQueue[path]; if (onDoneQueue == null) { UpdaterLog.LogException(new Exception("internal error , the AsyncLoadingQueue have no queue named :" + path)); yield break; } while (onDoneQueue.Count > 0) { onDoneQueue.Dequeue()(obj); } }