public static void doExportLua(string[] childrens) { EditorUtils.CheckstreamingAssetsPath(); string info = "luac"; string title = "build lua"; EditorUtility.DisplayProgressBar(title, info, 0); var checkChildrens = AssetDatabase.GetAllAssetPaths().Where(p => (p.StartsWith("Assets/Lua") || p.StartsWith("Assets/Config")) && (p.EndsWith(".lua")) ).ToArray(); string path = "Assets/Lua/"; //lua path string path1 = "Assets/Config/"; //config path string root = CurrentRootFolder; //Application.dataPath.Replace("Assets", ""); string crypName = "", crypEditorName = "", fileName = "", outfilePath = "", arg = ""; System.Text.StringBuilder sb = new System.Text.StringBuilder(); //refresh directory if (checkChildrens.Length == childrens.Length) { EditorUtils.DirectoryDelete(OutLuaPath); } EditorUtils.CheckDirectory(OutLuaPath); float allLen = childrens.Length; float i = 0; Debug.Log("luajit32Path:" + luajit32Path); Debug.Log("luajit64Path:" + luajit64Path); Debug.Log("luacPath:" + luacPath); string OutLuaBytesPath = EditorUtils.GetLuaBytesResourcesPath(); string luabytesParentPath = OutLuaBytesPath.Substring(0, OutLuaBytesPath.LastIndexOf("/")); string streamingAssetsPath = Path.Combine(CurrentRootFolder, OutLuaBytesPath); //Path.Combine(CurrentRootFolder, LuaTmpPath); EditorUtils.DirectoryDelete(luabytesParentPath); EditorUtils.CheckDirectory(luabytesParentPath); EditorUtils.CheckDirectory(streamingAssetsPath); Debug.Log(streamingAssetsPath); List <System.Diagnostics.Process> listPc = new List <System.Diagnostics.Process>(); List <string> luabytesAssets32 = new List <string>(); List <string> luabytesAssets64 = new List <string>(); foreach (string file in childrens) { string filePath = Path.Combine(root, file); fileName = CUtils.GetAssetName(filePath); crypName = file.Replace(path, "").Replace(path1, "").Replace(".lua", ".bytes").Replace("\\", "+").Replace("/", "+"); crypEditorName = file.Replace(path, "").Replace(path1, "").Replace(".lua", "." + Common.ASSETBUNDLE_SUFFIX).Replace("\\", "+").Replace("/", "+"); if (!string.IsNullOrEmpty(luajit32Path))// luajit32 { string override_name = CUtils.GetRightFileName(crypName); string override_lua = streamingAssetsPath + "/" + override_name; arg = "-b " + filePath + " " + override_lua; //for jit // Debug.Log(arg); listPc.Add(CreateProcess(arg, luajit32Path)); luabytesAssets32.Add(Path.Combine(OutLuaBytesPath, override_name)); sb.AppendLine("[\"" + crypName + "\"] = { name = \"" + override_name + "\", path = \"" + file + "\", out path = \"" + override_lua + "\"},"); } if (!string.IsNullOrEmpty(luajit64Path)) //luajit64 { string crypName_64 = CUtils.InsertAssetBundleName(crypName, "_64"); string override_name = CUtils.GetRightFileName(crypName_64); string override_lua = streamingAssetsPath + "/" + override_name; arg = "-b " + filePath + " " + override_lua; //for jit // Debug.Log(arg); listPc.Add(CreateProcess(arg, luajit64Path)); luabytesAssets64.Add(Path.Combine(OutLuaBytesPath, override_name)); sb.AppendLine("[\"" + crypName_64 + "\"] = { name = \"" + override_name + "\", path = \"" + file + "\", out path = \"" + override_lua + "\"},"); } if (!string.IsNullOrEmpty(luacPath)) //for editor { string override_name = CUtils.GetRightFileName(crypEditorName); //CUtils.GetRightFileName(CUtils.InsertAssetBundleName(crypName,"_64")); string override_lua = OutLuaPath + "/" + override_name; #if UNITY_EDITOR_OSX && !UNITY_STANDALONE_WIN arg = "-o " + override_lua + " " + filePath; //for lua #else arg = "-b " + filePath + " " + override_lua; //for jit #endif // Debug.Log(arg); listPc.Add(CreateProcess(arg, luacPath)); sb.AppendLine("[\"" + crypEditorName + "(editor)\"] = { name = \"" + override_name + "\", path = \"" + file + "\", out path = \"" + override_lua + "\"},"); } i++; // EditorUtility.DisplayProgressBar(title, info + "=>" + i.ToString() + "/" + allLen.ToString(), i / allLen); } //compile lua int total = listPc.Count; int workThreadCount = System.Environment.ProcessorCount * 2 + 2; int batchCount = (int)System.Math.Ceiling(total / (float)workThreadCount); for (int batchIndex = 0; batchIndex < batchCount; ++batchIndex) { int processIndex; int offset = batchIndex * workThreadCount; for (processIndex = 0; processIndex < workThreadCount; ++processIndex) { int fileIndex = offset + processIndex; if (fileIndex >= total) { break; } var ps = listPc[fileIndex]; ps.Start(); } bool fail = false; fileName = null; string arguments = null; for (int j = offset; j < offset + processIndex; ++j) { var ps = listPc[j]; ps.WaitForExit(); EditorUtility.DisplayProgressBar(title, info + "=>" + j.ToString() + "/" + total.ToString(), j / total); if (ps.ExitCode != 0 && !fail) { fail = true; fileName = ps.StartInfo.FileName; arguments = ps.StartInfo.Arguments; } ps.Dispose(); } if (fail) { throw new System.Exception(string.Format("Luajit Compile Fail.FileName={0},Arg={1}", fileName, arguments)); } } Debug.Log("lua:" + path + "files=" + childrens.Length + " completed"); System.Threading.Thread.Sleep(100); // AssetDatabase.Refresh(); //all luabytes in one asset // var luaBytesAsset = ScriptableObject.CreateInstance<LuaBytesAsset>(); // foreach (var file in luabytesAssets32) // { // var bytes = File.ReadAllBytes(file); // var fn = CUtils.GetAssetName(file); // luaBytesAsset.GenerateBytes(bytes, fn); // Debug.LogFormat("lua 32 bytes name ={0},len={1}", fn, bytes.Length); // } // string luaAssetPath = Path.Combine(luabytesParentPath, Common.LUA_BUNDLE_NAME_X86 + ".asset"); // AssetDatabase.DeleteAsset(luaAssetPath); // AssetDatabase.CreateAsset(luaBytesAsset, luaAssetPath); // Debug.LogFormat("lua32:{0}", luaAssetPath); // if (luabytesAssets64.Count > 0) // { // var luaBytesAsset64 = ScriptableObject.CreateInstance<LuaBytesAsset>(); // foreach (var file in luabytesAssets64) // { // var bytes = File.ReadAllBytes(file); // var fn = CUtils.GetAssetName(file); // luaBytesAsset.GenerateBytes(bytes, fn); // Debug.LogFormat("lua 64 bytes name ={0},len={1}", fn, bytes.Length); // } // luaAssetPath = Path.Combine(luabytesParentPath, Common.LUA_BUNDLE_NAME_X64 + ".asset"); // AssetDatabase.DeleteAsset(luaAssetPath); // AssetDatabase.CreateAsset(luaBytesAsset64, luaAssetPath); // Debug.LogFormat("lua64:{0}", luaAssetPath); // } //out md5 mapping file string tmpPath = EditorUtils.GetAssetTmpPath(); EditorUtils.CheckDirectory(tmpPath); string outPath = Path.Combine(tmpPath, "lua_md5mapping.txt"); Debug.Log("write to path=" + outPath); using (StreamWriter sr = new StreamWriter(outPath, false)) { sr.Write(sb.ToString()); } EditorUtility.ClearProgressBar(); }
/// <summary> /// 设置assetbundleName /// </summary> /// <param name="s"></param> public static void SetAssetBundleName(Object s, bool replace = false, bool variant = false, string variantName = null) { string abPath = AssetDatabase.GetAssetPath(s); AssetImporter import = AssetImporter.GetAtPath(abPath); string folder = EditorUtils.GetLabelsByPath(abPath); string objName = s.name.ToLower(); if (replace) { objName = HugulaEditorSetting.instance.GetAssetBundleNameByReplaceIgnore(objName); // Debug.LogFormat ("{0} replace {1}", s.name, objName); } bool isScene = abPath.EndsWith(".unity"); string name = CUtils.GetRightFileName(objName); var suffix = Common.CHECK_ASSETBUNDLE_SUFFIX; // if (variant) // variant // { // suffix = ""; // } if (string.IsNullOrEmpty(folder)) { import.assetBundleName = name + suffix; } else { import.assetBundleName = string.Format("{0}/{1}{2}", folder, name, suffix); } if (variant) { string m_variant; if (abPath.IndexOf("_high") == 0) { m_variant = "high"; } else if (abPath.IndexOf("_medium") == 0) { m_variant = "medium"; } else if (abPath.IndexOf("_low") == 0) { m_variant = "low"; } else { DirectoryInfo filePath = new DirectoryInfo(abPath); m_variant = variantName != null ? variantName : filePath.Parent.Name.ToLower(); } import.assetBundleVariant = m_variant; HugulaSetting.instance.AddVariant(m_variant); if (m_variant.Equals(Common.ASSETBUNDLE_SUFFIX)) { Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.ASSETBUNDLE_SUFFIX); } if (m_variant == Common.DOT_BYTES.Replace(".", "")) { Debug.LogWarningFormat("{0} variant folder name Equals {1}", abPath, Common.DOT_BYTES); } } if (s.name.Contains(" ")) { Debug.LogWarning(s.name + " contains space"); } Debug.Log(import.assetBundleName + ",variant=" + import.assetBundleVariant); // if (s is GameObject) // { // GameObject tar = s as GameObject; // if (tar.transform.parent == null) // { // ReferenceCount refe = LuaHelper.AddComponent(tar, typeof(ReferenceCount)) as ReferenceCount; // if (refe != null) // { // if (string.IsNullOrEmpty(import.assetBundleVariant)) // refe.assetbundle = import.assetBundleName; // else // refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant; // EditorUtility.SetDirty(s); // } // } // } // else if (isScene) //如果是场景需要添加引用计数脚本 // { //UnityEngine.SceneAsset // var sce = s; // as SceneAsset; // Debug.Log(sce); // AssetDatabase.OpenAsset(sce); // GameObject gobj = GameObject.Find(sce.name); // if (gobj == null) gobj = new GameObject(sce.name); // ReferenceCount refe = LuaHelper.AddComponent(gobj, typeof(ReferenceCount)) as ReferenceCount; // if (refe != null) // { // if (string.IsNullOrEmpty(import.assetBundleVariant)) // refe.assetbundle = import.assetBundleName; // else // refe.assetbundle = import.assetBundleName + "." + import.assetBundleVariant; // EditorUtility.SetDirty(sce); // } // var refers = GameObject.FindObjectsOfType<ReferenceCount>(); // foreach (var rf in refers) // { // if (rf != refe) // { // Debug.LogWarningFormat("you should not add ReferenceCount in {0}", EditorUtils.GetGameObjectPathInScene(rf.transform, string.Empty)); // } // } // } }
public static void GenerateAssetBundlesMd5Mapping(string[] allAssets) { string info = "Generate AssetBundles Md5Mapping "; EditorUtility.DisplayProgressBar("GenerateAssetBundlesMd5Mapping", info, 0); string speciallyPath = "Assets/Config/Lan/"; string luaPath = "Assets/Lua/"; AssetImporter import = null; float i = 0; float allLen = allAssets.Length; string name = ""; string nameMd5 = ""; //name mapping StringBuilder nameSb = new StringBuilder(); //asset map StringBuilder sb = new StringBuilder(); sb.AppendLine("return {"); foreach (string path in allAssets) { import = AssetImporter.GetAtPath(path); string line = string.Empty; if (import != null && string.IsNullOrEmpty(import.assetBundleName) == false) { string abName = import.assetBundleName; name = CUtils.GetAssetName(path).ToLower(); if (!string.IsNullOrEmpty(import.assetBundleVariant)) { abName = import.assetBundleName + "." + import.assetBundleVariant; // line = "{\"" + import.assetBundleName + "\" = { size = \"" + name + "\", path = \"" + path + "\"}},"; } line = "[\"" + abName + "\"] = { size = " + GetAssetbundleSize(abName) + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", CUtils.GetRightFileName(name), name); if (name.Contains(" ")) { Debug.LogWarning(name + " contains space"); } } else if (import != null && path.Contains(speciallyPath)) { name = CUtils.GetAssetName(path).ToLower(); string md5name = CUtils.GetRightFileName(name) + Common.CHECK_ASSETBUNDLE_SUFFIX; line = "[\"" + md5name + "\"] = { size = " + GetAssetbundleSize(md5name) + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", md5name, name); } else if (import != null && path.Contains(luaPath)) { string luaname = path.Replace(luaPath, "").Replace("\\", ".").Replace("/", "."); string luacname = luaname.Replace(".lua", "").Replace(".", "+"); string luaMd5Name = CUtils.GetRightFileName(luacname); line = "[\"" + luaMd5Name + "\"] = { size = " + GetAssetbundleSize(luaMd5Name + ".bytes") + ", path = \"" + path + "\"},"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", luaMd5Name, luaname); } EditorUtility.DisplayProgressBar("Generate AssetBundles Md5Mapping", info + "=>" + i.ToString() + "/" + allLen.ToString(), i / allLen); i++; } string[] special = new string[] { CUtils.platform, Common.CONFIG_CSV_NAME, Common.CRC32_FILELIST_NAME, Common.CRC32_VER_FILENAME }; foreach (string p in special) { name = EditorUtils.GetAssetBundleName(p); nameMd5 = CUtils.GetRightFileName(name); string line = "[\"" + nameMd5 + "\"] ={ size = 0, path = \"" + p + "\" },"; sb.AppendLine(line); nameSb.AppendFormat("{0}={1}\r\n", CUtils.GetRightFileName(name), name); } sb.AppendLine("}"); string tmpPath = Path.Combine(Application.dataPath, EditorUtils.TmpPath); EditorUtils.CheckDirectory(tmpPath); EditorUtility.DisplayProgressBar("Generate AssetBundles Md5Mapping", "write file to Assets/" + EditorUtils.TmpPath + "Md5Mapping.txt", 0.99f); string outPath = Path.Combine(tmpPath, "md5_asset_mapping.txt"); Debug.Log("write to path=" + outPath); using (StreamWriter sr = new StreamWriter(outPath, false)) { sr.Write(sb.ToString()); } outPath = Path.Combine(tmpPath, "md5_name_mapping.txt"); Debug.Log("write to path=" + outPath); using (StreamWriter sr = new StreamWriter(outPath, false)) { sr.Write(nameSb.ToString()); } EditorUtility.ClearProgressBar(); Debug.Log(info + " Complete! Assets/" + EditorUtils.TmpPath + "md5_asset_mapping.txt"); }
public static void CreateAtlasAsset() { var selection = Selection.objects; string path = string.Empty; StringBuilder sb = new StringBuilder(); foreach (Object s in selection) { if (s is DefaultAsset && (path = AssetDatabase.GetAssetPath(s)) != null && Directory.Exists(path)) { // var import = AssetImporter.GetAtPath(path); var ragName = s.name.ToLower() + "_atlas.asset"; string atlas_path = Path.Combine(path, ragName); sb.Append("Crate atlas Asset :"); sb.Append(ragName); sb.Append("\r\n"); var allchildren = EditorUtils.getAllChildFiles(path, @"\.meta$|\.manifest$|\.DS_Store$|\.u$", null, false); int count = 0; List <int> names = new List <int>(); List <Sprite> allSprites = new List <Sprite>(); foreach (var f in allchildren) { count++; TextureImporter ti = AssetImporter.GetAtPath(f) as TextureImporter; if (ti != null) { Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(f); foreach (var item in objs) { if (item is Sprite) { names.Add(LuaHelper.StringToHash(item.name)); allSprites.Add((Sprite)item); } } EditorUtility.DisplayProgressBar("Processing...", "生成中... (" + count + " / " + allchildren.Count + ")", count / allchildren.Count); } } EditorUtility.ClearProgressBar(); //生成或者替换资源 AtlasAsset atlas = AssetDatabase.LoadAssetAtPath <AtlasAsset>(atlas_path); if (atlas == null) { atlas = AtlasAsset.CreateInstance <AtlasAsset>(); AssetDatabase.CreateAsset(atlas, atlas_path); } atlas.names = names; atlas.sprites = allSprites; EditorUtility.SetDirty(atlas); var import = AssetImporter.GetAtPath(atlas_path); import.assetBundleName = Path.GetFileNameWithoutExtension(ragName) + Common.CHECK_ASSETBUNDLE_SUFFIX; sb.AppendFormat("build {0} success count = {1} ", ragName, names.Count); AssetDatabase.SaveAssets(); } } sb.AppendLine("\r\nall completed"); Debug.Log(sb.ToString()); }