Made static fields have different values

This commit is contained in:
2026-01-25 14:50:19 +00:00
parent 4d22a379ab
commit 5099e4ab2e
2 changed files with 24 additions and 14 deletions

View File

@@ -7,24 +7,30 @@ namespace PashaBibko.Pacore.Editor.Caches
{ {
public static class StaticInspectorFieldCache public static class StaticInspectorFieldCache
{ {
public struct FieldData
{
public FieldInfo Info;
public object Value;
}
private const BindingFlags BINDING_FLAGS = private const BindingFlags BINDING_FLAGS =
BindingFlags.Static | BindingFlags.Static |
BindingFlags.NonPublic | BindingFlags.NonPublic |
BindingFlags.Public; BindingFlags.Public;
private static Dictionary<Type, FieldInfo[]> Cache { get; } = new(); private static Dictionary<Type, FieldData[]> Cache { get; } = new();
public static FieldInfo[] GetAllFieldsOfType(Type type) public static FieldData[] GetAllFieldsOfType(Type type)
{ {
/* Checks the cache for the 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 */ /* Else finds all the fields with the attribute */
fields = type.GetFields(BINDING_FLAGS); FieldInfo[] fields = type.GetFields(BINDING_FLAGS);
List<FieldInfo> instances = new(); List<FieldData> instances = new();
foreach (FieldInfo field in fields) foreach (FieldInfo field in fields)
{ {
@@ -33,12 +39,17 @@ namespace PashaBibko.Pacore.Editor.Caches
if (attribute != null) if (attribute != null)
{ {
instances.Add(field); FieldData data = new()
{
Info = field
};
instances.Add(data);
} }
} }
/* Adds the fields to the cache before returning */ /* Adds the fields to the cache before returning */
FieldInfo[] array = instances.ToArray(); FieldData[] array = instances.ToArray();
Cache.Add(type, array); Cache.Add(type, array);
return array; return array;
} }

View File

@@ -41,24 +41,23 @@ namespace PashaBibko.Pacore.Editor.Drawers
} }
} }
private static object value;
public static void DrawStaticFields(Object target) public static void DrawStaticFields(Object target)
{ {
Type type = target.GetType(); Type type = target.GetType();
FieldInfo[] fields = StaticInspectorFieldCache.GetAllFieldsOfType(type); StaticInspectorFieldCache.FieldData[] fields
= StaticInspectorFieldCache.GetAllFieldsOfType(type);
if (fields.Length == 0) if (fields.Length == 0)
{ {
return; return;
} }
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.LabelField("Static Fields", EditorStyles.boldLabel); 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);
} }
} }