public IEnumerator PreLoadLua() { #if UNITY_EDITOR if (AssetBundleConfig.IsEditorMode) { yield break; } #endif string luaAssetbundleName = XLuaManager.Instance.AssetbundleName; var abloader = LoadAssetBundleAsync(luaAssetbundleName); yield return(abloader); luaCaching.Clear(); AssetBundle curAssetbundle = abloader.assetbundle; var allAssetNames = assetsPathMapping.GetAllAssetNames(luaAssetbundleName); for (int i = 0; i < allAssetNames.Count; i++) { var assetName = allAssetNames[i]; if (luaCaching.ContainsKey(assetName)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); var asset = curAssetbundle == null ? null : curAssetbundle.LoadAsset(assetPath); luaCaching[assetName] = asset; } abloader.Dispose(); }
private IEnumerator LoadLuaData(string luaAssetbundleName) { var abloader = LoadAssetBundleAsync(luaAssetbundleName); yield return(abloader); AssetBundle curAssetbundle = abloader.assetbundle; #if UNITY_EDITOR Logger.Log("当前内存占用 name = " + luaAssetbundleName + " " + EditorUtility.FormatBytes(UnityEngine.Profiling.Profiler.GetRuntimeMemorySizeLong(curAssetbundle))); #endif var allAssetNames = assetsPathMapping.GetAllAssetNames(luaAssetbundleName); long total = 0; for (int i = 0; i < allAssetNames.Count; i++) { var assetName = allAssetNames[i]; if (luaCaching.ContainsKey(assetName)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); var asset = curAssetbundle == null ? null : curAssetbundle.LoadAsset(assetPath); total += UnityEngine.Profiling.Profiler.GetRuntimeMemorySizeLong(asset); luaCaching[assetName] = asset; } #if UNITY_EDITOR Logger.Log("解析后内存占用 " + EditorUtility.FormatBytes(total)); #endif abloader.Dispose(); }
public BaseAssetAsyncLoader LoadAssetAsync(string assetPath, System.Type assetType) { #if UNITY_EDITOR if (AssetBundleConfig.IsEditorMode) { string path = AssetBundleUtility.PackagePathToAssetsPath(assetPath); UnityEngine.Object target = AssetDatabase.LoadAssetAtPath(path, assetType); return new EditorAssetAsyncLoader(target); } #endif string assetbundleName = null; string assetName = null; bool status = MapAssetPath(assetPath, out assetbundleName, out assetName); if (!status) { Logger.LogError("No assetbundle at asset path :" + assetPath); return null; } var loader = AssetAsyncLoader.Get(); prosessingAssetAsyncLoader.Add(loader); if (IsAssetLoaded(assetName)) { Logger.LogError(">>>> IsAssetLoaded {0}", assetName); loader.Init(assetName, GetAssetCache(assetName)); return loader; } var assetbundleLoader = LoadAssetBundleAsync(assetbundleName); loader.Init(assetName, assetbundleLoader); return loader; }
public void AddAssetbundleAssetsCache(string assetbundleName, string postfix = null) { #if UNITY_EDITOR if (AssetBundleConfig.IsEditorMode) { return; } #endif if (!IsAssetBundleLoaded(assetbundleName)) { Logger.LogError("Try to add assets cache from unloaded assetbundle : " + assetbundleName); return; } var curAssetbundle = GetAssetBundleCache(assetbundleName); var allAssetNames = assetsPathMapping.GetAllAssetNames(assetbundleName); for (int i = 0; i < allAssetNames.Count; i++) { var assetName = allAssetNames[i]; if (IsAssetLoaded(assetName)) { continue; } if (!string.IsNullOrEmpty(postfix) && !assetName.EndsWith(postfix)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); var asset = curAssetbundle == null ? null : curAssetbundle.LoadAsset(assetPath); AddAssetCache(assetName, asset); #if UNITY_EDITOR // 说明:在Editor模拟时,Shader要重新指定 var go = asset as GameObject; if (go != null) { var renderers = go.GetComponentsInChildren<Renderer>(); for (int j = 0; j < renderers.Length; j++) { var mat = renderers[j].sharedMaterial; if (mat == null) { continue; } var shader = mat.shader; if (shader != null) { var shaderName = shader.name; mat.shader = Shader.Find(shaderName); } } } #endif } }
public static void BuildVariantMapping(AssetBundleManifest manifest) { mappingList.Clear(); string outputFilePath = AssetBundleUtility.PackagePathToAssetsPath(AssetBundleConfig.VariantsMapFileName); string[] allVariants = manifest.GetAllAssetBundlesWithVariant(); // 处理带variants的assetbundle foreach (string assetbundle in allVariants) { // 该assetbundle中包含的所有asset的路径(相对于Assets文件夹),如: // Assets/AssetsPackage/UI/Prefabs/Language/[Chinese]/TestVariant.prefab // Assets/AssetsPackage/UI/Prefabs/Language/[English]/TestVariant.prefab // 在代码使用的加载路径中,它们被统一处理为 // Assets/AssetsPackage/UI/Prefabs/Language/[Variant]/TestVariant.prefab // 这里的variant为chinese、english,在AssetBundleManager中设置启用的variant会自动对路径进行正确还原 string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetbundle); if (assetPaths == null || assetPaths.Length == 0) { UnityEngine.Debug.LogError("Empty assetbundle with variant : " + assetbundle); continue; } // 自本节点向上找到Assetbundle所在 AssetBundleImporter assetbundleImporter = AssetBundleImporter.GetAtPath(assetPaths[0]); while (assetbundleImporter != null && string.IsNullOrEmpty(assetbundleImporter.assetBundleVariant)) { assetbundleImporter = assetbundleImporter.GetParent(); } if (assetbundleImporter == null || string.IsNullOrEmpty(assetbundleImporter.assetBundleVariant)) { UnityEngine.Debug.LogError("Can not find assetbundle with variant : " + assetbundle); continue; } string assetbundlePath = assetbundleImporter.assetPath; if (assetbundlePath.EndsWith("/")) { assetbundlePath = assetbundlePath.Substring(0, assetbundlePath.Length - 1); } // 由于各个Variant的内部结构必须完全一致,而Load时也必须完全填写,所以这里不需要关注到assetbundle具体的每个资源 string nowNode = System.IO.Path.GetFileName(assetbundlePath); string mappingItem = string.Format("{0}{1}{2}", assetbundle, PATTREN, nowNode); mappingList.Add(mappingItem); } mappingList.Sort(); if (!GameUtility.SafeWriteAllLines(outputFilePath, mappingList.ToArray())) { Debug.LogError("BuildVariantMapping failed!!! try rebuild it again!"); } else { AssetDatabase.Refresh(); AssetBundleEditorHelper.CreateAssetbundleForCurrent(outputFilePath); Debug.Log("BuildVariantMapping success..."); } AssetDatabase.Refresh(); }
public AssetBundleChecker(AssetBundleCheckerConfig config) { this.config = config; assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath); importer = AssetBundleImporter.GetAtPath(assetsPath); if (importer == null || !importer.IsValid) { Logger.LogError($"定义的Package信息path:({assetsPath}) 没有找不到,请对比Asset/Editor/AssetBundle/xxx和Asset/AssetsPackage/xxx"); } }
public AssetBundleDispatcher(AssetBundleDispatcherConfig config) { this.config = config; assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath); importer = AssetBundleImporter.GetAtPath(assetsPath); if (importer == null) { Debug.LogError("Asset path err : " + assetsPath); } }
public void AddAssetbundleAssetsCache(string assetbundleName) { #if UNITY_EDITOR // if (AssetBundleConfig.IsEditorMode) // { // return; // } #endif if (!IsAssetBundleLoaded(assetbundleName)) { Logger.LogError("Try to add assets cache from unloaded assetbundle : " + assetbundleName); return; } var curAssetbundle = GetAssetBundleCache(assetbundleName); if (curAssetbundle.isStreamedSceneAssetBundle) { // LoadScene(assetbundleName, curAssetbundle); return; } var allAssetNames = assetsPathMapping.GetAllAssetNames(assetbundleName); if (allAssetNames.Count == 0) { allAssetNames = idpAssetsPathMapping.GetAllAssetNames(assetbundleName); } for (int i = 0; i < allAssetNames.Count; i++) { var assetName = allAssetNames[i]; if (IsAssetLoaded(assetName)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); UnityEngine.Object asset = null; asset = curAssetbundle == null ? null : curAssetbundle.LoadAsset(assetPath); AddAssetCache(assetName, asset); var go = asset as GameObject; if (go != null) { #if UNITY_EDITOR ResetEditorShader(go); // 说明:在Editor模拟时,Shader要重新指定 #endif //多语言转换 TranslateAsset(go); ResetPostProcess(go); } } }
public void CheckChannelName() { string channelAssetPath = Path.Combine(AssetBundleConfig.ChannelFolderName, config.PackagePath); channelAssetPath = AssetBundleUtility.PackagePathToAssetsPath(channelAssetPath) + ".bytes"; if (!File.Exists(channelAssetPath)) { GameUtility.SafeWriteAllText(channelAssetPath, "None"); AssetDatabase.Refresh(); } var imp = AssetBundleImporter.GetAtPath(channelAssetPath); imp.assetBundleName = assetsPath; }
/// <summary> /// 写入地址 /// </summary> /// <param name="outFile"></param> /// <param name="outLines"></param> public static void WriteAssetsAddress() { string assetsaddress = AssetBundleUtility.PackagePathToAssetsPath(AssetBundleConfig.AssetsaddressConfig); if (!GameUtility.SafeWriteAllLines(assetsaddress, addressList.ToArray())) { Debug.LogError("BuildPathMapping failed!!! try rebuild it again!"); } else { AssetDatabase.Refresh(); AssetBundleEditorHelper.CreateAssetbundleForCurrent(assetsaddress); Debug.Log("BuildPathMapping success..."); } AssetDatabase.Refresh(); }
public BaseAssetAsyncLoader LoadAssetAsync(string assetPath, System.Type assetType) { #if UNITY_EDITOR if (AssetBundleConfig.IsEditorMode) { string path = AssetBundleUtility.PackagePathToAssetsPath(assetPath); KVDebugHelper.Instance.AddAssetInfo(assetPath, path); if (File.Exists(path)) { UnityEngine.Object target = AssetDatabase.LoadAssetAtPath(path, assetType); return(new EditorAssetAsyncLoader(target)); } } #endif string assetbundleName = null; string assetName = null; MapAssetPath(assetPath, out assetbundleName, out assetName); KVDebugHelper.Instance.AddAssetInfo(assetPath, assetbundleName); if (assetPath.IndexOf("spine") >= 0) { Debug.LogError($"不应该加载这个资源 assetPath:{assetPath}"); } if (assetName == null) { Debug.LogError($"没有这个资源 assetPath:{assetPath}"); return(null); } var loader = AssetAsyncLoader.Get(); prosessingAssetAsyncLoader.Add(loader); if (IsAssetLoaded(assetName)) { //Logger.LogError(">>>> IsAssetLoaded {0}", assetName); loader.Init(assetName, GetAssetCache(assetName)); return(loader); } var assetbundleLoader = LoadAssetBundleAsync(assetbundleName, assetPath); loader.Init(assetName, assetbundleLoader); return(loader); }
public override void Update() { if (isDone) { return; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(AssetName); if (this.isAsync) { //异步LoadAssetAsync if (assetbundleLoader.isDone) { if (assetBundleRequest == null) { assetBundleRequest = assetbundleLoader.assetbundle.LoadAssetAsync(assetPath, AssetType); } else { isOver = assetBundleRequest.isDone; if (isOver) { asset = assetBundleRequest.asset; assetbundleLoader.Dispose(); } } } } else { //同步LoadAsset isOver = assetbundleLoader.isDone; if (isOver) { asset = assetbundleLoader.assetbundle.LoadAsset(assetPath, AssetType); assetbundleLoader.Dispose(); } } }
/// <summary> /// 加载bundle内的asset对象 /// </summary> /// <param name="strBundleName"></param> private void LoadObj( ) { /// 主包的 { var assetNameList = AssetBundleManager.Instance.GetAssetNameList(_resRef.res.name); for (int n = 0; n < assetNameList.Count; n++) { var assetName = assetNameList[n]; if (AssetBundleManager.Instance.IsAssetLoaded(assetName)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); UnityEngine.Object obj = _resRef.res.Load(_resRef.res.assetbundle, assetPath); if (obj != null) { AssetBundleManager.Instance.AddAssetCache(assetName, obj); } #if UNITY_EDITOR ReBindShader(obj); #endif } bool _isResident = AssetBundleManager.Instance.IsAssetBundleResident(_resRef.res.name); if (assetNameList != null && assetNameList.Count == 1 && _resRef.res.assetbundle != null && !_isResident) { _resRef.res.assetbundle.Unload(false); _resRef.res.assetbundle = null; } } /// 依赖包的 for (int i = 0, ci = _resRefs.Count; i < ci; ++i) { var r = _resRefs[i]; var assetNameList = AssetBundleManager.Instance.GetAssetNameList(r.res.name); for (int n = 0; n < assetNameList.Count; n++) { var assetName = assetNameList[n]; if (AssetBundleManager.Instance.IsAssetLoaded(assetName)) { continue; } var assetPath = AssetBundleUtility.PackagePathToAssetsPath(assetName); UnityEngine.Object obj = r.res.Load(r.res.assetbundle, assetPath); if (obj != null) { AssetBundleManager.Instance.AddAssetCache(assetName, obj); } #if UNITY_EDITOR ReBindShader(obj); #endif } bool _isResident = AssetBundleManager.Instance.IsAssetBundleResident(_resRef.res.name); if (assetNameList != null && assetNameList.Count == 1 && _resRef.res.assetbundle != null && !_isResident) { r.res.assetbundle.Unload(false); r.res.assetbundle = null; } } }
public AssetBundleChecker(AssetBundleCheckerConfig config) { this.config = config; assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath); importer = AssetBundleImporter.GetAtPath(assetsPath); }
public static void BuildPathMapping(AssetBundleManifest manifest) { mappingList.Clear(); string outputFilePath = AssetBundleUtility.PackagePathToAssetsPath(AssetBundleConfig.AssetsPathMapFileName); string[] allAssetbundles = manifest.GetAllAssetBundles(); string[] allVariants = manifest.GetAllAssetBundlesWithVariant(); List <string> assetbundlesWithoutVariant = null; List <string> variantWithoutDeplicate = null; ProsessVariant(allAssetbundles, allVariants, out assetbundlesWithoutVariant, out variantWithoutDeplicate); // 处理所有不带variants的assetbundle foreach (string assetbundle in assetbundlesWithoutVariant) { // 该assetbundle中包含的所有asset的路径(相对于Assets文件夹),如: // Assets/AssetsPackage/UI/Prefabs/View/UILoading.prefab string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetbundle); foreach (string assetPath in assetPaths) { string packagePath = AssetBundleUtility.AssetsPathToPackagePath(assetPath); if (!addressList.Contains(packagePath)) { addressList.Add(packagePath); } string mappingItem = string.Format("{0}{1}{2}", assetbundle, PATTREN, packagePath); mappingList.Add(mappingItem); } } // 处理带variants的assetbundle(已经去重) // string variant = "[" + AssetBundleConfig.VariantMapParttren + "]"; foreach (string assetbundle in variantWithoutDeplicate) { // 该assetbundle中包含的所有asset的路径(相对于Assets文件夹),如: // Assets/AssetsPackage/UI/Prefabs/Language/[Chinese]/TestVariant.prefab // Assets/AssetsPackage/UI/Prefabs/Language/[English]/TestVariant.prefab // 由于已经去重,以上条目有且仅有一条出现 string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetbundle); if (assetPaths == null || assetPaths.Length == 0) { UnityEngine.Debug.LogError("Empty assetbundle with variant : " + assetbundle); continue; } // 自本节点向上找到Assetbundle所在 AssetBundleImporter assetbundleImporter = AssetBundleImporter.GetAtPath(assetPaths[0]); while (assetbundleImporter != null && string.IsNullOrEmpty(assetbundleImporter.assetBundleVariant)) { assetbundleImporter = assetbundleImporter.GetParent(); } if (assetbundleImporter == null || string.IsNullOrEmpty(assetbundleImporter.assetBundleVariant)) { UnityEngine.Debug.LogError("Can not find assetbundle with variant : " + assetbundle); continue; } string assetbundlePath = assetbundleImporter.assetPath; if (assetbundlePath.EndsWith("/")) { assetbundlePath = assetbundlePath.Substring(0, assetbundlePath.Length - 1); } // 将拿掉[Variant]目录名如: // Assets/AssetsPackage/UI/Prefabs/Language/TestVariant.prefab // 用此种方式可以统一路径,使加载Assetbundle时的路径与具体激活的variant无关 string nowRoot = GameUtility.FormatToUnityPath(System.IO.Path.GetDirectoryName(assetbundlePath)); foreach (string assetPath in assetPaths) { string nowAsset = assetPath.Replace(assetbundlePath, ""); string nowAssetPath = nowRoot + nowAsset; string packagePath = AssetBundleUtility.AssetsPathToPackagePath(nowAssetPath); if (!addressList.Contains(packagePath)) { addressList.Add(packagePath); } string mappingItem = string.Format("{0}{1}{2}", RemoveVariantSuffix(assetbundle), PATTREN, packagePath); mappingList.Add(mappingItem); } } mappingList.Sort(); addressList.Sort(); if (!GameUtility.SafeWriteAllLines(outputFilePath, mappingList.ToArray())) { Debug.LogError("BuildPathMapping failed!!! try rebuild it again!"); } else { AssetDatabase.Refresh(); AssetBundleEditorHelper.CreateAssetbundleForCurrent(outputFilePath); Debug.Log("BuildPathMapping success..."); } AssetDatabase.Refresh(); }
public static void OnCreateServerSkillTime() { //遍历文件夹 string path = AssetBundleUtility.PackagePathToAssetsPath("Lua"); string AssetbundleName = AssetBundleUtility.AssetBundlePathToAssetBundleName(path); XLua.LuaEnv luaEnv = new XLua.LuaEnv(); luaEnv.AddLoader(CustomLoader); luaEnv.AddBuildin("pb", XLua.LuaDLL.Lua.LoadPb); luaEnv.AddBuildin("cjson", XLua.LuaDLL.Lua.LoadRapidJson); string cmd = string.Format("require('{0}/Editor/AssetBundle/SkillTimeline')", Application.dataPath); luaEnv.DoString(cmd); XLua.LuaFunction func = luaEnv.Global.Get <XLua.LuaFunction>("ParseSkillTime"); object[] obj = func.Call(); if (obj != null && obj.Length > 0) { string serverPath = string.Format("{0}/{1}.txt", Application.dataPath.Substring(0, Application.dataPath.Length - 7), "normal_timeline"); if (File.Exists(serverPath)) { File.Delete(serverPath); } FileStream fs = new FileStream(serverPath, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write("id" + "\t" + "roleId" + "\t" + "key" + "\t" + "time"); sw.WriteLine(); sw.Write("int" + "\t" + "int" + "\t" + "string" + "\t" + "float"); sw.WriteLine(); XLua.LuaTable tempTable = obj[0] as XLua.LuaTable; int index = 1; tempTable.ForEach <int, XLua.LuaTable>(delegate(int a, XLua.LuaTable luaTable) { sw.Write(index + "\t" + luaTable.Get <int>("roldId") + "\t" + luaTable.Get <string>("key") + "\t" + luaTable.Get <float>("time")); sw.WriteLine(); index++; }); sw.Close(); fs.Close(); } func = luaEnv.Global.Get <XLua.LuaFunction>("ParseCSkillTime"); obj = func.Call(); if (obj != null && obj.Length > 0) { string serverPath = string.Format("{0}/{1}.txt", Application.dataPath.Substring(0, Application.dataPath.Length - 7), "cc_timeline"); if (File.Exists(serverPath)) { File.Delete(serverPath); } FileStream fs = new FileStream(serverPath, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.Write("id" + "\t" + "key" + "\t" + "time"); sw.WriteLine(); sw.Write("int" + "\t" + "string" + "\t" + "float"); sw.WriteLine(); XLua.LuaTable tempTable = obj[0] as XLua.LuaTable; int index = 1; tempTable.ForEach <int, XLua.LuaTable>(delegate(int a, XLua.LuaTable luaTable) { sw.Write(index + "\t" + luaTable.Get <string>("key") + "\t" + luaTable.Get <float>("time")); sw.WriteLine(); index++; }); sw.Close(); fs.Close(); } }
public AssetsPathMapping() { AssetName = AssetBundleUtility.PackagePathToAssetsPath(AssetBundleConfig.AssetsPathMapFileName); AssetbundleName = AssetBundleUtility.AssetBundlePathToAssetBundleName(AssetName); }
public AddressableDispatcher(AddressableDispatcherConfig config) { this.config = config; assetsPath = AssetBundleUtility.PackagePathToAssetsPath(config.PackagePath); }