private void InvokeAssetCallback(UnityEngine.Object obj, string resPath, IResourceLoad iLoad) { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = obj; ret.sErrorMsg = ""; ret.sPath = resPath; //finally try to load from default resource package if (ret.resObj == null) { ret.resObj = Resources.Load(resPath); } if (ret.resObj != null) { if (iLoad != null) { iLoad.ResourceLoadSucc(ret); } } else { Trace.LogError("LoadFromAssetBundleAsyn fail, respath = " + resPath); if (iLoad != null) { iLoad.ResourceLoadFail(ret); } } }
public void ResourceLoadSucc(ResLoadResult result) { Trace.Log("AsynGameObject::PreLoadFromPath OK, path = " + result.sPath); if (!AsynGameObject.CachePrefabs.Contains(result.sPath)) { AsynGameObject.CachePrefabs[result.sPath] = result.resObj; } }
public void ResourceLoadSucc(ResLoadResult result) { if (result.resObj != null && result.sPath.Length > 0) { if (CacheShaders.ContainsKey(result.sPath) == false) { CacheShaders[result.sPath] = result.resObj; } } }
public static void LoadResPackageAsyn(string PackName, IResourceLoad iLoad, bool PureAsyn = true) { if (!Application.isPlaying) { return; } if (PackName.Length <= 0) { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.sErrorMsg = ""; ret.sPath = PackName; Singleton.InvokePackageCallback(ret, PackName, iLoad); return; } if (Singleton.CachePackages.Contains(PackName)) { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = Singleton.CachePackages[PackName] as AssetBundle; ret.sErrorMsg = ""; ret.sPath = PackName; Singleton.InvokePackageCallback(ret, PackName, iLoad); return; } if (Singleton.RequestTable.Contains(PackName)) { RequestPackageInfo info = Singleton.RequestTable[PackName] as RequestPackageInfo; if (info.bLoading == true) { return; } } if (PureAsyn) { Singleton.StartCoroutine(Singleton.LoadResPackageAsynEnumerator(PackName, iLoad)); } else { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.sErrorMsg = ""; ret.sPath = PackName; LoadResPackageSync(PackName); Singleton.InvokePackageCallback(ret, PackName, iLoad); } }
private void InvokePackageCallback(ResLoadResult ret, string PackName, IResourceLoad iLoad) { bool bSucc = CachePackages.Contains(PackName); if (bSucc) { if (iLoad != null) { iLoad.ResourceLoadSucc(ret); } } else { if (iLoad != null) { iLoad.ResourceLoadFail(ret); } } if (bSucc) { AssetBundle assetbundle = CachePackages[PackName] as AssetBundle; if (RequestTable.Contains(PackName)) { RequestPackageInfo packinfo = RequestTable[PackName] as RequestPackageInfo; List <RequestAssetInfo> RequestList = packinfo.RequestAssets; for (int i = 0; i < RequestList.Count; i++) { RequestAssetInfo info = RequestList[i]; info.obj = null; if (assetbundle.Contains(info.resPath)) { info.obj = assetbundle.LoadAsset(info.resPath); } if (info.obj == null) { string resShort = System.IO.Path.GetFileNameWithoutExtension(info.resPath); if (assetbundle.Contains(resShort)) { info.obj = assetbundle.LoadAsset(resShort); } } RequestList[i] = info; } foreach (RequestAssetInfo info in RequestList) { InvokeAssetCallback(info.obj, info.resPath, info.iLoad); } RequestTable.Remove(PackName); } } }
public void ResourceLoadSucc(ResLoadResult result) { Trace.Log("LoadAsynGameObject OK, path = " + result.sPath); if (!CachePrefabs.Contains(result.sPath)) { CachePrefabs[result.sPath] = result.resObj; } Perform(result.resObj as GameObject); DestroyImmediate(this); }
/// <summary> /// 通用异步加载 /// </summary> /// <param name="resPath">资源相对路径(不带文件格式)</param> /// <param name="iLoad">资源加载回调接口</param> public static void LoadAssetAsyn(string resPath, IResourceLoad iLoad, bool PureAsyn = true) { if (resPath.Length <= 0) { return; } if (!Application.isPlaying) { Debug.LogError("LoadAssetAsyn(resPath) can not use in edit mode"); return; } //PureAsyn = true; if (PureAsyn) { Singleton.LoadFromAssetBundleAsynImp(resPath, iLoad); } else { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = Singleton.LoadFromAssetBundleSyncImp(resPath); ret.sErrorMsg = ""; ret.sPath = resPath; if (ret.resObj != null) { if (iLoad != null) { iLoad.ResourceLoadSucc(ret); } } else { if (iLoad != null) { iLoad.ResourceLoadFail(ret); } } } }
// 资源加载失败回调函数 public void ResourceLoadFail(ResLoadResult result) { Trace.Error("AsynGameObject::PreLoadFromPath Fail, error = " + result.sErrorMsg + ",path = " + result.sPath); }
// 资源加载失败回调函数 public void ResourceLoadFail(ResLoadResult result) { Trace.Error("LoadAsynGameObject Fail, error = " + result.sErrorMsg + ",path = " + result.sPath); DestroyImmediate(this); }
IEnumerator LoadResPackageAsynEnumerator(string PackName, IResourceLoad iLoad) { if (RequestTable.Contains(PackName)) { RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo; if (info.bLoading == true) { yield break; } } Resources.UnloadUnusedAssets(); Caching.CleanCache(); ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.sErrorMsg = ""; ret.sPath = PackName; if (RequestTable.Contains(PackName)) { RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo; info.bLoading = true; RequestTable[PackName] = info; } long t1 = Api.GetTickCount(); string ext = bEncrypt ? ".zen" : ".unity3D"; string outerpath = ResDef.OuterPackageDirectory + PackName + ext; string innerpath1 = ResDef.InterPackageDirectoryEx + PackName + ext; string innerpath2 = ResDef.InterPackageDirectory + PackName + ext; string finalpath = ""; WWW www = null; AssetBundleCreateRequest request = null; //外包优先 if (System.IO.File.Exists(outerpath)) { finalpath = outerpath; //if (!bEncrypt &&(Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)) //对于外包,如果没加密则直接采用读文件方式 if (false) { byte[] data = System.IO.File.ReadAllBytes(outerpath); if (data != null) { request = AssetBundle.LoadFromMemoryAsync(data); while (!request.isDone) { yield return(new WaitForEndOfFrame()); } } data = null; System.GC.Collect(); yield return(new WaitForEndOfFrame()); } else { www = WWW.LoadFromCacheOrDownload("file:///" + outerpath, 5); //www = new WWW("file:///" + outerpath); yield return(www); } } //内包由于有可能在jar/zip等压缩包内,只能通过www组件去提取 else if (System.IO.File.Exists(innerpath1)) { finalpath = innerpath1; www = new WWW("file:///" + innerpath1); yield return(www); } else { finalpath = innerpath2; yield return(true); } if (RequestTable.Contains(PackName)) { RequestPackageInfo info = RequestTable[PackName] as RequestPackageInfo; info.bLoading = false; RequestTable[PackName] = info; } AssetBundle pAssetBundle = null; if (www != null) { if (www.error == null) { if (!CachePackages.Contains(PackName)) { try { if (bEncrypt) { string key = PackName + "*^$@&%#!"; byte[] outdata = DecryptDES(www.bytes, key); if (outdata != null) { #if U462 pAssetBundle = AssetBundle.LoadFromMemory(outdata); #else string temppath = ResDef.UserPath + "temp.unity3d"; System.IO.File.WriteAllBytes(temppath, outdata); pAssetBundle = AssetBundle.CreateFromFile(temppath); #endif } } else { pAssetBundle = www.assetBundle; } } catch (UnityException e) { Trace.LogError(e.ToString()); } if (pAssetBundle != null) { CachePackages[PackName] = pAssetBundle; CacheOrders.Add(PackName); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadResPackageAsynEnumerator OK, www.assetBundle OK:" + finalpath + ",time=" + dt.ToString() + "ms"); } else { Trace.LogError("LoadResPackageAsynEnumerator fail, www.assetBundle fail:" + finalpath); } } } else { Trace.LogError("LoadResPackageAsynEnumerator fail, www.errpr fail:" + www.error); } www.Dispose(); www = null; } else if (request != null) { if (request.isDone) { if (!CachePackages.Contains(PackName)) { try { pAssetBundle = request.assetBundle; } catch (UnityException e) { Trace.LogError(e.ToString()); } if (pAssetBundle != null) { CachePackages[PackName] = pAssetBundle; CacheOrders.Add(PackName); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadResPackageAsynEnumerator OK, www.assetBundle OK:" + finalpath + ",time=" + dt.ToString() + "ms"); } else { Trace.LogError("LoadResPackageAsynEnumerator fail, www.assetBundle fail:" + finalpath); } } } else { Trace.LogError("LoadResPackageAsynEnumerator fail, www.errpr fail:" + www.error); } request = null; } else { Trace.LogError("LoadResPackageAsynEnumerator fail, www null,package doesn't exists:" + finalpath); } System.GC.Collect(); yield return(new WaitForEndOfFrame()); InvokePackageCallback(ret, PackName, iLoad); }
//异步读文本 public static void LoadAllTextAsyn(string textfilename, IResourceLoad iLoad) { if (textfilename.Length <= 0) { return; } //先外包 string outerpath = ResDef.OuterPackageDirectory + textfilename; if (System.IO.File.Exists(outerpath)) { long t1 = Api.GetTickCount(); string text = System.IO.File.ReadAllText(outerpath); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadAllTextAsyn OK:" + outerpath + ",time=" + dt.ToString() + "ms"); ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.resText = text; ret.sErrorMsg = ""; ret.sPath = outerpath; iLoad.ResourceLoadSucc(ret); return; } //接着以文件系统方式读内包 string innerpath1 = ResDef.InterPackageDirectoryEx + textfilename; if (System.IO.File.Exists(innerpath1)) { long t1 = Api.GetTickCount(); string text = System.IO.File.ReadAllText(innerpath1); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadAllTextAsyn OK:" + innerpath1 + ",time=" + dt.ToString() + "ms"); ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.resText = text; ret.sErrorMsg = ""; ret.sPath = innerpath1; iLoad.ResourceLoadSucc(ret); return; } #if SupportPackageIO //再以包系统方式读内包 string innerpath2 = ResDef.InterPackageDirectory + textfilename; if (PackageIO.Exists(innerpath2)) { string text = PackageIO.ReadAllText(innerpath2); ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.resText = text; ret.sErrorMsg = ""; ret.sPath = innerpath2; iLoad.ResourceLoadSucc(ret); } #endif //Trace.LogError("LoadAllTextSync fail:" + outerpath + "," + innerpath1); //都失败就返回失败 { ResLoadResult ret = new ResLoadResult(); ret.fProgress = 1.0f; ret.resObj = null; ret.resText = ""; ret.sErrorMsg = ""; ret.sPath = outerpath; iLoad.ResourceLoadFail(ret); return; } }
// 资源加载失败回调函数 public void ResourceLoadFail(ResLoadResult result) { }