LabelSetDrawer.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 NameExcludeString;
  26. private SerializedProperty ComponentScript;
  27. private SerializedProperty Languages;
  28. private SerializedProperty Prefabs;
  29. private SerializedProperty ComponentPurviews;
  30. #endregion
  31. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  32. {
  33. Instance = (LabelSet) InstanceUtility.GetObject(fieldInfo, property);
  34. return Instance.TotalHeight;
  35. }
  36. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  37. {
  38. Instance = (LabelSet) InstanceUtility.GetObject(fieldInfo, property);
  39. Name = property.FindPropertyRelative("Name");
  40. LabelScriptPath = property.FindPropertyRelative("LabelScriptPath");
  41. LabelScriptName = property.FindPropertyRelative("LabelScriptName");
  42. LabePrefix = property.FindPropertyRelative("LabePrefix");
  43. LabelScript = property.FindPropertyRelative("LabelScript");
  44. ComponentScriptPath = property.FindPropertyRelative("ComponentScriptPath");
  45. ComponentScriptName = property.FindPropertyRelative("ComponentScriptName");
  46. ComponentPrefix = property.FindPropertyRelative("ComponentPrefix");
  47. NameExcludeString = property.FindPropertyRelative("NameExcludeString");
  48. ComponentScript = property.FindPropertyRelative("ComponentScript");
  49. Languages = property.FindPropertyRelative("Languages");
  50. Prefabs = property.FindPropertyRelative("Prefabs");
  51. ComponentPurviews = property.FindPropertyRelative("ComponentPurviews");
  52. float originY = position.y;
  53. position.height = LineHeight;
  54. Instance.FoldOut = EditorGUI.Foldout(position, Instance.FoldOut, new GUIContent(Instance.Name));
  55. position.y += LineHeight + LineSpan;
  56. if (Instance.FoldOut)
  57. {
  58. EditorGUI.indentLevel++;
  59. position = DrawProperty(position, Name);
  60. position = DrawProperty(position, LabelScriptPath);
  61. position = DrawProperty(position, LabelScriptName);
  62. position = DrawProperty(position, LabePrefix);
  63. position = DrawProperty(position, LabelScript);
  64. position = DrawProperty(position, ComponentScriptPath);
  65. position = DrawProperty(position, ComponentScriptName);
  66. position = DrawProperty(position, ComponentPrefix);
  67. position = DrawProperty(position, NameExcludeString);
  68. position = DrawProperty(position, ComponentScript);
  69. position = DrawPropertys(position, Languages);
  70. position = DrawPropertys(position, Prefabs);
  71. position = DrawButton(position, "Create Label Script", ()=> {LabelUtility.CreateLabelScript(Instance);});
  72. position = DrawButton(position, "Create Component Script", ()=> { LabelUtility.CreateComponentScript(Instance); });
  73. position = DrawButton(position, "Create Label From Prefab", ()=> { LabelUtility.CreateLabelFromPrefab(Instance); });
  74. position = DrawButton(position, "Create Label From LanguageXml", ()=> { LabelUtility.CreateLabelFromLanguageXml(Instance); }); //todo Ôö¼Ó
  75. position = DrawPropertys(position, ComponentPurviews);
  76. position = DrawButton(position, "Create Components From Prefab", ()=> { LabelUtility.CreateComponentsFromPrefab(Instance); });
  77. }
  78. Instance.TotalHeight = position.y - originY;
  79. }
  80. private Rect DrawButton(Rect position, string name, Action OnClick)
  81. {
  82. Rect contentPosition = position;
  83. contentPosition.height = ButtonHeight;
  84. if (GUI.Button(contentPosition, name))
  85. {
  86. OnClick.Invoke();
  87. }
  88. position.y += ButtonHeight + ButtonSpan;
  89. return position;
  90. }
  91. private Rect DrawProperty(Rect position, SerializedProperty property)
  92. {
  93. Rect contentPosition = position;
  94. contentPosition.height = LineHeight;
  95. contentPosition = EditorGUI.PrefixLabel(contentPosition, new GUIContent(property.displayName));
  96. EditorGUI.PropertyField(contentPosition, property, GUIContent.none);
  97. position.y += LineHeight + LineSpan;
  98. return position;
  99. }
  100. private Rect DrawPropertys(Rect position, SerializedProperty property)
  101. {
  102. Rect contentPosition = position;
  103. contentPosition.height = LineHeight;
  104. contentPosition = EditorGUI.PrefixLabel(contentPosition, new GUIContent(property.displayName));
  105. EditorGUI.PropertyField(contentPosition, property, GUIContent.none, true);
  106. if (property.isExpanded)
  107. {
  108. position.y += (LineHeight + LineSpan)*(property.arraySize + 2);
  109. }
  110. else
  111. {
  112. position.y += LineHeight + LineSpan;
  113. }
  114. return position;
  115. }
  116. }
  117. }