Last active
April 24, 2020 08:02
-
-
Save ioncry/9d1e7f9661396f609f83d07287f56797 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 取代Script用。 | |
/// 例如做了一支ImagePlus,想要取代某個GameObject內所有的Image Script,可以使用這支code來執行 | |
/// 2020-01 kumako | |
/// </summary> | |
[CanEditMultipleObjects] | |
public class ComponentConvertTool : EditorWindow | |
{ | |
private MonoScript m_TargetScript; | |
private GameObject m_TargetGameObject; | |
private string[] m_GameObjectStrAry; | |
private bool[] m_GameObjectToogleAry; | |
private Object[] m_ObjectAry; | |
private int[] m_ObjectHashCodeAry; | |
private string m_LogStr; | |
private Vector2 m_ScrollPos; | |
private int m_GameObjectCount = 0; | |
private GUIStyle m_ObjectUnchoosedStyle, m_ObjectChoosedStyle; | |
private GUIStyle m_ObjectListStyle; | |
[MenuItem("Tools/ConvertComponents")] | |
public static void ShowWindow() | |
{ | |
EditorWindow.GetWindow(typeof(ComponentConvertTool), false, "Component Convert Tool"); | |
} | |
private void OnGUI() | |
{ | |
if (m_ObjectListStyle == null) | |
{ | |
m_ObjectListStyle = new GUIStyle(GUI.skin.box); | |
} | |
if (m_ObjectUnchoosedStyle == null) | |
{ | |
m_ObjectUnchoosedStyle = new GUIStyle(GUI.skin.label); | |
m_ObjectUnchoosedStyle.alignment = TextAnchor.MiddleLeft; | |
m_ObjectUnchoosedStyle.normal.textColor = Color.gray; | |
} | |
if (m_ObjectChoosedStyle == null) | |
{ | |
m_ObjectChoosedStyle = new GUIStyle(GUI.skin.label); | |
m_ObjectChoosedStyle.alignment = TextAnchor.MiddleLeft; | |
m_ObjectChoosedStyle.normal.textColor = Color.white; | |
} | |
EditorGUI.BeginChangeCheck(); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("Target script:"); | |
m_TargetScript = EditorGUILayout.ObjectField(m_TargetScript, typeof(MonoScript), true) as MonoScript; | |
EditorGUILayout.EndHorizontal(); | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("Be Convert GameObject/Group: "); | |
m_TargetGameObject = EditorGUILayout.ObjectField(m_TargetGameObject, typeof(GameObject), true) as GameObject; | |
EditorGUILayout.EndHorizontal(); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
if (m_TargetScript != null) | |
{ | |
if (m_TargetGameObject != null) | |
{ | |
DoSomething("ShowTargetObject"); | |
} | |
else | |
{ | |
m_LogStr = ""; | |
m_GameObjectStrAry = null; | |
m_GameObjectCount = 0; | |
} | |
} | |
else | |
{ | |
m_LogStr = ""; | |
m_GameObjectStrAry = null; | |
m_GameObjectCount = 0; | |
} | |
} | |
EditorGUILayout.BeginHorizontal(); | |
GUILayout.Label("Include GameObject List:"); | |
if (GUILayout.Button("All")) | |
{ | |
for (int i = 0; i < m_GameObjectCount; i++) | |
{ | |
m_GameObjectToogleAry[i] = !m_GameObjectToogleAry[i]; | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (m_GameObjectCount > 0) | |
{ | |
if (m_GameObjectToogleAry == null || m_GameObjectToogleAry.Length != m_GameObjectCount) | |
{ | |
m_GameObjectToogleAry = new bool[m_GameObjectCount]; | |
for (int i = 0; i < m_GameObjectCount; i++) | |
m_GameObjectToogleAry[i] = true; | |
} | |
EditorGUILayout.BeginVertical(m_ObjectListStyle); | |
m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos, false, false); | |
for (int i = 0; i < m_GameObjectCount; i++) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
m_GameObjectToogleAry[i] = EditorGUILayout.Toggle(m_GameObjectToogleAry[i], GUILayout.Width(20)); | |
//GUILayout.Label(m_GameObjectStrAry[i], m_GameObjectToogleAry[i] ? m_ObjectChoosedStyle : m_ObjectUnchoosedStyle); | |
if (GUILayout.Button(m_GameObjectStrAry[i], m_GameObjectToogleAry[i] ? m_ObjectChoosedStyle : m_ObjectUnchoosedStyle)) | |
{ | |
Selection.activeInstanceID = m_ObjectHashCodeAry[i]; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
EditorGUILayout.EndScrollView(); | |
EditorGUILayout.EndVertical(); | |
if (GUILayout.Button("Convert")) | |
{ | |
m_LogStr = ""; | |
if (m_TargetScript == null || m_TargetGameObject == null) | |
{ | |
GUILayout.Label("ERROR! Targets are NULL", EditorStyles.boldLabel); | |
} | |
else | |
{ | |
DoSomething("DoConvert"); | |
} | |
} | |
} | |
GUILayout.Label(m_LogStr, EditorStyles.helpBox); | |
} | |
private void DoSomething(string MethodName) | |
{ | |
System.Type t = m_TargetScript.GetClass().BaseType; | |
var method = typeof(ComponentConvertTool).GetMethod(MethodName); | |
if (method != null) | |
{ | |
var fooRef = method.MakeGenericMethod(t); | |
fooRef.Invoke(this, null); | |
} | |
else | |
{ | |
Debug.Log("ERROR!Can't Find this Method:" + MethodName); | |
} | |
} | |
public void ShowTargetObject<T>() | |
{ | |
T[] oAry = m_TargetGameObject.GetComponentsInChildren<T>(true); | |
m_GameObjectCount = oAry.Length; | |
m_GameObjectStrAry = new string[m_GameObjectCount]; | |
m_ObjectAry = new Object[m_GameObjectCount]; | |
m_ObjectHashCodeAry = new int[m_GameObjectCount]; | |
for (int i = 0; i < m_GameObjectCount; i++) | |
{ | |
m_ObjectAry[i] = oAry[i] as Object; | |
m_ObjectHashCodeAry[i] = oAry[i].GetHashCode(); | |
m_GameObjectStrAry[i] += oAry[i].ToString(); | |
} | |
if (oAry.Length == 0) | |
{ | |
m_LogStr = "NO Script Match BaseType("+typeof(T).ToString()+")"; | |
} | |
} | |
public void DoConvert<T>() | |
{ | |
for (int i = 0;i < m_GameObjectCount; i++) | |
{ | |
if (m_GameObjectToogleAry[i]) | |
{ | |
Object o = m_ObjectAry[i]; | |
var so = new SerializedObject(o); | |
so.Update(); | |
so.FindProperty("m_Script").objectReferenceValue = m_TargetScript; | |
so.ApplyModifiedProperties(); | |
} | |
} | |
DoSomething("ShowTargetObject"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment