-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnimDropDownAttribute.cs
82 lines (77 loc) · 1.87 KB
/
UnimDropDownAttribute.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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class UnimDropDownAttribute : PropertyAttribute
{
public string[] enumNames;
public bool UseDefaultTagFieldDrawer = false;
public UnimDropDownAttribute(Type enumType)
{
enumNames = Enum.GetNames(enumType);
}
}
[CustomPropertyDrawer(typeof(UnimDropDownAttribute))]
public class UnimDropDownPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.String)
{
EditorGUI.BeginProperty(position, label, property);
var attrib = attribute as UnimDropDownAttribute;
if (attrib == null)
{
return;
}
if (attrib.UseDefaultTagFieldDrawer)
{
property.stringValue = EditorGUI.TagField(position, label, property.stringValue);
}
else
{
List<string> tagList = attrib.enumNames.ToList();
tagList.AddRange(UnityEditorInternal.InternalEditorUtility.tags);
string propertyString = property.stringValue;
int index = -1;
if (propertyString == "")
{
//The tag is empty
index = 0; //first index is the special <notag> entry
}
else
{
for (int i = 1; i < tagList.Count; i++)
{
if (tagList[i] == propertyString)
{
index = i;
break;
}
}
}
//Draw the popup box with the current selected index
index = EditorGUI.Popup(position, label.text, index, tagList.ToArray());
//Adjust the actual string value of the property based on the selection
if (index == 0)
{
property.stringValue = "";
}
else if (index >= 1)
{
property.stringValue = tagList[index];
}
else
{
property.stringValue = "";
}
}
EditorGUI.EndProperty();
}
else
{
EditorGUI.PropertyField(position, property, label);
}
}
}