//-------------------------------------------------------------------------------------------- static public Dictionary <string, ResPackItm> ReadResTable(byte[] pBf) { Dictionary <string, ResPackItm> tbl = new Dictionary <string, ResPackItm>(); if (pBf.Length > 0) { string line; MemoryStream mryStm = new MemoryStream(pBf); StreamReader stm = new StreamReader(mryStm); while ((line = stm.ReadLine()) != null) { if (line.Length <= 2) { continue; } line = line.Replace("\n", ""); line = line.Replace("\r", ""); ResPackItm itm = ResPackItm.MakeFrom(line); if (itm != null) { tbl.Add(itm.mFile.ToLower(), itm); } } stm.Close(); mryStm.Close(); } return(tbl); }
//-------------------------------------------------------------------------------------------- // 从包里加载资源 UnityEngine.Object NtfLoadResource(string szfileWithoutResources) { string szfile; if (mResDic != null) { ResPackItm itm = null; szfile = "Extends/" + szfileWithoutResources; if (mResDic.TryGetValue(szfile.ToLower(), out itm)) { if (mPackDic != null) { ResPackge pck = null; if (mPackDic.TryGetValue(itm.mPack, out pck)) { return(pck.LoadObject(szfile)); } } } szfile = "resources/" + szfileWithoutResources; if (mResDic.TryGetValue(szfile.ToLower(), out itm)) { if (mPackDic != null) { ResPackge pck = null; if (mPackDic.TryGetValue(itm.mPack, out pck)) { return(pck.LoadObject(szfile)); } } } } return(null); }
//-------------------------------------------------------------------------------------------- // 生成对应文件的 Object 对象 static public List <Object> BuildObjectList(ref BuildArgs vArgs, BundleVersionControl version, ref bool isNeedPackage) { isNeedPackage = false; string szPckNm = Path.GetFileName(vArgs.vPack); szPckNm = szPckNm.ToLower(); List <Object> objList = new List <Object>(); foreach (string fileName in vArgs.fAry) { string szt = fileName.ToLower(); if (szt.EndsWith(".unity")) { Debug.LogWarning("BuildPipeline Skip file = " + fileName); continue; } if (szt.EndsWith(".unitypackage")) { Debug.LogWarning("BuildPipeline Skip file = " + fileName); continue; } string fl = ArchiveUtil.NtfPathBeginAssets(fileName); fl = fl.Replace('\\', '/'); string szKey = ArchiveUtil.NtfPathAfterAssets(fl).ToLower(); if (version != null && version.UpdateFileMD5(szKey, szPckNm) == false) {//MD5相同,不用打包 } else { isNeedPackage = true; } Object obj = AssetDatabase.LoadMainAssetAtPath(fl); if (null == obj) { Debug.LogWarning("BuildPipeline LOAD failed file = " + fileName); continue; } objList.Add(obj); vArgs.kAry.Add(szKey); ResPackItm itm = new ResPackItm(); itm.mType = 0; // 普通资源 itm.mfVer = 1; //// 需要查找对应版本号 itm.mFile = szKey; itm.mPack = szPckNm; vArgs.vTbl.Add(szKey, itm); } //更新package对应的版本号 if (version != null && isNeedPackage) { version.IncreasePackageVersion(szPckNm); } return(objList); }
//-------------------------------------------------------------------------------------------- static public ResPackItm MakeFrom(string sz) { string[] szAry = sz.Split('|'); if (szAry.Length >= 4) { ResPackItm itm = new ResPackItm(); itm.mType = ResUtil.TryToInt(szAry[0]); itm.mfVer = ResUtil.TryToInt(szAry[1]); itm.mFile = szAry[2]; itm.mPack = szAry[3]; return(itm); } return(null); }
//-------------------------------------------------------------------------------------------- // 从包里加载场景 bool NtfLoadScene(string szScene) { if (mPackDic != null) { ResPackItm itm = null; string szNm = Path.GetFileNameWithoutExtension(szScene).ToLower(); if (mResDic.TryGetValue(szNm + ".unity", out itm)) { ResPackge pck = null; if (mPackDic.TryGetValue(itm.mPack, out pck)) { return(pck.LoadLevel(szNm)); } } } return(false); }
//-------------------------------------------------------------------------------------------- // 把场景文件也加到资源索引表里 static void AppendResPackItm(string[] szAry, string fpck, BuildTarget tgt) { Dictionary <string, ResPackItm> tbl = new Dictionary <string, ResPackItm>(); string szPckNm = Path.GetFileName(fpck); szPckNm = szPckNm.ToLower(); foreach (string sf in szAry) { string szKey = Path.GetFileName(sf).ToLower(); ResPackItm itm = new ResPackItm(); itm.mType = 1; // 场景资源 itm.mfVer = 1; //// 需要查找对应版本号 itm.mFile = szKey; itm.mPack = szPckNm; tbl.Add(szKey, itm); } AssetBundleUtil.WriteResList(tbl, tgt); }
//-------------------------------------------------------------------------------------------- static public Dictionary <string, ResPackItm> ReplaceAndMergeResTable(Dictionary <string, ResPackItm> vNew, Dictionary <string, ResPackItm> vOld) { Dictionary <string, ResPackItm> tbl = new Dictionary <string, ResPackItm>(vOld); foreach (ResPackItm v0 in vNew.Values) { ResPackItm val = null; if (tbl.TryGetValue(v0.mFile.ToLower(), out val)) { //if (v0.mfVer > val.mfVer) { tbl.Remove(v0.mFile.ToLower()); tbl.Add(v0.mFile.ToLower(), v0); } continue; } tbl.Add(v0.mFile.ToLower(), v0); } return(tbl); }
public IEnumerator LoadSceneAsync(string sceneName, ResPackge.AsyncLoadData data, Action callback = null) { bool isLoaded = false; string szNm = Path.GetFileNameWithoutExtension(sceneName).ToLower(); if (mPackDic != null) { ResPackItm itm = null; if (mResDic.TryGetValue(szNm + ".unity", out itm)) { ResPackge pck = null; if (mPackDic.TryGetValue(itm.mPack, out pck)) { IEnumerator e = pck.LoadPackageAsync(data); while (true) { e.MoveNext(); if (data.IsFinish) { break; } yield return(e.Current); } isLoaded = pck.LoadLevel(sceneName); AddDelayCleanPackage(pck); } } } data.IsFinish = true; if (!isLoaded) // 也许没有打包或用Unity打包 { string unitySceneName = Path.GetFileNameWithoutExtension(sceneName); Application.LoadLevel(unitySceneName.ToLower()); } if (callback != null) { callback(); } }
//-------------------------------------------------------------------------------------------- // szfile要求: Assets/ 之后的完整路径(含扩展名), 示例参考顶部说明 public IEnumerator LoadResourceAsync(string szfileWithoutResources, ResPackge.AsyncLoadData data) { string szfile = "resources/" + szfileWithoutResources; ResPackge pck = null; bool isInPackage = false; if (mResDic != null) { ResPackItm itm = null; if (mResDic.TryGetValue(szfile.ToLower(), out itm)) { isInPackage = true; if (mPackDic != null) { if (mPackDic.TryGetValue(itm.mPack, out pck)) { } } } if (pck == null) { szfile = "Extends/" + szfileWithoutResources; itm = null; if (mResDic.TryGetValue(szfile.ToLower(), out itm)) { isInPackage = true; if (mPackDic != null) { if (mPackDic.TryGetValue(itm.mPack, out pck)) { } } } } } if (pck != null) { UpdateDelayCleanPackageTime(pck); IEnumerator e = pck.LoadObjectAsync(szfile, mEntry, data); bool interrupted = false; data.AsyncCount++; while (!interrupted) { e.MoveNext(); if (data.IsFinish) { break; } yield return(e.Current); } data.FinishCount++; } ////////////////////////////////////////////////////////////////////////// //Simulate #if (UNITY_EDITOR) // 编辑模式下 if (GameResMng.ForcePackage && DisableOtherResource) { IEnumerator eSimulate = SimulateWait(); data.AsyncCount++; int testcount = 0; while (true) { eSimulate.MoveNext(); testcount++; if (testcount > 1) { break; } yield return(eSimulate.Current); } data.FinishCount++; data.IsFinish = true; } #endif ////////////////////////////////////////////////////////////////////////// #if (UNITY_EDITOR) // 编辑模式下 if (null == data.m_res && !DisableOtherResource && !ForcePackage) { data.m_res = AssetDatabase.LoadMainAssetAtPath("Assets/Extends/" + szfileWithoutResources); } if (null == data.m_res) { data.m_res = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/" + szfileWithoutResources); } #endif if (data.m_res == null) // 容错: 也许没有打包或用Unity打包 { string szf = szfileWithoutResources; // 影射到 Resources/ 目录下 int npos = szf.LastIndexOf('.'); // 去掉扩展名 if (npos > 0) { szf = szf.Substring(0, npos); } #if (UNITY_EDITOR) // 编辑模式下 if (!DisableOtherResource) { data.m_res = Resources.Load("Extends/" + szf); } #else data.m_res = Resources.Load("Extends/" + szf); #endif if (null == data.m_res) { data.m_res = Resources.Load(szf); } } data.IsFinish = true; if (null == data.m_res) { // string msg = ""; if (!isInPackage) { Debug.LogWarning("Load Resource Failed:" + szfile); } else { Debug.LogWarning("[Package]Load Resource Failed:" + szfile); } } }