forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefinedValuesAttribute.cs
85 lines (69 loc) · 2.37 KB
/
DefinedValuesAttribute.cs
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
using System;
using System.Linq;
using UnityEngine;
namespace MyBox
{
/// <summary>
/// Create Popup with predefined values for string, int or float property
/// </summary>
public class DefinedValuesAttribute : PropertyAttribute
{
public readonly object[] ValuesArray;
public DefinedValuesAttribute(params object[] definedValues)
{
ValuesArray = definedValues;
}
}
}
#if UNITY_EDITOR
namespace MyBox.Internal
{
using UnityEditor;
[CustomPropertyDrawer(typeof(DefinedValuesAttribute))]
public class DefinedValuesAttributeDrawer : PropertyDrawer
{
private string[] _values;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var values = ((DefinedValuesAttribute) attribute).ValuesArray;
if (values.IsNullOrEmpty() || !TypeMatch(values[0].GetType(), property))
{
EditorGUI.PropertyField(position, property, label);
return;
}
if (_values.IsNullOrEmpty()) _values = values.Select(v => v.ToString()).ToArray();
var valType = values[0].GetType();
bool isString = valType == typeof(string);
bool isInt = valType == typeof(int);
bool isFloat = valType == typeof(float);
EditorGUI.BeginChangeCheck();
var newIndex = EditorGUI.Popup(position, label.text, GetSelectedIndex(), _values);
if (EditorGUI.EndChangeCheck()) ApplyNewValue(_values[newIndex]);
int GetSelectedIndex()
{
for (var i = 0; i < _values.Length; i++)
{
if (isString && property.stringValue == _values[i]) return i;
if (isInt && property.intValue == Convert.ToInt32(_values[i])) return i;
if (isFloat && Mathf.Approximately(property.floatValue, Convert.ToSingle(_values[i]))) return i;
}
return 0;
}
void ApplyNewValue(string newValue)
{
if (isString) property.stringValue = newValue;
if (isInt) property.intValue = Convert.ToInt32(newValue);
if (isFloat) property.floatValue = Convert.ToSingle(newValue);
property.serializedObject.ApplyModifiedProperties();
}
}
private bool TypeMatch(Type valType, SerializedProperty property)
{
if (valType == typeof(string) && property.propertyType == SerializedPropertyType.String) return true;
if (valType == typeof(int) && property.propertyType == SerializedPropertyType.Integer) return true;
if (valType == typeof(float) && property.propertyType == SerializedPropertyType.Float) return true;
return false;
}
}
}
#endif