diff --git a/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs index 246dc9b..806a4e7 100644 --- a/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs +++ b/Assets/Pacore/Editor/Caches/InspectorCallableCache.cs @@ -5,7 +5,7 @@ using System; namespace PashaBibko.Pacore.Editor.Caches { - public class InspectorCallableCache + public static class InspectorCallableCache { public struct AttributeInfo { @@ -20,7 +20,7 @@ namespace PashaBibko.Pacore.Editor.Caches private static Dictionary Cache { get; } = new(); - public static AttributeInfo[] GetAllAttributes(Type type) + public static AttributeInfo[] GetAllAttributesOfType(Type type) { /* Checks the cache for the type */ if (Cache.TryGetValue(type, out AttributeInfo[] attributes)) @@ -34,7 +34,9 @@ namespace PashaBibko.Pacore.Editor.Caches foreach (MethodInfo method in methods) { - InspectorCallableAttribute attribute = method.GetCustomAttribute(); + InspectorCallableAttribute attribute + = method.GetCustomAttribute(); + if (attribute != null) { AttributeInfo info = new() diff --git a/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs new file mode 100644 index 0000000..2982bd6 --- /dev/null +++ b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs @@ -0,0 +1,46 @@ +using PashaBibko.Pacore.Attributes; +using System.Collections.Generic; +using System.Reflection; +using System; + +namespace PashaBibko.Pacore.Editor.Caches +{ + public static class StaticInspectorFieldCache + { + private const BindingFlags BINDING_FLAGS = + BindingFlags.Static | + BindingFlags.NonPublic | + BindingFlags.Public; + + private static Dictionary Cache { get; } = new(); + + public static FieldInfo[] GetAllFieldsOfType(Type type) + { + /* Checks the cache for the type */ + if (Cache.TryGetValue(type, out FieldInfo[] fields)) + { + return fields; + } + + /* Else finds all the fields with the attribute */ + fields = type.GetFields(BINDING_FLAGS); + List instances = new(); + + foreach (FieldInfo field in fields) + { + StaticInspectorFieldAttribute attribute + = field.GetCustomAttribute(); + + if (attribute != null) + { + instances.Add(field); + } + } + + /* Adds the fields to the cache before returning */ + FieldInfo[] array = instances.ToArray(); + Cache.Add(type, array); + return array; + } + } +} \ No newline at end of file diff --git a/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs.meta b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs.meta new file mode 100644 index 0000000..e890ecf --- /dev/null +++ b/Assets/Pacore/Editor/Caches/StaticInspectorFieldCache.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7c0d83f0696f4a8d92f0ad89514645f4 +timeCreated: 1769350289 \ No newline at end of file diff --git a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs index 36bb636..a00667c 100644 --- a/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs +++ b/Assets/Pacore/Editor/Drawers/MonoBehaviourDrawer.cs @@ -2,6 +2,7 @@ using UnityEditor; using UnityEngine; using System; +using System.Reflection; using Object = UnityEngine.Object; namespace PashaBibko.Pacore.Editor.Drawers @@ -13,13 +14,14 @@ namespace PashaBibko.Pacore.Editor.Drawers { DrawDefaultInspector(); DrawFunctionButtons(target); + DrawStaticFields(target); } public static void DrawFunctionButtons(Object target) { Type type = target.GetType(); InspectorCallableCache.AttributeInfo[] buttons - = InspectorCallableCache.GetAllAttributes(type); + = InspectorCallableCache.GetAllAttributesOfType(type); if (buttons.Length == 0) { @@ -38,5 +40,47 @@ namespace PashaBibko.Pacore.Editor.Drawers } } } + + private static object value; + + public static void DrawStaticFields(Object target) + { + Type type = target.GetType(); + FieldInfo[] fields = StaticInspectorFieldCache.GetAllFieldsOfType(type); + + if (fields.Length == 0) + { + return; + } + + EditorGUILayout.Space(); + EditorGUILayout.LabelField("Static Fields", EditorStyles.boldLabel); + + foreach (FieldInfo field in fields) + { + value = DrawStaticField(field, value); + } + } + + private static object DrawStaticField(FieldInfo field, object current) + { + Type type = field.FieldType; + string label = field.Name; + + if (type == typeof(int)) + return EditorGUILayout.IntField(label, current as int? ?? 0); + + if (type == typeof(float)) + return EditorGUILayout.FloatField(label, current as float? ?? 0f); + + if (type == typeof(string)) + return EditorGUILayout.TextField(label, current as string ?? ""); + + if (type == typeof(bool)) + return EditorGUILayout.Toggle(label, current as bool? ?? false); + + EditorGUILayout.LabelField(label, $"Unsupported type: {type.FullName}"); + return current; + } } } \ No newline at end of file diff --git a/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs b/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs new file mode 100644 index 0000000..3146e73 --- /dev/null +++ b/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs @@ -0,0 +1,9 @@ +using System; + +namespace PashaBibko.Pacore.Attributes +{ + [AttributeUsage(validOn: AttributeTargets.Field)] + public class StaticInspectorFieldAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs.meta b/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs.meta new file mode 100644 index 0000000..3f2a785 --- /dev/null +++ b/Assets/Pacore/Runtime/Attributes/StaticInspectorField.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f4bdb9ab5a544b57a8e8ec1dae246433 +timeCreated: 1769349847 \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 1433e06..850138d 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -135,6 +135,7 @@ GameObject: - component: {fileID: 330585544} - component: {fileID: 330585548} - component: {fileID: 330585547} + - component: {fileID: 330585549} m_Layer: 0 m_Name: Main Camera m_TagString: MainCamera @@ -275,6 +276,21 @@ MonoBehaviour: go: {fileID: 0} Spawnable: PashaBibko.Pacore.Editor.Drawers.DetectInspectorChangesDrawer Test: 0 +--- !u!114 &330585549 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 330585543} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a411a69d284bfd04bafc210a81b43e88, type: 3} + m_Name: + m_EditorClassIdentifier: + go: {fileID: 0} + Spawnable: + Test: 0 --- !u!1 &410087039 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/TestMonoBehaviour.cs b/Assets/TestMonoBehaviour.cs index 1d9167b..ba3b21e 100644 --- a/Assets/TestMonoBehaviour.cs +++ b/Assets/TestMonoBehaviour.cs @@ -1,9 +1,7 @@ -using System; -using System.Threading; using PashaBibko.Pacore.Attributes; -using PashaBibko.Pacore.DevTools; using PashaBibko.Pacore.Threading; using UnityEngine; +using System; [CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour { @@ -14,6 +12,9 @@ using UnityEngine; [DetectInspectorChanges("OnTestChange")] public int Test; + [StaticInspectorField] private static string StaticText; + [StaticInspectorField] private static string OtherStaticText; + private void OnTestChange() => LogTestValue(); [InspectorCallable(nameof(LogSpawnableType))]