Eonirr

Untitled

Nov 28th, 2025
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3.  
  4. [AttributeUsage(AttributeTargets.Field)]
  5. public class ListableAttribute : PropertyAttribute
  6. {
  7. public Type acceptedType;
  8.  
  9. public ListableAttribute(Type type)
  10. {
  11. acceptedType = type;
  12. }
  13. }
  14.  
  15.  
  16. ----------------------------------
  17.  
  18. using UnityEditor;
  19. using UnityEngine;
  20. using System.Reflection;
  21.  
  22. [CustomEditor(typeof(TestManager))]
  23. public class TestManagerEditor : Editor
  24. {
  25. private bool showList = true;
  26.  
  27. public override void OnInspectorGUI()
  28. {
  29. TestManager tm = (TestManager)target;
  30.  
  31. DrawDefaultInspector();
  32.  
  33. GUILayout.Space(10);
  34.  
  35. showList = EditorGUILayout.Foldout(showList, "Listable Fields");
  36.  
  37. if (showList)
  38. {
  39. FieldInfo[] fields = typeof(TestManager).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
  40.  
  41. foreach (var field in fields)
  42. {
  43. var attr = field.GetCustomAttribute(typeof(ListableAttribute)) as ListableAttribute;
  44.  
  45. if (attr != null)
  46. {
  47. if (attr.acceptedType.IsAssignableFrom(field.FieldType))
  48. {
  49. object value = field.GetValue(tm);
  50. object newValue = EditorGUILayout.ObjectField(field.Name, value as UnityEngine.Object, field.FieldType, true);
  51.  
  52. if (newValue != value)
  53. {
  54. Undo.RecordObject(tm, "Modify Field");
  55. field.SetValue(tm, newValue);
  56. EditorUtility.SetDirty(tm);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment