#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Linq; using System.Collections.Generic; using UnityEngine.UI; using LuaFramework; using System; using System.IO; using System.Collections; public class MixTool : MonoBehaviour { static List files = new List(); static string destDir = "TextureNeedChange/texture/"; static string cacheEffectFilePath = "TextureNeedChange/EffectPathCache.txt"; static string cacheModelFilePath = "TextureNeedChange/ModelPathCache.txt"; static string cacheSceneFilePath = "TextureNeedChange/ScenePathCache.txt"; static string cacheIconFilePath = "TextureNeedChange/IconPathCache.txt"; static Dictionary cachePaths; static string[] allPath; static long startTimeMs = 0; static int startIndex = 0; static int count = 0; static List prefabsPath; static public string GetCacheFilePath(string textureType) { string cacheFilePath = ""; if(textureType == "effect") cacheFilePath = cacheEffectFilePath; if(textureType == "model") cacheFilePath = cacheModelFilePath; if(textureType == "scene") cacheFilePath = cacheSceneFilePath; if(textureType == "icon") cacheFilePath = cacheIconFilePath; return cacheFilePath; } static public void OnReadCachePath(string textureType) { string cacheFilePath = GetCacheFilePath(textureType); if (File.Exists(cacheFilePath)) { cachePaths = new Dictionary(); var infoContent = File.ReadAllLines(cacheFilePath); foreach (var item in infoContent) { var kvParts = item.Split(','); cachePaths.Add(kvParts[0], kvParts[1]); } } } static public void OnWriteCachePath(string textureType) { string cacheFilePath = GetCacheFilePath(textureType); List cacheList = new List(); foreach (var item in cachePaths) { cacheList.Add(item.Key+","+item.Value); } File.WriteAllLines(cacheFilePath, cacheList.ToArray()); } static public string GetFileName(string filePath) { int pos = filePath.LastIndexOf("/"); string fileName = ""; if(pos > 0) { fileName = filePath.Remove(0, pos+1); // Debug.Log("fileName = " + fileName); } return fileName; } static void OnCopyTexture(string textTurePath, string textureType, int maxSize) { files.Clear(); FindFileContentWithContain(textTurePath, ".meta", "tga"); FindFileContentWithContain(textTurePath, ".meta", "png"); FindFileContentWithContain(textTurePath, ".meta", "jpg"); Dictionary resList = new Dictionary(); cachePaths = new Dictionary(); for (int z = 0; z < files.Count; z++) { string filePath = files[z]; filePath = filePath.Substring(0, filePath.Length - 5); filePath = filePath.Replace('\\', '/'); bool is_tga = false; if (filePath.Contains(".tga")) { is_tga = true; } Texture2D t = LoadByIO(filePath); int curMinSize = t.width; if(t.height < curMinSize) curMinSize = t.height; if (is_tga || curMinSize > maxSize) { string fileName = GetFileName(filePath); string tempPath = GetParmPath(filePath, textureType); string parmPath = "/" + tempPath; string desFile = destDir + textureType + parmPath + fileName; string path = Path.GetDirectoryName(desFile); if (!Directory.Exists(path)) Directory.CreateDirectory(path); if (File.Exists(filePath)) { cachePaths[fileName] = filePath; // Debug.Log("filePath = " + filePath); // Debug.Log("desFile = " + desFile); File.Copy(filePath, desFile, true); } } DestroyImmediate(t); } OnWriteCachePath(textureType); files.Clear(); EditorUtility.DisplayDialog("提示", "抽取贴图完成", "确定"); } static void OnPasteTexture(string textureType) { OnReadCachePath(textureType); foreach (var item in cachePaths) { string fileName = item.Key; string desFilePath = item.Value; string tempPath = GetParmPath(desFilePath, textureType); string parmPath = "/" + tempPath; string curFilePath = destDir + textureType + parmPath + fileName; if (File.Exists(curFilePath)) { File.Copy(curFilePath, desFilePath, true); } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.DisplayDialog("提示", "替换贴图完成", "确定"); } public static string GetParmPath(string filePath, string textureType) { string parmPath = ""; if (textureType == "model") { // alphatexture后续要手动排查 int alpha_name_pos = filePath.LastIndexOf("object/alphatexture"); int role_name_pos = filePath.LastIndexOf("object/role"); int pet_name_pos = filePath.LastIndexOf("object/pet"); int mount_name_pos = filePath.LastIndexOf("object/mount"); int npc_name_pos = filePath.LastIndexOf("object/npc"); int boss_name_pos = filePath.LastIndexOf("object/monster/boss"); // Debug.Log("filePath = " + filePath + alpha_name_pos +";" + role_name_pos +";" + pet_name_pos +";" + mount_name_pos +";" + boss_name_pos +";"); if (alpha_name_pos>0 || role_name_pos>0 || pet_name_pos>0 || mount_name_pos>0 || npc_name_pos>0 || boss_name_pos>0) { parmPath = "512/"; } else { parmPath = "256/"; } } return parmPath; } public static Texture2D LoadByIO(string path) { float time = Time.time; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.Begin); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, (int)fs.Length); fs.Close(); fs.Dispose(); fs = null; Texture2D t = new Texture2D(1,1); t.LoadImage(bytes); return t; } // [MenuItem("MixTool/DeleteFile _F4", false, 150)] // public static void DeleteFile() // { // String path = AppConst.AppDataPath + "/Assets/LuaFramework/AssetBundleRes/ui/common/comon_delete"; // var files = Directory.GetFiles(path, "*.png"); // foreach (var file in files) // { // string path1 = file.Replace("comon_delete", "texture"); // if (File.Exists(path1)) // { // Debug.Log(path1); // File.Delete(path1); // } // } // EditorUtility.DisplayDialog("提示", "DeleteFile完成", "确定"); // } public static void FindFileContentWithContain(string root_path, string extstr, string contain) { string[] names = Directory.GetFiles(root_path); string[] dirs = Directory.GetDirectories(root_path); foreach (string filename in names) { string ext = Path.GetExtension(filename); if (ext.Equals(extstr)) { if (filename.Contains(contain)) { files.Add(filename); } } } foreach (string dir in dirs) { FindFileContentWithContain(dir, extstr, contain); } } [MenuItem("MixTool/OptimizeBatch _F5", false, 151)] public static void OptimizeBatchForMenu() { OptimizeBatch(Selection.activeTransform); } public static void OptimizeBatch(Transform trans) { if (trans == null) return; Dictionary> imageGroup = new Dictionary>(); Dictionary> textGroup = new Dictionary>(); Dictionary> tmpGroup = new Dictionary>(); List> sortedImgageGroup = new List>(); List> sortedTextGroup = new List>(); List> sortedTMPGroup = new List>(); for (int i = 0; i < trans.childCount; i++) { Transform child = trans.GetChild(i); Texture cur_texture = null; Image img = child.GetComponent(); if (img != null) { cur_texture = img.mainTexture; } else { RawImage rimg = child.GetComponent(); if (rimg != null) cur_texture = rimg.mainTexture; } if (cur_texture != null) { string cur_path = AssetDatabase.GetAssetPath(cur_texture); TextureImporter importer = AssetImporter.GetAtPath(cur_path) as TextureImporter; // Debug.Log("cur_path : " + cur_path + " importer:"+(importer!=null).ToString()); if (importer != null) { string atlas = importer.spritePackingTag; // Debug.Log("atlas : " + atlas); if (atlas != "") { if (!imageGroup.ContainsKey(atlas)) { List list = new List(); sortedImgageGroup.Add(list); imageGroup.Add(atlas, list); } imageGroup[atlas].Add(child); } } } else { Text text = child.GetComponent(); if (text != null) { string fontName = text.font.name; //Debug.Log("fontName : " + fontName); if (!textGroup.ContainsKey(fontName)) { List list = new List(); sortedTextGroup.Add(list); textGroup.Add(fontName, list); } textGroup[fontName].Add(child); } else { TMPro.TextMeshProUGUI tmp = child.GetComponent(); if (tmp != null) { string fontName = tmp.font.name; //Debug.Log("fontName : " + fontName); if (!tmpGroup.ContainsKey(fontName)) { List list = new List(); sortedTMPGroup.Add(list); tmpGroup.Add(fontName, list); } tmpGroup[fontName].Add(child); } } } OptimizeBatch(child); } //同一图集的Image间层级顺序继续保留,不同图集的顺序就按每组第一张的来 for (int i = sortedImgageGroup.Count - 1; i >= 0; i--) { List children = sortedImgageGroup[i]; for (int j = children.Count - 1; j >= 0; j--) { children[j].SetAsFirstSibling(); } } foreach (var item in sortedTextGroup) { List children = item; for (int i = 0; i < children.Count; i++) { children[i].SetAsLastSibling(); } } foreach (var item in sortedTMPGroup) { List children = item; for (int i = 0; i < children.Count; i++) { children[i].SetAsLastSibling(); } } } [MenuItem("MixTool/" + "effect - 抽取特效贴图")] static void CopyEffectTexture() { string textTurePath = "Assets/LuaFramework/AssetBundleRes/scene/effect/texture"; string textureType = "effect"; int maxSize = 256; OnCopyTexture(textTurePath, textureType, maxSize); } [MenuItem("MixTool/" + "effect - 替换特效贴图")] static void PasteEffectTexture() { OnPasteTexture("effect"); } [MenuItem("MixTool/" + "model - 抽取模型贴图")] static void CopyModelTexture() { string textTurePath = "Assets/LuaFramework/AssetBundleRes/scene/object"; string textureType = "model"; int maxSize = 256; OnCopyTexture(textTurePath, textureType, maxSize); } [MenuItem("MixTool/" + "model - 替换模型贴图")] static void PasteModelTexture() { OnPasteTexture("model"); } [MenuItem("MixTool/" + "scene - 抽取场景贴图")] static void CopySceneTexture() { // string textTurePath = "Assets/LuaFramework/AssetBundleRes/scene/terrain"; string textTurePath = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/1003_new/Textures"; string textureType = "scene"; int maxSize = 512; OnCopyTexture(textTurePath, textureType, maxSize); } [MenuItem("MixTool/" + "scene - 替换场景贴图")] static void PasteSceneTexture() { OnPasteTexture("scene"); } [MenuItem("MixTool/" + "scene - 抽取icon贴图")] static void CopyIconTexture() { string textTurePath = "Assets/LuaFramework/AssetBundleRes/icon"; string textureType = "icon"; int maxSize = 1024; OnCopyTexture(textTurePath, textureType, maxSize); } [MenuItem("MixTool/" + "测试信息 ")] static void TestInfo() { // GameObject[] tempObject; // if (Selection.activeGameObject) // { // tempObject = Selection.gameObjects; // for (int i = 0; i < tempObject.Length; i++) // { // Debug.Log("Object name: " + tempObject[i].name); // Debug.Log("Lightmaping Index: " + tempObject[i].GetComponent().lightmapIndex); // Debug.Log("Lightmaping Offset: " + tempObject[i].GetComponent().lightmapScaleOffset); // } // } string filePath = "object/texture/talisman/res/model_talisman_1003/model/model_talisman_1003_mask.jpg"; int pos = filePath.LastIndexOf('/'); string fileName = ""; if(pos > 0) { fileName = filePath.Remove(0, pos+1); Debug.Log("fileName = " + fileName); } } [MenuItem("MixTool/" + "ResetTerrainLayer ")] static void ResetTerrainLayer() { Time.timeScale = 1; List prefabsPath = new List(); string path = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/"; //string path = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/1001_new/"; allPath = Directory.GetFiles(path, "*prefab*", SearchOption.AllDirectories); EditorApplication.update += EditorUpdateLayer; } public static void EditorUpdateLayer() { long curTimeMs = GetTimeMs(); //Debug.Log("curTimeMs = " + curTimeMs); if (Math.Abs(curTimeMs - startTimeMs) >= 0.1*1000) { string filePath = allPath[startIndex]; if (filePath.EndsWith(".prefab")) { GameObject prefab = AssetDatabase.LoadAssetAtPath(filePath); if (prefab) { startTimeMs = GetTimeMs(); //Debug.Log("prefab.name = " + prefab.name + ",prefab.layer = " + prefab.layer); if (prefab.layer == 0) { count++; SetSceneObjLayer(prefab, 30); AssetDatabase.SaveAssets(); } } } bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format("资源处理中({0}/{1})", startIndex, allPath.Length), filePath, (float)startIndex / (float)allPath.Length); startIndex++; if (isCancel || startIndex >= allPath.Length) { AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); EditorApplication.update = null; Debug.Log("处理完成, 处理数量:"+ count); startIndex = 0; count = 0; } } } public static void SetSceneObjLayer(GameObject obj, int layer) { obj.layer = layer; if (obj.transform.childCount <= 0) { return; } foreach (Transform child in obj.transform) { SetSceneObjLayer(child.gameObject, layer); } } public static long GetTimeMs() { long currentTicks = DateTime.Now.Ticks; DateTime dtFrom = new DateTime(1970, 1, 1, 0, 0, 0, 0); long currentMillis = (currentTicks - dtFrom.Ticks) / 10000; return currentMillis; } [MenuItem("MixTool/" + "CloseLightData ")] static void CloseLightData() { startIndex = 0; count = 0; Time.timeScale = 1; prefabsPath = new List(); //string path = "Assets/LuaFramework/AssetBundleRes/scene/object"; //var files = Directory.GetFiles(path, "*prefab*", SearchOption.AllDirectories); //foreach (var file in files) //{ // if (file.EndsWith(".prefab")) // { // prefabsPath.Add(file); // } //} //string path = "Assets/LuaFramework/AssetBundleRes/scene/effect/objs"; //var files = Directory.GetFiles(path, "*prefab*", SearchOption.AllDirectories); //foreach (var file in files) //{ // if (file.EndsWith(".prefab")) // { // prefabsPath.Add(file); // } //} string path = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/"; //string path = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/1001_new"; var files = Directory.GetFiles(path, "*prefab*", SearchOption.AllDirectories); foreach (var file in files) { if (file.EndsWith(".prefab")) { prefabsPath.Add(file); } } allPath = prefabsPath.ToArray(); EditorApplication.update += EditorUpdateLightData; } public static void EditorUpdateLightData() { long curTimeMs = GetTimeMs(); //Debug.Log("curTimeMs = " + curTimeMs); if (Math.Abs(curTimeMs - startTimeMs) >= 0.1 * 1000) { string singlePaht = allPath[startIndex]; int map_1000_index = singlePaht.LastIndexOf("map/1000"); if (map_1000_index <= 0 ) { var prefab = AssetDatabase.LoadAssetAtPath(singlePaht); if (prefab) { startTimeMs = GetTimeMs(); CheckSingPrefab(prefab); AssetDatabase.SaveAssets(); } } bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format("资源处理中({0}/{1})", startIndex, allPath.Length), singlePaht, (float)startIndex / (float)allPath.Length); startIndex++; if (isCancel || startIndex >= allPath.Length) { AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); EditorApplication.update = null; Debug.Log("处理完成, 处理数量:" + count); startIndex = 0; count = 0; } } } public static void CheckSingPrefab(GameObject go) { CheckSingGameObject(go); foreach (Transform child in go.transform) { if (child.childCount > 0) { CheckSingPrefab(child.gameObject); } CheckSingGameObject(child.gameObject); } } public static void CheckSingGameObject(GameObject go) { var img = go.GetComponentInChildren(); if (img != null) { if (img.receiveShadows || img.lightProbeUsage != UnityEngine.Rendering.LightProbeUsage.Off || img.reflectionProbeUsage != UnityEngine.Rendering.ReflectionProbeUsage.Off || img.shadowCastingMode != UnityEngine.Rendering.ShadowCastingMode.Off || img.motionVectorGenerationMode != MotionVectorGenerationMode.ForceNoMotion || img.skinnedMotionVectors == true) { img.receiveShadows = false; img.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; img.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off; img.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; img.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion; img.skinnedMotionVectors = false; count++; } } else { var img2 = go.GetComponentInChildren(); if (img2 != null) { if (img2.receiveShadows || img2.lightProbeUsage != UnityEngine.Rendering.LightProbeUsage.Off || img2.reflectionProbeUsage != UnityEngine.Rendering.ReflectionProbeUsage.Off || img2.shadowCastingMode != UnityEngine.Rendering.ShadowCastingMode.Off || img2.motionVectorGenerationMode != MotionVectorGenerationMode.ForceNoMotion) { img2.receiveShadows = false; img2.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off; img2.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off; img2.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; img2.motionVectorGenerationMode = MotionVectorGenerationMode.ForceNoMotion; count++; } } } } [MenuItem("MixTool/" + "ResetAlphaMaterial-1001 ")] public static void ResetAlphaMaterial() { Dictionary path = new Dictionary(); path.Add("special/XSCCZ_CWJ01_11_Alpha", "XSCCZ_CWJ01_01_D2_ALPHA.mat"); path.Add("special/XSCCZ_CWJ01_12_Alpha", "XSCCZ_CWJ01_02_ALPHA.mat"); path.Add("special/XSCCZ_CWJ01_08_Alpha", "XSCCZ_CWJ01_02_ALPHA.mat"); path.Add("special/XSCCZ_CWJ01_01_Alpha", "XSCCZ_CWJ01_01_D_ALPHA.mat"); path.Add("special/XSCCZ_JZ01_02_Alpha", "XSCCZ_JZ01_01_ALPHA.mat"); path.Add("special/XSCCZ_JZ02_01_Alpha", "XSCCZ_JZ02_01_ALPHA.mat"); path.Add("special/XSCCZ_WJ03_05_Alpha", "XSCCZ_WJ03_01_ALPHA.mat"); path.Add("special/XSCCZ_WJ02_07_Alpha", "XSCCZ_WJ02_01_ALPHA.mat"); path.Add("special/XSCCZ_CWJ01_13_Alpha", "XSCCZ_CWJ01_01_D2_ALPHA.mat"); foreach (var item in path) { string prefab_path = item.Key; var obj = GameObject.Find(prefab_path); Debug.Log("obj = " + obj); if(obj) { MeshRenderer render = obj.GetComponent(); Debug.Log("render = " + render); if (render) { string root_path = "Assets/LuaFramework/AssetBundleRes/scene/terrain/map/1001_new/Materials/"; string mat_path = root_path + item.Value; Debug.Log("mat_path = " + mat_path); var mat = AssetDatabase.LoadAssetAtPath(mat_path); if(mat) { Debug.Log("mat = " + mat); render.material = mat; } } } AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); } } } #endif