LabelSetDrawer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. namespace labelUtility
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEditor;
  7. using UnityEngine;
  8. [CustomPropertyDrawer(typeof(LabelSet))]
  9. public class LabelSetDrawer : PropertyDrawer
  10. {
  11. #region Config
  12. private float LineSpan = 2;
  13. private float LineHeight = 16;
  14. private float ButtonSpan = 4;
  15. private float ButtonHeight = 32;
  16. private LabelSet Instance;
  17. private SerializedProperty Name;
  18. private SerializedProperty LabelScriptPath;
  19. private SerializedProperty LabelScriptName;
  20. private SerializedProperty LabePrefix;
  21. private SerializedProperty LabelScript;
  22. private SerializedProperty ComponentScriptPath;
  23. private SerializedProperty ComponentScriptName;
  24. private SerializedProperty ComponentPrefix;
  25. private SerializedProperty ComponentScript;
  26. private SerializedProperty Languages;
  27. private SerializedProperty Prefabs;
  28. private SerializedProperty ComponentPurviews;
  29. #endregion
  30. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  31. {
  32. Instance = (LabelSet) InstanceUtility.GetObject(fieldInfo, property);
  33. return Instance.TotalHeight;
  34. }
  35. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  36. {
  37. Instance = (LabelSet) InstanceUtility.GetObject(fieldInfo, property);
  38. Name = property.FindPropertyRelative("Name");
  39. LabelScriptPath = property.FindPropertyRelative("LabelScriptPath");
  40. LabelScriptName = property.FindPropertyRelative("LabelScriptName");
  41. LabePrefix = property.FindPropertyRelative("LabePrefix");
  42. LabelScript = property.FindPropertyRelative("LabelScript");
  43. ComponentScriptPath = property.FindPropertyRelative("ComponentScriptPath");
  44. ComponentScriptName = property.FindPropertyRelative("ComponentScriptName");
  45. ComponentPrefix = property.FindPropertyRelative("ComponentPrefix");
  46. ComponentScript = property.FindPropertyRelative("ComponentScript");
  47. Languages = property.FindPropertyRelative("Languages");
  48. Prefabs = property.FindPropertyRelative("Prefabs");
  49. ComponentPurviews = property.FindPropertyRelative("ComponentPurviews");
  50. float originY = position.y;
  51. position.height = LineHeight;
  52. Instance.FoldOut = EditorGUI.Foldout(position, Instance.FoldOut, new GUIContent(Instance.Name));
  53. position.y += LineHeight + LineSpan;
  54. if (Instance.FoldOut)
  55. {
  56. EditorGUI.indentLevel++;
  57. position = DrawProperty(position, Name);
  58. position = DrawProperty(position, LabelScriptPath);
  59. position = DrawProperty(position, LabelScriptName);
  60. position = DrawProperty(position, LabePrefix);
  61. position = DrawProperty(position, LabelScript);
  62. position = DrawProperty(position, ComponentScriptPath);
  63. position = DrawProperty(position, ComponentScriptName);
  64. position = DrawProperty(position, ComponentPrefix);
  65. position = DrawProperty(position, ComponentScript);
  66. position = DrawPropertys(position, Languages);
  67. position = DrawPropertys(position, Prefabs);
  68. position = DrawButton(position, "Create Label Script", ()=> {LabelUtility.CreateLabelScript(Instance);});
  69. position = DrawButton(position, "Create Component Script", ()=> { LabelUtility.CreateComponentScript(Instance); });
  70. position = DrawButton(position, "Create Label From Prefab", ()=> { LabelUtility.CreateLabelFromPrefab(Instance); });
  71. position = DrawPropertys(position, ComponentPurviews);
  72. position = DrawButton(position, "Create Components From Prefab", ()=> { LabelUtility.CreateComponentsFromPrefab(Instance); });
  73. }
  74. Instance.TotalHeight = position.y - originY;
  75. }
  76. private Rect DrawButton(Rect position, string name, Action OnClick)
  77. {
  78. Rect contentPosition = position;
  79. contentPosition.height = ButtonHeight;
  80. if (GUI.Button(contentPosition, name))
  81. {
  82. OnClick.Invoke();
  83. }
  84. position.y += ButtonHeight + ButtonSpan;
  85. return position;
  86. }
  87. private Rect DrawProperty(Rect position, SerializedProperty property)
  88. {
  89. Rect contentPosition = position;
  90. contentPosition.height = LineHeight;
  91. contentPosition = EditorGUI.PrefixLabel(contentPosition, new GUIContent(property.displayName));
  92. EditorGUI.PropertyField(contentPosition, property, GUIContent.none);
  93. position.y += LineHeight + LineSpan;
  94. return position;
  95. }
  96. private Rect DrawPropertys(Rect position, SerializedProperty property)
  97. {
  98. Rect contentPosition = position;
  99. contentPosition.height = LineHeight;
  100. contentPosition = EditorGUI.PrefixLabel(contentPosition, new GUIContent(property.displayName));
  101. EditorGUI.PropertyField(contentPosition, property, GUIContent.none, true);
  102. if (property.isExpanded)
  103. {
  104. position.y += (LineHeight + LineSpan)*(property.arraySize + 2);
  105. }
  106. else
  107. {
  108. position.y += LineHeight + LineSpan;
  109. }
  110. return position;
  111. }
  112. }
  113. }