Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System;
- [AttributeUsage(AttributeTargets.Field)]
- public class ListableAttribute : PropertyAttribute
- {
- public Type acceptedType;
- public ListableAttribute(Type type)
- {
- acceptedType = type;
- }
- }
- ----------------------------------
- using UnityEditor;
- using UnityEngine;
- using System.Reflection;
- [CustomEditor(typeof(TestManager))]
- public class TestManagerEditor : Editor
- {
- private bool showList = true;
- public override void OnInspectorGUI()
- {
- TestManager tm = (TestManager)target;
- DrawDefaultInspector();
- GUILayout.Space(10);
- showList = EditorGUILayout.Foldout(showList, "Listable Fields");
- if (showList)
- {
- FieldInfo[] fields = typeof(TestManager).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
- foreach (var field in fields)
- {
- var attr = field.GetCustomAttribute(typeof(ListableAttribute)) as ListableAttribute;
- if (attr != null)
- {
- if (attr.acceptedType.IsAssignableFrom(field.FieldType))
- {
- object value = field.GetValue(tm);
- object newValue = EditorGUILayout.ObjectField(field.Name, value as UnityEngine.Object, field.FieldType, true);
- if (newValue != value)
- {
- Undo.RecordObject(tm, "Modify Field");
- field.SetValue(tm, newValue);
- EditorUtility.SetDirty(tm);
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment