From 5099e4ab2ebf2ce2259e96a7bba9aba042d2d69f Mon Sep 17 00:00:00 2001 From: Pasha Date: Sun, 25 Jan 2026 14:50:19 +0000 Subject: [PATCH] Made static fields have different values --- .../Caches/StaticInspectorFieldCache.cs | 27 +++++++++++++------ .../Editor/Drawers/MonoBehaviourDrawer.cs | 11 ++++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs index 2982bd6..c567cd2 100644 --- a/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs +++ b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs @@ -7,24 +7,30 @@ namespace PashaBibko.Pacore.Editor.Caches { public static class StaticInspectorFieldCache { + public struct FieldData + { + public FieldInfo Info; + public object Value; + } + private const BindingFlags BINDING_FLAGS = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; - private static Dictionary Cache { get; } = new(); + private static Dictionary Cache { get; } = new(); - public static FieldInfo[] GetAllFieldsOfType(Type type) + public static FieldData[] GetAllFieldsOfType(Type type) { /* Checks the cache for the type */ - if (Cache.TryGetValue(type, out FieldInfo[] fields)) + if (Cache.TryGetValue(type, out FieldData[] cached)) { - return fields; + return cached; } /* Else finds all the fields with the attribute */ - fields = type.GetFields(BINDING_FLAGS); - List instances = new(); + FieldInfo[] fields = type.GetFields(BINDING_FLAGS); + List instances = new(); foreach (FieldInfo field in fields) { @@ -33,12 +39,17 @@ namespace PashaBibko.Pacore.Editor.Caches if (attribute != null) { - instances.Add(field); + FieldData data = new() + { + Info = field + }; + + instances.Add(data); } } /* Adds the fields to the cache before returning */ - FieldInfo[] array = instances.ToArray(); + FieldData[] array = instances.ToArray(); Cache.Add(type, array); return array; } diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs index a00667c..dc4ba20 100644 --- a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -41,24 +41,23 @@ namespace PashaBibko.Pacore.Editor.Drawers } } - private static object value; - public static void DrawStaticFields(Object target) { Type type = target.GetType(); - FieldInfo[] fields = StaticInspectorFieldCache.GetAllFieldsOfType(type); + StaticInspectorFieldCache.FieldData[] fields + = StaticInspectorFieldCache.GetAllFieldsOfType(type); if (fields.Length == 0) { return; } - + EditorGUILayout.Space(); EditorGUILayout.LabelField("Static Fields", EditorStyles.boldLabel); - foreach (FieldInfo field in fields) + for (int i = 0; i < fields.Length; i++) { - value = DrawStaticField(field, value); + fields[i].Value = DrawStaticField(fields[i].Info, fields[i].Value); } }