BaseGraphEditor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 BaseGraphEditor : 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 BaseGraph m_BaseGraph;
  19. public static T AddUIComponent<T>(string chartName) where T : BaseGraph
  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_BaseGraph = (BaseGraph)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. OnDebugStartInspectorGUI();
  42. OnDebugEndInspectorGUI();
  43. }
  44. protected virtual void OnDebugStartInspectorGUI() { }
  45. protected virtual void OnDebugEndInspectorGUI() { }
  46. protected void PropertyField(string name)
  47. {
  48. if (!m_Properties.ContainsKey(name))
  49. {
  50. var prop = serializedObject.FindProperty(name);
  51. if (prop == null)
  52. {
  53. Debug.LogError("Property " + name + " not found!");
  54. return;
  55. }
  56. m_Properties.Add(name, prop);
  57. }
  58. EditorGUILayout.PropertyField(m_Properties[name]);
  59. }
  60. protected void PropertyField(SerializedProperty property)
  61. {
  62. Assert.IsNotNull(property);
  63. var title = ChartEditorHelper.GetContent(property.displayName);
  64. PropertyField(property, title);
  65. }
  66. protected void PropertyField(SerializedProperty property, GUIContent title)
  67. {
  68. EditorGUILayout.PropertyField(property, title);
  69. }
  70. protected void PropertyListField(string relativePropName, bool showOrder = true, params HeaderMenuInfo[] menus)
  71. {
  72. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  73. var height = 0f;
  74. var prop = FindProperty(relativePropName);
  75. prop.isExpanded = ChartEditorHelper.MakeListWithFoldout(ref m_DrawRect, ref height,
  76. prop, prop.isExpanded, showOrder, true, menus);
  77. if (prop.isExpanded)
  78. {
  79. GUILayoutUtility.GetRect(1f, height - 17);
  80. }
  81. }
  82. protected void PropertyTwoFiled(string relativePropName)
  83. {
  84. var m_DrawRect = GUILayoutUtility.GetRect(1f, 17f);
  85. var prop = FindProperty(relativePropName);
  86. ChartEditorHelper.MakeTwoField(ref m_DrawRect, m_DrawRect.width, prop, prop.displayName);
  87. }
  88. protected SerializedProperty FindProperty(string path)
  89. {
  90. if (!m_Properties.ContainsKey(path))
  91. {
  92. m_Properties.Add(path, serializedObject.FindProperty(path));
  93. }
  94. return m_Properties[path];
  95. }
  96. }
  97. }