| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEngine.UI;
- [CustomEditor(typeof(UINode))]
- [CanEditMultipleObjects]
- public class UINodeEditor : Editor
- {
- struct UINodeEditorSetting
- {
- public string[] keys;
- public string[] values;
- public UINodeEditorSetting(string[] keys, string[] values)
- {
- this.keys = keys;
- this.values = values;
- }
- }
- static List<UINodeEditorSetting> UINodeEditorSettings = new List<UINodeEditorSetting>
- {
- //new UINodeEditorSetting(new string[2]{ typeof(Button).FullName, typeof(Image).FullName }, new string[1]{ typeof(Image).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Text).FullName }, new string[1]{ typeof(Text).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Button).FullName }, new string[1]{ typeof(Button).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Toggle).FullName }, new string[1]{ typeof(Toggle).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Slider).FullName }, new string[1]{ typeof(Slider).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Scrollbar).FullName }, new string[1]{ typeof(Scrollbar).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(Dropdown).FullName }, new string[1]{ typeof(Dropdown).FullName }),
- new UINodeEditorSetting(new string[1]{ typeof(InputField).FullName }, new string[1]{ typeof(InputField).FullName }),
- };
- void OnEnable()
- {
- }
- public override void OnInspectorGUI()
- {
- base.OnInspectorGUI();
- serializedObject.Update();
- List<UINode> nodes = new List<UINode>();
- UnityEngine.Object[] targetObjects = serializedObject.targetObjects;
- foreach (UnityEngine.Object targetObject in targetObjects)
- {
- UINode uiNode = targetObject as UINode;
- if (uiNode == null)
- {
- continue;
- }
- if (uiNode.keys == null)
- {
- uiNode.keys = new List<string>();
- }
- if (uiNode.values == null)
- {
- uiNode.values = new List<bool>();
- }
- //check
- Component[] components = uiNode.gameObject.GetComponents<Component>();
- for (int i = uiNode.keys.Count - 1; i >= 0; i--)
- {
- string key = uiNode.keys[i];
- bool b = false;
- foreach (var component in components)
- {
- if (component.GetType().FullName == key)
- {
- b = true;
- break;
- }
- }
- if (!b)
- {
- uiNode.keys.RemoveAt(i);
- uiNode.values.RemoveAt(i);
- }
- }
- List<string> addComp = new List<string>();
- foreach (var component in components)
- {
- bool b = false;
- for (int i = 0; i < uiNode.keys.Count; i++)
- {
- string key = uiNode.keys[i];
- if (component.GetType().FullName == key)
- {
- b = true;
- break;
- }
- }
- if (!b)
- {
- addComp.Add(component.GetType().FullName);
- //设置默认
- uiNode.keys.Add(component.GetType().FullName);
- uiNode.values.Add(false);
- }
- }
- if (addComp.Count > 0)
- {
- foreach (var settings in UINodeEditorSettings)
- {
- bool b = true;
- foreach (var key in settings.keys)
- {
- bool tmpB = false;
- foreach (var addC in addComp)
- {
- if (addC == key)
- {
- tmpB = true;
- break;
- }
- }
- if (!tmpB)
- {
- b = false;
- break;
- }
- }
- if (b)
- {
- foreach (var value in settings.values)
- {
- uiNode.SetValue(value, true);
- }
- }
- }
- }
- nodes.Add(uiNode);
- }
- Dictionary<string, bool> keyValues = new Dictionary<string, bool>();
- Color tmp = GUI.color;
- foreach (var firstNode in nodes)
- {
- for (int i = 0; i < firstNode.keys.Count; i++)
- {
- string key = firstNode.keys[i];
- if (key == "UINode")
- {
- continue;
- }
- bool currB = firstNode.values[i];
- bool b = true;
- bool bSame = true;
- foreach (var node in nodes)
- {
- bool value;
- if (!node.GetValue(key, out value))
- {
- b = false;
- break;
- }
- else
- {
- if (value != currB)
- {
- bSame = false;
- }
- }
- }
- if (b)
- {
- if (!bSame)
- {
- GUI.color = Color.red;
- currB = false;
- }
- string name = key;
- int indexOf = name.LastIndexOf(".");
- if (indexOf != -1)
- {
- name = name.Substring(indexOf + 1, name.Length - indexOf - 1);
- }
- bool toggle = EditorGUILayout.Toggle(name, currB);
- if (currB != toggle)
- {
- keyValues[key] = toggle;
- }
- if (!bSame)
- GUI.color = tmp;
- }
- }
- break;
- }
- GUI.color = tmp;
- foreach (var key in keyValues.Keys)
- {
- foreach (var n in nodes)
- {
- n.SetValue(key, keyValues[key]);
- }
- }
- //EditorUtility.SetDirty(target);
- serializedObject.ApplyModifiedProperties();
- }
- }
|