/// <summary> /// 本地资源加载完成之后处理 /// </summary> public void OnLoaded() { if (this.m_assetType <= EnumAssetType.eAssetType_AssetBundleFont || this.m_assetType == EnumAssetType.eAssetType_Scene) { if (this.m_www != null) { this.m_AssetBunle = this.m_www.assetBundle; this.m_www.Dispose(); this.m_www = null; } } TimeSpan timeSpan = DateTime.Now - this.m_dataBeginLoadTime; try { if (!this.m_bHasCallBacked) { this.m_bHasCallBacked = true; if (this.GetLoadFinishedHandler() != null) { this.GetLoadFinishedHandler()(this); } } } catch (Exception e) { AssetLogger.Fatal(e.ToString()); } }
/// <summary> /// 加载主资源MainAsset /// </summary> public void LoadMainAsset() { try { if (this.IsDone && this.m_AssetBunle != null) //如果assetbundle已经加载完成 { this.m_MainAsset = this.m_AssetBunle.mainAsset; //取得assetbundle的主资源 Debug.Log(this.m_MainAsset.name); /*if (!this.m_MainAsset.name.Contains("Backdrop") && !this.m_MainAsset.name.Contains("Fantasy Atlast") && !this.m_MainAsset.name.Contains("msyha") && !this.m_MainAsset.name.Contains("Fantasy Atlasm") && !this.m_MainAsset.name.Contains("Unlit/Transparent Colored")) * { * GameObject a = (GameObject)UnityEngine.Object.Instantiate(this.m_MainAsset); * } */ } } catch (OutOfMemoryException ex2) { AssetLogger.Fatal(ex2.ToString()); Resources.UnloadUnusedAssets(); GC.Collect(); } catch (Exception ex1) { AssetLogger.Fatal("AssetEx LoadAll Exception: " + ex1.ToString()); } }
public void DetectComplete() { if (!this.m_HasCallBack && this.DetectAllHasFinished()) { if (this.m_assetResource != null) { AssetLogger.Debug("Asset: DetectComplete:" + this.m_assetResource.URL); } else { AssetLogger.Error("Asset: DetectComplete: null == m_assetResource"); } this.m_HasCallBack = true; //设置加载完成 this.DebugError(); //打印出出错的资源 this.LoadMainAsset(); /*Debug.Log("Main:" + this.m_assetResource.MainAsset); * for (int j = 0; j < this.m_depAssetResources.Length; j++) * { * Debug.Log("dep:" + this.m_depAssetResources[j].MainAsset); * } */ try { for (int i = 0; i < this.m_listAssetRequest.Count; i++) { LocalAssetRequest request = this.m_listAssetRequest[i]; request.OnAssetRequestFinishedHandler(this.m_assetResource);//执行资源加载完成之后的委托 } } catch (Exception e) { AssetLogger.Fatal(e.ToString()); } this.UnloadAssetBundle(); } }
/// <summary> /// 开始加载assetbundle,通过本地url从内存中加载 /// </summary> public void BeginDownload() { this.m_dataBeginLoadTime = DateTime.Now; try { AssetLogger.Debug("BeginDownLoad:" + this.URL); this.m_isBeginDownload = true; if (LocalAssetResource.s_hashSetAssetURL.Contains(this.URL))//已经加载过了 { AssetLogger.Error("s_hashSet.Contains(m_strAssetUrl) == true: " + this.URL); this.m_isDone = true; } else { string text = Path.GetFileName(this.URL).ToLower(); //取得要加载的资源名(小写) if (!LocalAssetResource.s_hashSetAssetName.Add(text)) //添加资源名到资源名集合中 { AssetLogger.Error(string.Format("s_hashSetAssetName.Add(strAssetName) == false:{0}", text)); //添加失败 } if (!this.m_url.EndsWith(".unity3d")) { string text2 = this.URL; if (this.URL.IndexOf("file:///") == 0) { text2 = this.URL.Substring(8); } FileInfo fileInfo = new FileInfo(text2); if (fileInfo.Exists) { /*FileStream fileStream = fileInfo.OpenRead();//打开文件流 * Debug.Log("doload"+fileStream.Length); * byte[] array = new byte[fileStream.Length]; * fileStream.Read(array, 0, (int)fileStream.Length);//读取资源 * fileStream.Close(); * this.m_AssetBunle = AssetBundle.CreateFromMemoryImmediate(array);//从内存中创建assetbundleCreateRequest,这个比较占内存,虽然是异步的 */ this.m_AssetBunle = AssetBundle.CreateFromFile(text2); if (!LocalAssetResource.m_dicAllAssetResource.ContainsKey(this.m_url)) { LocalAssetResource.m_dicAllAssetResource.Add(this.m_url, this); if (LocalAssetResource.m_dicAllAssetResource.Count == 200) { AssetLogger.Debug(string.Format("AssetBundle count Exceed 200, it is dangerous!", new object[0])); } } this.m_isDone = true; } else { //不存在文件资源,报错 AssetLogger.Error(string.Format("fileInfo.Exists == false:{0}", text2)); this.m_isDone = true; } } else { //如果平台不是windows,那么就用www来加载资源 AssetLogger.Debug("Scene: BeginDownLoad:" + this.URL); this.m_www = new WWW(this.URL); } LocalAssetResource.s_hashSetAssetURL.Add(this.URL);//加载完成之后,把资源的url添加到url集合中 } } catch (Exception e) { AssetLogger.Fatal(e.ToString()); this.m_isDone = true; } }
public void OnUpdate() { if (!this.UnLoadNotUsedResource() && this.m_allUnLoadFinishedEventHandler != null) { GC.Collect(); Resources.UnloadUnusedAssets(); Action <bool> allUnLoadFinishedEventHandler = this.m_allUnLoadFinishedEventHandler; this.m_allUnLoadFinishedEventHandler = null; allUnLoadFinishedEventHandler(true); } LinkedListNode <LocalAssetResource> next; for (LinkedListNode <LocalAssetResource> linkedListNode = this.m_linkedListAssetResourceInLoading.First; linkedListNode != null; linkedListNode = next) { next = linkedListNode.Next; LocalAssetResource value = linkedListNode.Value; if (value.IsDone)//已经加载完成 { Debug.Log(value.URL + "已经下载过了"); if (!string.IsNullOrEmpty(value.Error))//如果加载没错的话 { this.m_bLoadError = true; AssetLogger.Error(value.Error); } if (this.m_allLoadFinishedEventHandler != null) { this.m_current += value.Size; } this.m_linkedListAssetResourceInLoading.Remove(linkedListNode); value.OnLoaded(); } else //还没有加载 { if (value.Canceled)//如果已经取消就从链表中移除 { Debug.Log("取消下载:" + value.URL); this.m_linkedListAssetResourceInLoading.Remove(linkedListNode); } else { if (!value.Started) { Debug.Log(value.URL + "开始下载"); value.BeginDownload(); } } } } //如果平台是手机的话,就处理1个,不是的话处理5个 int dealCount = Application.isMobilePlatform ? 1 : 10; if (this.m_linkedListNeedToLoad.Count > 0 && this.m_linkedListAssetResourceInLoading.Count < dealCount)//保持一次处理5个asset { Debug.Log(this.m_linkedListNeedToLoad.Count); LocalAssetResource value2 = this.m_linkedListNeedToLoad.First.Value; this.m_linkedListNeedToLoad.RemoveFirst(); Debug.Log("AddLast:" + value2.URL); this.m_linkedListAssetResourceInLoading.AddLast(value2); } //如果没有加载任务了,就表示完成执行完成回调函数 if (this.m_linkedListNeedToLoad.Count == 0 && this.m_linkedListAssetResourceInLoading.Count == 0 && this.m_allLoadFinishedEventHandler != null) { Action <bool> allLoadFinishedEventHandler = this.m_allLoadFinishedEventHandler; this.m_allLoadFinishedEventHandler = null; try { allLoadFinishedEventHandler(!this.m_bLoadError); } catch (Exception e) { AssetLogger.Fatal(e.ToString()); } this.m_total = 0; this.m_current = 0; } }