-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSBCustomTypePropertyDrawer.cs
More file actions
91 lines (72 loc) · 3.1 KB
/
SBCustomTypePropertyDrawer.cs
File metadata and controls
91 lines (72 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
//Original version of the ConditionalHideAttribute created by Brecht Lecluyse (www.brechtos.com)
//Modified by: Quin Kennedy
[CustomPropertyDrawer(typeof(SBCustomTypeAttribute))]
public class SBCustomTypePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SBCustomTypeAttribute condHAtt = (SBCustomTypeAttribute)attribute;
bool enabled = GetConditionalHideAttributeResult(condHAtt, property);
bool wasEnabled = GUI.enabled;
GUI.enabled = enabled;
if (enabled)
{
EditorGUI.PropertyField(position, property, label, true);
}
GUI.enabled = wasEnabled;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
SBCustomTypeAttribute condHAtt = (SBCustomTypeAttribute)attribute;
bool enabled = GetConditionalHideAttributeResult(condHAtt, property);
if (enabled)
{
return EditorGUI.GetPropertyHeight(property, label);
}
else
{
//The property is not being drawn
//We want to undo the spacing added before and after the property
return -EditorGUIUtility.standardVerticalSpacing;
//return 0.0f;
}
/*
//Get the base height when not expanded
var height = base.GetPropertyHeight(property, label);
// if the property is expanded go through all its children and get their height
if (property.isExpanded)
{
var propEnum = property.GetEnumerator();
while (propEnum.MoveNext())
height += EditorGUI.GetPropertyHeight((SerializedProperty)propEnum.Current, GUIContent.none, true);
}
return height;*/
}
private bool GetConditionalHideAttributeResult(SBCustomTypeAttribute condHAtt, SerializedProperty property)
{
bool enabled = true;
//Handle primary property
SerializedProperty sourcePropertyValue = null;
//Get the full relative property path of the sourcefield so we can have nested hiding.
string propertyPath = property.propertyPath; //returns the property path of the property we want to apply the attribute to
string conditionPath = propertyPath.Replace(property.name, condHAtt.ConditionalSourceField); //changes the path to the conditionalsource property path
sourcePropertyValue = property.serializedObject.FindProperty(conditionPath);
if (sourcePropertyValue != null)
{
SpacebrewClient.type pubTypeValue = (SpacebrewClient.type)sourcePropertyValue.enumValueIndex;
enabled = pubTypeValue == SpacebrewClient.type.CUSTOM;
}
else
{
Debug.LogWarning("Attempting to use a ConditionalHideAttribute but no matching SourcePropertyValue found in object: " + condHAtt.ConditionalSourceField);
}
//wrap it all up
if (condHAtt.Inverse) enabled = !enabled;
return enabled;
}
}
#endif