Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEditor;
- using UnityEngine;
- namespace Homebrew
- {
- [CustomPropertyDrawer(typeof(TagFilterAttribute))]
- public class TagFilterPropertyDrawer : PropertyDrawer
- {
- public int currentIndex;
- public FastString fs = new FastString(128);
- public List<FieldInfo> fields = new List<FieldInfo>();
- public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
- {
- EditorGUI.BeginProperty(position, label, property);
- EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
- var tagFilter = attribute as TagFilterAttribute;
- var tagType = tagFilter.Type;
- var objectFields = ReturnConst(tagType);
- var listNames = new List<string>();
- fields.Clear();
- var vv = property.intValue;
- for (var i = 0; i < objectFields.Length; i++)
- {
- var myFieldInfo = objectFields[i];
- var tagField = Attribute.GetCustomAttribute(objectFields[i], typeof(TagFieldAttribute)) as TagFieldAttribute;
- if (tagField == null) continue;
- fs.Append(tagField.categoryName).Append("/").Append(myFieldInfo.Name);
- listNames.Add(fs.ToString());
- fs.Clear();
- fields.Add(myFieldInfo);
- if (vv == (int) myFieldInfo.GetValue(this))
- currentIndex = fields.Count - 1;
- }
- if (listNames.Count == 0)
- {
- EditorGUI.EndProperty();
- return;
- }
- currentIndex = EditorGUI.Popup(position, property.displayName, currentIndex, listNames.ToArray());
- var raw = listNames[currentIndex].Split('/');
- var name = raw[raw.Length - 1];
- var field = fields.Find(f => f.Name == name);
- property.intValue = (int) field.GetValue(this);
- EditorGUI.EndProperty();
- }
- FieldInfo[] ReturnConst(Type t)
- {
- ArrayList constants = new ArrayList();
- FieldInfo[] fieldInfos = t.GetFields(
- // Gets all public and static fields
- BindingFlags.Public | BindingFlags.Static |
- // This tells it to get the fields from all base types as well
- BindingFlags.FlattenHierarchy);
- // Go through the list and only pick out the constants
- foreach (FieldInfo fi in fieldInfos)
- // IsLiteral determines if its value is written at
- // compile time and not changeable
- // IsInitOnly determine if the field can be set
- // in the body of the constructor
- // for C# a field which is readonly keyword would have both true
- // but a const field would have only IsLiteral equal to true
- if (fi.IsLiteral && !fi.IsInitOnly)
- constants.Add(fi);
- // Return an array of FieldInfos
- return (FieldInfo[]) constants.ToArray(typeof(FieldInfo));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement