Made static fields have different values

This commit is contained in:
2026-01-25 14:50:19 +00:00
parent 4d22a379ab
commit eacff3fc65
4 changed files with 33 additions and 48 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,12 +41,11 @@ 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)
{ {
@@ -54,11 +53,11 @@ namespace PashaBibko.Pacore.Editor.Drawers
} }
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.LabelField("Static Fields", EditorStyles.boldLabel); EditorGUILayout.LabelField("Static Fields (only applied at runtime)", 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);
} }
} }

View File

@@ -134,8 +134,8 @@ GameObject:
- component: {fileID: 330585545} - component: {fileID: 330585545}
- component: {fileID: 330585544} - component: {fileID: 330585544}
- component: {fileID: 330585548} - component: {fileID: 330585548}
- component: {fileID: 330585547}
- component: {fileID: 330585549} - component: {fileID: 330585549}
- component: {fileID: 330585547}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
@@ -273,9 +273,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
go: {fileID: 0} TestValue: 12122
Spawnable: PashaBibko.Pacore.Editor.Drawers.DetectInspectorChangesDrawer
Test: 0
--- !u!114 &330585549 --- !u!114 &330585549
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -288,9 +286,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
go: {fileID: 0} TestValue: 0
Spawnable:
Test: 0
--- !u!1 &410087039 --- !u!1 &410087039
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -1,37 +1,16 @@
using PashaBibko.Pacore.Attributes; using PashaBibko.Pacore.Attributes;
using PashaBibko.Pacore.Threading;
using UnityEngine; using UnityEngine;
using System;
[CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour [CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour
{ {
[InspectorReadOnly, SerializeField] private GameObject go; public int TestValue;
[MonoScript(inherited: typeof(MonoBehaviour))] public string Spawnable;
[DetectInspectorChanges("OnTestChange")]
public int Test;
[StaticInspectorField] private static string StaticText; [StaticInspectorField] private static string StaticText;
[StaticInspectorField] private static string OtherStaticText; [StaticInspectorField] private static string OtherStaticText;
[StaticInspectorField] private static int StaticInt;
private void OnTestChange() => LogTestValue(); [InspectorCallable(nameof(PrintStaticField))] public void PrintStaticField()
[InspectorCallable(nameof(LogSpawnableType))]
public void LogSpawnableType()
{ {
Debug.Log(Spawnable); Debug.Log(StaticText);
}
[InspectorCallable("Test button")] public void LogTestValue()
{
try
{
ThreadSafe.EnforceBackgroundThread();
}
catch (Exception err)
{
Debug.LogError(err.Message);
}
} }
} }