-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntdashAttributes.cs
More file actions
160 lines (139 loc) · 4.95 KB
/
Copy pathIntdashAttributes.cs
File metadata and controls
160 lines (139 loc) · 4.95 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class IntdashLabelAttribute : PropertyAttribute
{
public readonly string DisplayName;
public IntdashLabelAttribute(string displayName)
{
DisplayName = displayName;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(IntdashLabelAttribute))]
public class IscpLabelDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var newLabel = attribute as IntdashLabelAttribute;
EditorGUI.PropertyField(position, property, new GUIContent(newLabel.DisplayName), true);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, true);
}
}
#endif
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class IntdashEnumItemAttribute : Attribute
{
public string DisplayName { get; private set; }
public IntdashEnumItemAttribute(string displayName)
{
DisplayName = displayName;
}
}
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class IntdashEnumAttribute : PropertyAttribute
{
public Type Type { get; private set; }
public IntdashEnumAttribute(Type selfType)
{
Type = selfType;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(IntdashEnumAttribute))]
public class IntdashEnumItemDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = attribute as IntdashEnumAttribute;
var names = new List<string>();
foreach (var fi in attr.Type.GetFields())
{
if (fi.IsSpecialName)
{
continue;
}
var elementAttribute = fi.GetCustomAttributes(typeof(IntdashEnumItemAttribute), false).FirstOrDefault() as IntdashEnumItemAttribute;
names.Add(elementAttribute == null ? fi.Name : elementAttribute.DisplayName);
}
var values = Enum.GetValues(attr.Type).Cast<int>();
property.intValue = EditorGUI.IntPopup(position, property.displayName, property.intValue, names.ToArray(), values.ToArray());
}
}
#endif
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(IntdashVisiblityAttribute))]
internal sealed class IntdashVisiblityPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = base.attribute as IntdashVisiblityAttribute;
object value = null;
var prop = property.serializedObject.FindProperty(attr.VariableName);
if (prop == null)
{
Debug.LogError($"Not found '{attr.VariableName}' property");
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndDisabledGroup();
return;
}
var targetObject = prop.serializedObject.targetObject;
var type = targetObject.GetType();
var field = type.GetField(prop.propertyPath);
value = field.GetValue(targetObject);
var enable = value.Equals(attr.EnableValue);
if (attr.Invisible && !enable)
{
return;
}
EditorGUI.BeginDisabledGroup(!enable);
EditorGUI.PropertyField(position, property, label, true);
EditorGUI.EndDisabledGroup();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var attr = base.attribute as IntdashVisiblityAttribute;
var prop = property.serializedObject.FindProperty(attr.VariableName);
if (prop == null)
{
return EditorGUI.GetPropertyHeight(property, true);
}
var targetObject = prop.serializedObject.targetObject;
var type = targetObject.GetType();
var field = type.GetField(prop.propertyPath);
var value = field.GetValue(targetObject);
var enable = value.Equals(attr.EnableValue);
if (attr.Invisible && !enable)
{
return -EditorGUIUtility.standardVerticalSpacing;
}
return EditorGUI.GetPropertyHeight(property, true);
}
private bool IsEnable(IntdashVisiblityAttribute attr, object enableValue)
{
return attr.EnableValue == enableValue;
}
}
#endif
public class IntdashVisiblityAttribute : PropertyAttribute
{
public readonly string VariableName;
public readonly object EnableValue;
public readonly bool Disable;
public readonly bool Invisible;
public IntdashVisiblityAttribute(string variableName, object enableValue, bool disable = false, bool invisible = false)
{
this.VariableName = variableName;
this.EnableValue = enableValue;
this.Disable = disable;
this.Invisible = invisible;
}
}