AssetOpt.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System;
  10. using System.Xml;
  11. using UnityEngine.UI;
  12. using UnityEngine.Rendering;
  13. using UnityEngine;
  14. public class AssetOpt
  15. {
  16. [MenuItem("Assets/设置PackingTag")]
  17. static void SetAtlasName()
  18. {
  19. UnityEngine.Object obj = Selection.activeObject;
  20. string assetPath = AssetDatabase.GetAssetPath(obj);
  21. if(!Directory.Exists(assetPath))
  22. {
  23. return;
  24. }
  25. string packTagName = FileUtils.ExtractPureName(assetPath) + "Atlas";
  26. string[] files = FileUtils.TraverseAllFiles(assetPath,"*.png");
  27. for(int idx =0; idx < files.Length;idx++)
  28. {
  29. TextureImporter ai = (TextureImporter)AssetImporter.GetAtPath(files[idx]);
  30. if(ai!=null)
  31. {
  32. ai.spritePackingTag = packTagName;
  33. ai.SaveAndReimport();
  34. }
  35. }
  36. }
  37. static string[] exposeNodes = new string[] { "Base_Point", "Back_Point", "foot_point", "Foot_Point", "Head_Point", "Hit_Point", "LHand_Point", "LArm_Point", "RHand_Point", "RArm_Point", "ui_point", "look_point", "Weapon_Point","Weapon_Point1","Weapon_Point2","Weapon_Point3","Weapon_Point4","Weapon_Point5", "Add1_Point","Add1_Point1","Add1_Point2","Add1_Point3","Add1_Point4","Add1_Point5"};
  38. static List<string> nodes = new List<string>();
  39. [MenuItem("Assets/OptModel")]
  40. static void OptModelPrefab()
  41. {
  42. nodes.Clear();
  43. GameObject selectGo = Selection.activeGameObject;
  44. if (selectGo == null || PrefabUtility.GetPrefabAssetType(selectGo) != PrefabAssetType.Regular) return;
  45. nodes.AddRange(exposeNodes);
  46. string assetPath = AssetDatabase.GetAssetPath(selectGo);
  47. string[] deps = AssetDatabase.GetDependencies(assetPath);
  48. string fbxAssetPath = "";
  49. for(int idx =0; idx < deps.Length;idx++)
  50. {
  51. if(deps[idx].Contains(".FBX"))
  52. {
  53. fbxAssetPath = deps[idx];
  54. break;
  55. }
  56. }
  57. if (string.IsNullOrEmpty(fbxAssetPath)) return;
  58. string rootName = selectGo.name;
  59. GameObject oldGo = GameObject.Instantiate(selectGo);
  60. oldGo.name = rootName;
  61. for (int idx =0; idx < exposeNodes.Length;idx++)
  62. {
  63. Transform t = UnityEngineUtils.RecurisiveFindTransformChild(oldGo.transform, exposeNodes[idx]);
  64. if(t!=null && t.parent!=null && t.parent.name != rootName)
  65. {
  66. if (!nodes.Contains(t.parent.name))
  67. {
  68. nodes.Add(t.parent.name);
  69. }
  70. }
  71. }
  72. List<string> exposeNames = new List<string>();
  73. ModelImporter mi = AssetImporter.GetAtPath(fbxAssetPath) as ModelImporter;
  74. string[] paths = mi.transformPaths;
  75. for (int idx = 0; idx < paths.Length; idx++)
  76. {
  77. if (IsExposeNode(paths[idx]))
  78. {
  79. exposeNames.Add(paths[idx]);
  80. }
  81. }
  82. mi.optimizeGameObjects = true;
  83. mi.extraExposedTransformPaths = exposeNames.ToArray();
  84. mi.SaveAndReimport();
  85. oldGo.name = rootName;
  86. GameObject fbxGo = AssetDatabase.LoadAssetAtPath<GameObject>(fbxAssetPath);
  87. GameObject newGo = GameObject.Instantiate(fbxGo);
  88. newGo.name = rootName;
  89. SkinnedMeshRenderer[] oldSmrs = oldGo.GetComponentsInChildren<SkinnedMeshRenderer>(true);
  90. SkinnedMeshRenderer[] smrs = newGo.GetComponentsInChildren<SkinnedMeshRenderer>(true);
  91. for(int idx =0; idx < oldSmrs.Length;idx++)
  92. {
  93. var smr = smrs[idx];
  94. var oldSmr = oldSmrs[idx];
  95. smr.sharedMaterials = oldSmr.sharedMaterials;
  96. }
  97. for(int idx =0; idx < exposeNodes.Length;idx++)
  98. {
  99. Transform t = UnityEngineUtils.RecurisiveFindTransformChild(oldGo.transform, exposeNodes[idx]);
  100. if(t!=null)
  101. {
  102. Transform t1 = UnityEngineUtils.RecurisiveFindTransformChild(newGo.transform, exposeNodes[idx]);
  103. if(t1!=null)
  104. {
  105. if (t.parent != null)
  106. {
  107. Transform t1Parent = UnityEngineUtils.RecurisiveFindTransformChild(newGo.transform, t.parent.name);
  108. if(t1Parent!=null)
  109. {
  110. t1.SetParent(t1Parent);
  111. t1.localPosition = t.localPosition;
  112. t1.localScale = t.localScale;
  113. t1.localRotation = t.localRotation;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. GameObject t1Go = new GameObject(t.name);
  120. if (t.parent != null)
  121. {
  122. Transform t1Parent = UnityEngineUtils.RecurisiveFindTransformChild(newGo.transform, t.parent.name);
  123. if (t1Parent != null)
  124. {
  125. t1Go.transform.SetParent(t1Parent);
  126. t1Go.transform.localPosition = t.localPosition;
  127. t1Go.transform.localScale = t.localScale;
  128. t1Go.transform.localRotation = t.localRotation;
  129. }
  130. }
  131. else
  132. {
  133. t1Go.transform.SetParent(newGo.transform);
  134. t1Go.transform.localPosition = t.localPosition;
  135. t1Go.transform.localScale = t.localScale;
  136. t1Go.transform.localRotation = t.localRotation;
  137. }
  138. }
  139. }
  140. }
  141. Animator oldCtrl = oldGo.GetComponent<Animator>();
  142. Animator newCtrl = newGo.GetComponent<Animator>();
  143. if(oldCtrl!=null && newCtrl!=null)
  144. {
  145. newCtrl.runtimeAnimatorController = oldCtrl.runtimeAnimatorController;
  146. }
  147. bool success = false;
  148. PrefabUtility.SaveAsPrefabAsset(newGo,assetPath,out success);
  149. GameObject.DestroyImmediate(oldGo);
  150. GameObject.DestroyImmediate(newGo);
  151. }
  152. [MenuItem("Assets/SaveRTToPNG")]
  153. static void SaveRTTOPng()
  154. {
  155. UnityEngine.Object selectRTGO = Selection.activeObject;
  156. if (selectRTGO == null) return;
  157. string path = Application.dataPath + "/test.png";
  158. RenderTexture rt = selectRTGO as RenderTexture;
  159. if(rt != null)
  160. {
  161. RenderTexture.active = rt;
  162. Texture2D newTexture = new Texture2D(rt.width, rt.height, TextureFormat.ARGB32, true);
  163. newTexture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0);
  164. byte[] bytes = newTexture.EncodeToPNG();
  165. if (bytes != null && bytes.Length > 0)
  166. {
  167. File.WriteAllBytes(path, bytes);
  168. Debug.Log("Save Down");
  169. }
  170. }
  171. }
  172. //[MenuItem("Assets/ExposeNode")]
  173. static void ExposeFbxNode()
  174. {
  175. GameObject selectGo = Selection.activeGameObject;
  176. if (selectGo == null || PrefabUtility.GetPrefabAssetType(selectGo) != PrefabAssetType.Model) return;
  177. List<string> exposeNames = new List<string>();
  178. string modelPath = AssetDatabase.GetAssetPath(selectGo);
  179. ModelImporter mi = AssetImporter.GetAtPath(modelPath) as ModelImporter;
  180. string[] paths = mi.transformPaths;
  181. for(int idx =0; idx < paths.Length;idx++)
  182. {
  183. if(IsExposeNode(paths[idx]))
  184. {
  185. exposeNames.Add(paths[idx]);
  186. }
  187. }
  188. mi.optimizeGameObjects = true;
  189. mi.extraExposedTransformPaths = exposeNames.ToArray();
  190. mi.SaveAndReimport();
  191. }
  192. static bool IsExposeNode(string nodeName)
  193. {
  194. string pureName = FileUtils.ExtractPureName(nodeName);
  195. for (int idx =0; idx < nodes.Count;idx++)
  196. {
  197. if (pureName == nodes[idx]) return true;
  198. }
  199. return false;
  200. }
  201. [MenuItem("Assets/RO/清理Animator")]
  202. static void ClearGoAnimator()
  203. {
  204. GameObject selectPrefab = Selection.activeGameObject;
  205. if (selectPrefab == null) return;
  206. GameObject selectGo = PrefabUtility.InstantiatePrefab(selectPrefab) as GameObject;
  207. bool changed = false;
  208. var animators = selectGo.GetComponentsInChildren<Animator>(true);
  209. DebugHelper.LogError("LEN:" + animators.Length);
  210. for (int i = animators.Length-1; i >=0; i--)
  211. {
  212. animators[i].DestroyImmediate();
  213. changed = true;
  214. }
  215. if(changed)
  216. PrefabUtility.ReplacePrefab(selectGo, selectPrefab);
  217. GameObject.DestroyImmediate(selectGo);
  218. }
  219. [MenuItem("Tools/检查/查找使用某个shader的材质")]
  220. static void CheckMaterial()
  221. {
  222. string shaderName = "SpeedTree";
  223. StringBuilder strBuilder = new StringBuilder();
  224. string[] files = FileUtils.TraverseAllFiles("Assets", "*.mat");
  225. for(int idx =0; idx < files.Length;idx++)
  226. {
  227. string fileName = FileUtils.ExtractAssetRelativePath(files[idx]);
  228. Material mat = AssetDatabase.LoadAssetAtPath<Material>(fileName);
  229. if(mat != null && mat.shader.name == shaderName)
  230. {
  231. strBuilder.AppendLine(fileName);
  232. }
  233. }
  234. Debug.LogError(strBuilder.ToString());
  235. }
  236. [MenuItem("GameObject/统计场景Mesh", false, 23)]
  237. static void CheckSceneMesh()
  238. {
  239. GameObject selectPrefab = Selection.activeGameObject;
  240. if (selectPrefab == null) return;
  241. MeshFilter[] meshFilters = selectPrefab.GetComponentsInChildren<MeshFilter>();
  242. if (meshFilters == null) return;
  243. long tris = 0;
  244. long verts = 0;
  245. for (int idx =0; idx < meshFilters.Length;idx++)
  246. {
  247. MeshFilter mf = meshFilters[idx];
  248. if(mf.sharedMesh != null)
  249. {
  250. tris += mf.sharedMesh.triangles.Length / 3;
  251. verts += mf.sharedMesh.vertexCount;
  252. }
  253. }
  254. Debug.Log("场景 三角面:" + tris + " 顶点数:" + verts);
  255. }
  256. [MenuItem("Tools/优化/生成SVC")]
  257. static void PrintMatKeyword()
  258. {
  259. int startIndex = 0;
  260. List<GameObject> goList = new List<GameObject>();
  261. string[] effectPrefabs = FileUtils.TraverseAllFiles(Constants.EffectPath, "*.prefab");
  262. for (int idx = 0; idx < effectPrefabs.Length; idx++)
  263. {
  264. string fullPath = effectPrefabs[idx];
  265. string relativePath = FileUtils.ExtractAssetRelativePath(fullPath);
  266. GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(relativePath);
  267. if(prefab!=null)
  268. {
  269. GameObject go = GameObject.Instantiate(prefab);
  270. goList.Add(go);
  271. }
  272. }
  273. EditorApplication.update = delegate ()
  274. {
  275. startIndex++;
  276. if(startIndex == 10)
  277. {
  278. EditorApplication.update = null;
  279. for(int idx =0; idx < goList.Count;idx++)
  280. {
  281. GameObject.DestroyImmediate(goList[idx]);
  282. }
  283. goList.Clear();
  284. Debug.Log("completed");
  285. }
  286. };
  287. }
  288. }