//同步读文本 public static string LoadAllTextSync(string textfilename) { //先外包 string outerpath = ResDef.OuterPackageDirectory + textfilename; if (System.IO.File.Exists(outerpath)) { long t1 = Api.GetTickCount(); string text = System.IO.File.ReadAllText(outerpath, System.Text.Encoding.UTF8); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadAllTextSync OK:" + outerpath + ",time=" + dt.ToString() + "ms"); return(text); } //接着以文件系统方式读内包 string innerpath1 = ResDef.InterPackageDirectoryEx + textfilename; if (System.IO.File.Exists(innerpath1)) { long t1 = Api.GetTickCount(); string text = System.IO.File.ReadAllText(innerpath1, System.Text.Encoding.UTF8); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadAllTextSync OK:" + innerpath1 + ",time=" + dt.ToString() + "ms"); return(text); } #if SupportPackageIO //再以包系统方式读内包 string innerpath2 = ResDef.InterPackageDirectory + textfilename; if (PackageIO.Exists(innerpath2)) { return(PackageIO.ReadAllText(innerpath2, System.Text.Encoding.UTF8)); } #endif //Trace.LogError("LoadAllTextSync fail:" + outerpath + "," + innerpath1); //都失败就返回失败 return(""); }
/// <summary> /// 同步加载资源包 /// </summary> /// <param name="packID">资源包名</param> public static AssetBundle LoadResPackageSync(string PackName) { if (!Application.isPlaying) { return(null); } if (PackName.Length <= 0) { return(null); } if (Singleton.CachePackages.Contains(PackName)) { return(Singleton.CachePackages[PackName] as AssetBundle); } if (Singleton.RequestTable.Contains(PackName)) { RequestPackageInfo info = Singleton.RequestTable[PackName] as RequestPackageInfo; if (info.bLoading == true) { return(null); } } string finalpath = ""; string ext = bEncrypt ? ".zen" : ".unity3D"; string outerpath = ResDef.OuterPackageDirectory + PackName + ext; string innerpath1 = ResDef.InterPackageDirectoryEx + PackName + ext; string innerpath2 = ResDef.InterPackageDirectory + PackName + ext; byte[] data = null; long t1 = Api.GetTickCount(); //先读外包 if (System.IO.File.Exists(outerpath)) { data = System.IO.File.ReadAllBytes(outerpath); finalpath = outerpath; } //没有的话先以文件系统方式再读内包 else if (System.IO.File.Exists(innerpath1)) { data = System.IO.File.ReadAllBytes(innerpath1); finalpath = innerpath1; } //再没有再以包系统方式读内包(安卓下内包可能在jar包里需要多次解压) #if SupportPackageIO else if (PackageIO.Exists(innerpath2)) { data = PackageIO.ReadAllBytes(innerpath2); } #endif else { Trace.LogError("LoadResPackageSync fail, the file doesn't exists in paths below:" + outerpath + "," + innerpath1 + "," + innerpath2); } if (bEncrypt) { string key = PackName + "*^$@&%#!"; byte[] outdata = DecryptDES(data, key); if (outdata != null) { data = outdata; } } if (data != null) { AssetBundle pAssetBundle = null; try { #if U462 pAssetBundle = AssetBundle.LoadFromMemory(data); #else string temppath = ResDef.UserPath + "temp.unity3d"; System.IO.File.WriteAllBytes(temppath, data); pAssetBundle = AssetBundle.CreateFromFile(temppath); #endif } catch (UnityException e) { Trace.LogError(e.ToString()); } if (pAssetBundle != null) { Singleton.CachePackages[PackName] = pAssetBundle; Singleton.CacheOrders.Add(PackName); long t2 = Api.GetTickCount(); long dt = t2 - t1; Trace.Log("LoadResPackageSync OK:" + finalpath + ",time=" + dt.ToString() + "ms"); return(pAssetBundle); } else { Trace.LogError("LoadResPackageSync fail, CreateFromMemoryImmediate fail:" + finalpath); } } else { Trace.LogError("LoadResPackageSync fail, ReadAllBytes fail:" + finalpath); } return(null); }
//异步读文本 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; } }