Advertisement
dimmpixeye

Untitled

May 3rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEditor;
  7. using UnityEngine;
  8.  
  9. namespace Homebrew
  10. {
  11.     [CustomPropertyDrawer(typeof(TagFilterAttribute))]
  12.     public class TagFilterPropertyDrawer : PropertyDrawer
  13.     {
  14.         public int currentIndex;
  15.         public FastString fs = new FastString(128);
  16.         public List<FieldInfo> fields = new List<FieldInfo>();
  17.  
  18.         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  19.         {
  20.             EditorGUI.BeginProperty(position, label, property);
  21.             EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
  22.  
  23.             var tagFilter = attribute as TagFilterAttribute;
  24.             var tagType = tagFilter.Type;
  25.  
  26.             var objectFields = ReturnConst(tagType);
  27.  
  28.             var listNames = new List<string>();
  29.             fields.Clear();
  30.  
  31.             var vv = property.intValue;
  32.  
  33.             for (var i = 0; i < objectFields.Length; i++)
  34.             {
  35.                 var myFieldInfo = objectFields[i];
  36.                 var tagField = Attribute.GetCustomAttribute(objectFields[i], typeof(TagFieldAttribute)) as TagFieldAttribute;
  37.  
  38.                 if (tagField == null) continue;
  39.                 fs.Append(tagField.categoryName).Append("/").Append(myFieldInfo.Name);
  40.                 listNames.Add(fs.ToString());
  41.                 fs.Clear();
  42.                 fields.Add(myFieldInfo);
  43.                 if (vv == (int) myFieldInfo.GetValue(this))
  44.                     currentIndex = fields.Count - 1;
  45.             }
  46.  
  47.             if (listNames.Count == 0)
  48.             {
  49.                 EditorGUI.EndProperty();
  50.                 return;
  51.             }
  52.  
  53.             currentIndex = EditorGUI.Popup(position, property.displayName, currentIndex, listNames.ToArray());
  54.  
  55.  
  56.             var raw = listNames[currentIndex].Split('/');
  57.             var name = raw[raw.Length - 1];
  58.  
  59.             var field = fields.Find(f => f.Name == name);
  60.             property.intValue = (int) field.GetValue(this);
  61.  
  62.             EditorGUI.EndProperty();
  63.         }
  64.  
  65.  
  66.         FieldInfo[] ReturnConst(Type t)
  67.         {
  68.             ArrayList constants = new ArrayList();
  69.  
  70.             FieldInfo[] fieldInfos = t.GetFields(
  71.                 // Gets all public and static fields
  72.                 BindingFlags.Public | BindingFlags.Static |
  73.                 // This tells it to get the fields from all base types as well
  74.                 BindingFlags.FlattenHierarchy);
  75.  
  76.             // Go through the list and only pick out the constants
  77.             foreach (FieldInfo fi in fieldInfos)
  78.                 // IsLiteral determines if its value is written at
  79.                 //   compile time and not changeable
  80.                 // IsInitOnly determine if the field can be set
  81.                 //   in the body of the constructor
  82.                 // for C# a field which is readonly keyword would have both true
  83.                 //   but a const field would have only IsLiteral equal to true
  84.                 if (fi.IsLiteral && !fi.IsInitOnly)
  85.                     constants.Add(fi);
  86.  
  87.             // Return an array of FieldInfos
  88.             return (FieldInfo[]) constants.ToArray(typeof(FieldInfo));
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement