From eacff3fc65acc6de40abe2210dd05d65967156f8 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 | 15 +++++----- Assets/Scenes/SampleScene.unity | 10 ++----- Assets/TestMonoBehaviour.cs | 29 +++---------------- 4 files changed, 33 insertions(+), 48 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..9a30e33 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) + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Static Fields (only applied at runtime)", EditorStyles.boldLabel); + + for (int i = 0; i < fields.Length; i++) { - value = DrawStaticField(field, value); + fields[i].Value = DrawStaticField(fields[i].Info, fields[i].Value); } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 850138d..54c7db2 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -134,8 +134,8 @@ GameObject: - component: {fileID: 330585545} - component: {fileID: 330585544} - component: {fileID: 330585548} - - component: {fileID: 330585547} - component: {fileID: 330585549} + - component: {fileID: 330585547} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -273,9 +273,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Name: m_EditorClassIdentifier: - go: {fileID: 0} - Spawnable: PashaBibko.Pacore.Editor.Drawers.DetectInspectorChangesDrawer - Test: 0 + TestValue: 12122 --- !u!114 &330585549 MonoBehaviour: m_ObjectHideFlags: 0 @@ -288,9 +286,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} m_Name: m_EditorClassIdentifier: - go: {fileID: 0} - Spawnable: - Test: 0 + TestValue: 0 --- !u!1 &410087039 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index ba3b21e..195cb61 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -1,37 +1,16 @@ using PashaBibko.Pacore.Attributes; -using PashaBibko.Pacore.Threading; using UnityEngine; -using System; [CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour { - [InspectorReadOnly, SerializeField] private GameObject go; - - [MonoScript(inherited: typeof(MonoBehaviour))] public string Spawnable; + public int TestValue; - [DetectInspectorChanges("OnTestChange")] - public int Test; - [StaticInspectorField] private static string StaticText; [StaticInspectorField] private static string OtherStaticText; + [StaticInspectorField] private static int StaticInt; - private void OnTestChange() => LogTestValue(); - - [InspectorCallable(nameof(LogSpawnableType))] - public void LogSpawnableType() + [InspectorCallable(nameof(PrintStaticField))] public void PrintStaticField() { - Debug.Log(Spawnable); - } - - [InspectorCallable("Test button")] public void LogTestValue() - { - try - { - ThreadSafe.EnforceBackgroundThread(); - } - catch (Exception err) - { - Debug.LogError(err.Message); - } + Debug.Log(StaticText); } }