123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- using UnityEngine;
- using UnityEditor;
- using System;
- ///
- /// 此类里基本都是删除/修改场景里的(hierarchy)component
- ///
- namespace AWEditor.Utilities
- {
- public class ModifyComponent : EditorWindow
- {
- string inputField;
- [MenuItem("Tools/RenderHelper")]
- public static void ShowWindow()
- {
- GetWindow(typeof(ModifyComponent));
- }
- public void OnGUI()
- {
- /// 删除所有LODGroup,保留LOD2低模
- /// 目前针对的LOD结构是 :
- /// LODGroup
- /// - LOD0
- /// - LOD1
- /// - LOD2
- if (GUILayout.Button("RemoveLOD"))
- {
- Traverse(null);
- }
-
- ///
- /// 删除动画组件
- ///
- if(GUILayout.Button("RemoveAnimator"))
- {
- Traverse<Animator>(null, (a) => { DestroyImmediate(a); });
- }
- /// <summary>
- /// 替换所有的Standard shader为Mobile diffuse
- /// </summary>
- if(GUILayout.Button("ReplaceStandardWithBumpedDiffuse"))
- {
- Traverse<MeshRenderer>(null, replaceBakeShader);
- }
- if(GUILayout.Button("ReplaceToMobileDiffuse"))
- {
- Traverse<MeshRenderer>(null, replaceRuntimeShader);
- }
- inputField = EditorGUILayout.TextField("To Delete : ", inputField);
-
- /// <summary>
- /// 删除:输入的内容-,目前必须是Unity Namespace
- /// </summary>
- if(GUILayout.Button("RemoveUnityComponent"))
- {
- if(!string.IsNullOrEmpty(inputField))
- {
- Traverse(null, inputField,(c) => { DestroyImmediate(c); });
- }
- }
- }
-
- /// <summary>
- /// 转换为烘培光照需要的贴图
- /// </summary>
- /// <param name="mesh"></param>
- void replaceBakeShader(MeshRenderer mesh)
- {
- Shader toBeCheck = mesh.sharedMaterial.shader;
- if(toBeCheck.name == "Standard")
- {
- mesh.sharedMaterial.shader = Shader.Find("Mobile/Bumped Diffuse");
- }
- }
- /// <summary>
- /// 转换为手机运行时需要的贴图
- /// </summary>
- /// <param name="mesh"></param>
- void replaceRuntimeShader(MeshRenderer mesh)
- {
- Shader toBeCheck = mesh.sharedMaterial.shader;
- if(toBeCheck.name == "Standard" || toBeCheck.name == "Mobile/Bumped Diffuse")
- {
- mesh.sharedMaterial.shader = Shader.Find("Mobile/Diffuse");
- } else {
- if(toBeCheck.name != "Mobile/Diffuse" && toBeCheck.name != "Unlit/Texture" )
- Debug.Log("Warning : " + mesh.name + " with shader of " + toBeCheck.name);
- }
- }
- /// <summary>
- /// 遍历所有T Component类型
- /// </summary>
- void Traverse<T>(GameObject root, Action<T> work) where T : Component
- {
- if (root == null)
- {
- foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
- {
- T component = obj.GetComponent<T>();
- if (component && work != null) work(component);
- }
- }
- else
- {
- foreach(Transform child in root.transform)
- {
- T component = child.GetComponent<T>();
- if (component && work != null) work(component);
- }
- }
- AssetDatabase.SaveAssets();
- }
- /// <summary>
- /// 遍历所有 Type(string)类型版本
- /// </summary>
- void Traverse(GameObject root, string mType, Action<Component> work)
- {
- if (root == null)
- {
- foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
- {
- Component component = obj.GetComponent(mType);
- if (component && work != null) work(component);
- }
- }
- else
- {
- foreach(Transform child in root.transform)
- {
- Component component = child.GetComponent(mType);
- if (component && work != null) work(component);
- }
- }
- }
- void Traverse(GameObject root)
- {
- if (root == null)
- {
- foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
- {
- LODGroup g = obj.GetComponent<LODGroup>();
- if (g != null)
- {
- DestroyImmediate(g);
- foreach (Transform child in obj.transform)
- {
- if (child.name.Contains("LOD"))
- {
- //keep LOD2
- bool isLOD2 = child.name.Contains("LOD2");
- if(!isLOD2)
- DestroyImmediate(child.gameObject);
- }
- }
- }
- }
- } else {
- foreach (Transform obj in root.transform)
- {
- LODGroup g = obj.GetComponent<LODGroup>();
- if (g != null)
- {
- DestroyImmediate(g);
- foreach (Transform child in obj.transform)
- {
- if (child.name.Contains("LOD"))
- {
- //keep LOD2
- bool isLOD2 = child.name.Contains("LOD2");
- if(!isLOD2)
- DestroyImmediate(child.gameObject);
- }
- }
- }
- }
- }
- }
-
- }
- }
|