暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UIComponentEditor.cs 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.Assertions;
  5. using XCharts.Runtime;
  6. namespace XCharts.Editor
  7. {
  8. public class UIComponentEditor : UnityEditor.Editor
  9. {
  10. class Styles
  11. {
  12. public static readonly GUIContent btnAddComponent = new GUIContent("Add Main Component", "");
  13. public static readonly GUIContent btnRebuildChartObject = new GUIContent("Rebuild Object", "");
  14. public static readonly GUIContent btnSaveAsImage = new GUIContent("Save As Image", "");
  15. public static readonly GUIContent btnCheckWarning = new GUIContent("Check Warning", "");
  16. public static readonly GUIContent btnHideWarning = new GUIContent("Hide Warning", "");
  17. }
  18. public UIComponent m_UIComponent;
  19. public static T AddUIComponent<T>(string chartName) where T : UIComponent
  20. {
  21. return XChartsEditor.AddGraph<T>(chartName);
  22. }
  23. protected Dictionary<string, SerializedProperty> m_Properties = new Dictionary<string, SerializedProperty>();
  24. protected virtual void OnEnable()
  25. {
  26. m_Properties.Clear();
  27. m_UIComponent = (UIComponent) target;
  28. }
  29. public override void OnInspectorGUI()
  30. {
  31. serializedObject.Update();
  32. PropertyField("m_Script");
  33. OnStartInspectorGUI();
  34. OnDebugInspectorGUI();
  35. serializedObject.ApplyModifiedProperties();
  36. }
  37. protected virtual void OnStartInspectorGUI() { }
  38. protected virtual void OnDebugInspectorGUI()
  39. {
  40. EditorGUILayout.Space();
  41. PropertyField("m_DebugModel");
  42. OnDebugStartInspectorGUI();
  43. if (GUILayout.Button(Styles.btnRebuildChartObject))
  44. {
  45. m_UIComponent.RebuildChartObject();
  46. }
  47. if (GUILayout.Button(Styles.btnSaveAsImage))
  48. {
  49. m_UIComponent.SaveAsImage();
  50. }
  51. OnDebugEndInspectorGUI();
  52. }
  53. protected virtual void OnDebugStartInspectorGUI() { }
  54. protected virtual void OnDebugEndInspectorGUI() { }
  55. protected void PropertyField(string name)
  56. {
  57. if (!m_Properties.ContainsKey(name))
  58. {
  59. var prop = serializedObject.FindProperty(name);
  60. if (prop == null)
  61. {
  62. Debug.LogError("Property " + name + " not found!");
  63. return;
  64. }
  65. m_Properties.Add(name, prop);
  66. }
  67. EditorGUILayout.PropertyField(m_Properties[name]);
  68. }
  69. protected void PropertyField(SerializedProperty property)
  70. {
  71. Assert.IsNotNull(property);
  72. var title = ChartEditorHelper.GetContent(property.displayName);
  73. PropertyField(property, title);
  74. }
  75. protected void PropertyField(SerializedProperty property, GUIContent title)
  76. {
  77. EditorGUILayout.PropertyField(property, title);
  78. }
  79. protected void PropertyListField(string relativePropName, bool showOrder = true, params HeaderMenuInfo[] menus)
  80. {
  81. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  82. var height = 0f;
  83. var prop = FindProperty(relativePropName);
  84. prop.isExpanded = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
  85. prop, prop.isExpanded, showOrder, true, menus);
  86. if (prop.isExpanded)
  87. {
  88. GUILayoutUtility.GetRect(1f, height - 17);
  89. }
  90. }
  91. protected void PropertyTwoFiled(string relativePropName)
  92. {
  93. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  94. var prop = FindProperty(relativePropName);
  95. ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName);
  96. }
  97. protected SerializedProperty FindProperty(string path)
  98. {
  99. if (!m_Properties.ContainsKey(path))
  100. {
  101. m_Properties.Add(path, serializedObject.FindProperty(path));
  102. }
  103. return m_Properties[path];
  104. }
  105. }
  106. }