SmartRMLOD.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. ///
  5. /// 此类里基本都是删除/修改场景里的(hierarchy)component
  6. ///
  7. namespace AWEditor.Utilities
  8. {
  9. public class ModifyComponent : EditorWindow
  10. {
  11. string inputField;
  12. [MenuItem("Tools/RenderHelper")]
  13. public static void ShowWindow()
  14. {
  15. GetWindow(typeof(ModifyComponent));
  16. }
  17. public void OnGUI()
  18. {
  19. /// 删除所有LODGroup,保留LOD2低模
  20. /// 目前针对的LOD结构是 :
  21. /// LODGroup
  22. /// - LOD0
  23. /// - LOD1
  24. /// - LOD2
  25. if (GUILayout.Button("RemoveLOD"))
  26. {
  27. Traverse(null);
  28. }
  29. ///
  30. /// 删除动画组件
  31. ///
  32. if(GUILayout.Button("RemoveAnimator"))
  33. {
  34. Traverse<Animator>(null, (a) => { DestroyImmediate(a); });
  35. }
  36. /// <summary>
  37. /// 替换所有的Standard shader为Mobile diffuse
  38. /// </summary>
  39. if(GUILayout.Button("ReplaceStandardWithBumpedDiffuse"))
  40. {
  41. Traverse<MeshRenderer>(null, replaceBakeShader);
  42. }
  43. if(GUILayout.Button("ReplaceToMobileDiffuse"))
  44. {
  45. Traverse<MeshRenderer>(null, replaceRuntimeShader);
  46. }
  47. inputField = EditorGUILayout.TextField("To Delete : ", inputField);
  48. /// <summary>
  49. /// 删除:输入的内容-,目前必须是Unity Namespace
  50. /// </summary>
  51. if(GUILayout.Button("RemoveUnityComponent"))
  52. {
  53. if(!string.IsNullOrEmpty(inputField))
  54. {
  55. Traverse(null, inputField,(c) => { DestroyImmediate(c); });
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// 转换为烘培光照需要的贴图
  61. /// </summary>
  62. /// <param name="mesh"></param>
  63. void replaceBakeShader(MeshRenderer mesh)
  64. {
  65. Shader toBeCheck = mesh.sharedMaterial.shader;
  66. if(toBeCheck.name == "Standard")
  67. {
  68. mesh.sharedMaterial.shader = Shader.Find("Mobile/Bumped Diffuse");
  69. }
  70. }
  71. /// <summary>
  72. /// 转换为手机运行时需要的贴图
  73. /// </summary>
  74. /// <param name="mesh"></param>
  75. void replaceRuntimeShader(MeshRenderer mesh)
  76. {
  77. Shader toBeCheck = mesh.sharedMaterial.shader;
  78. if(toBeCheck.name == "Standard" || toBeCheck.name == "Mobile/Bumped Diffuse")
  79. {
  80. mesh.sharedMaterial.shader = Shader.Find("Mobile/Diffuse");
  81. } else {
  82. if(toBeCheck.name != "Mobile/Diffuse" && toBeCheck.name != "Unlit/Texture" )
  83. Debug.Log("Warning : " + mesh.name + " with shader of " + toBeCheck.name);
  84. }
  85. }
  86. /// <summary>
  87. /// 遍历所有T Component类型
  88. /// </summary>
  89. void Traverse<T>(GameObject root, Action<T> work) where T : Component
  90. {
  91. if (root == null)
  92. {
  93. foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
  94. {
  95. T component = obj.GetComponent<T>();
  96. if (component && work != null) work(component);
  97. }
  98. }
  99. else
  100. {
  101. foreach(Transform child in root.transform)
  102. {
  103. T component = child.GetComponent<T>();
  104. if (component && work != null) work(component);
  105. }
  106. }
  107. AssetDatabase.SaveAssets();
  108. }
  109. /// <summary>
  110. /// 遍历所有 Type(string)类型版本
  111. /// </summary>
  112. void Traverse(GameObject root, string mType, Action<Component> work)
  113. {
  114. if (root == null)
  115. {
  116. foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
  117. {
  118. Component component = obj.GetComponent(mType);
  119. if (component && work != null) work(component);
  120. }
  121. }
  122. else
  123. {
  124. foreach(Transform child in root.transform)
  125. {
  126. Component component = child.GetComponent(mType);
  127. if (component && work != null) work(component);
  128. }
  129. }
  130. }
  131. void Traverse(GameObject root)
  132. {
  133. if (root == null)
  134. {
  135. foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
  136. {
  137. LODGroup g = obj.GetComponent<LODGroup>();
  138. if (g != null)
  139. {
  140. DestroyImmediate(g);
  141. foreach (Transform child in obj.transform)
  142. {
  143. if (child.name.Contains("LOD"))
  144. {
  145. //keep LOD2
  146. bool isLOD2 = child.name.Contains("LOD2");
  147. if(!isLOD2)
  148. DestroyImmediate(child.gameObject);
  149. }
  150. }
  151. }
  152. }
  153. } else {
  154. foreach (Transform obj in root.transform)
  155. {
  156. LODGroup g = obj.GetComponent<LODGroup>();
  157. if (g != null)
  158. {
  159. DestroyImmediate(g);
  160. foreach (Transform child in obj.transform)
  161. {
  162. if (child.name.Contains("LOD"))
  163. {
  164. //keep LOD2
  165. bool isLOD2 = child.name.Contains("LOD2");
  166. if(!isLOD2)
  167. DestroyImmediate(child.gameObject);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }