10using System.Collections.Generic;
19 private List<IDrawable> _drawables;
20 private SerializedProperty _property;
22 private string _onGuiSampleName;
23 private string _getHeightSampleName;
26 _onGuiSampleName =
"OnGUI for " + GetType().Name;
27 _getHeightSampleName =
"GetPropertyHeight for " + GetType().Name;
30 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
34 foreach (var drawable
in _drawables) {
35 drawable.Draw(ref position);
45 foreach (var drawable
in _drawables) {
46 if (drawable is PropertyContainer) {
47 height += ((PropertyContainer)drawable).getHeight();
55 protected virtual void init(SerializedProperty property) {
56 if (_property == property) {
60 _drawables =
new List<IDrawable>();
65 SerializedProperty property, condition;
70 _drawables.Add(
new PropertyContainer() {
72 if (condition.boolValue) {
73 EditorGUI.PropertyField(rect, property, includeChildren);
77 return condition.boolValue ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
83 SerializedProperty property;
88 _drawables.Add(
new PropertyContainer() {
91 EditorGUI.PropertyField(rect, property, includeChildren);
95 return condition() ? EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren) : 0;
100 protected void drawProperty(
string name,
bool includeChildren =
true,
bool disable =
false) {
101 SerializedProperty property;
106 GUIContent content =
new GUIContent(property.displayName, property.tooltip);
107 _drawables.Add(
new PropertyContainer() {
109 EditorGUI.BeginDisabledGroup(disable);
110 EditorGUI.PropertyField(rect, property, content, includeChildren);
111 EditorGUI.EndDisabledGroup();
113 getHeight = () => EditorGUI.GetPropertyHeight(property, GUIContent.none, includeChildren)
117 protected void drawProperty(
string name, Func<string> nameFunc,
bool includeChildren =
true) {
118 SerializedProperty property;
123 GUIContent content =
new GUIContent(nameFunc(), property.tooltip);
125 _drawables.Add(
new PropertyContainer() {
127 content.text = nameFunc() ??
property.displayName;
128 EditorGUI.PropertyField(rect, property, content, includeChildren);
130 getHeight = () => EditorGUI.GetPropertyHeight(property, content, includeChildren)
134 protected void drawCustom(Action<Rect> drawFunc,
float height) {
135 _drawables.Add(
new PropertyContainer() {
137 getHeight = () => height
141 protected void drawCustom(Action<Rect> drawFunc, Func<float> heightFunc) {
142 _drawables.Add(
new PropertyContainer() {
144 getHeight = heightFunc
149 _drawables.Add(
new IndentDrawable() {
155 _drawables.Add(
new IndentDrawable() {
161 property = _property.FindPropertyRelative(name);
163 if (property ==
null) {
164 Debug.LogWarning(
"Could not find property " + name +
", was it renamed or removed?");
172 if (_property.FindPropertyRelative(name) ==
null) {
173 Debug.LogWarning(
"Could not find property " + name +
", was it renamed or removed?");
180 private interface IDrawable {
181 void Draw(ref Rect rect);
184 private struct PropertyContainer : IDrawable {
185 public Action<Rect> draw;
186 public Func<float> getHeight;
188 public void Draw(ref Rect rect) {
189 rect.height = getHeight();
191 rect.y += rect.height;
195 private struct IndentDrawable : IDrawable {
198 public void Draw(ref Rect rect) {
200 rect.width -= indent;
virtual void init(SerializedProperty property)
CustomPropertyDrawerBase()
override float GetPropertyHeight(SerializedProperty property, GUIContent label)
void drawProperty(string name, bool includeChildren=true, bool disable=false)
void drawProperty(string name, Func< string > nameFunc, bool includeChildren=true)
void drawCustom(Action< Rect > drawFunc, Func< float > heightFunc)
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
bool validateProperty(string name)
void drawCustom(Action< Rect > drawFunc, float height)
const float INDENT_AMOUNT
bool tryGetProperty(string name, out SerializedProperty property)
void drawPropertyConditionally(string propertyName, Func< bool > condition, bool includeChildren=true)
void drawPropertyConditionally(string propertyName, string conditionalName, bool includeChildren=true)
A utility struct for ease of use when you want to wrap a piece of code in a Profiler....