Started adding StaticInspectorField

This commit is contained in:
2026-01-25 14:39:20 +00:00
parent f8b49427f1
commit 4d22a379ab
8 changed files with 131 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ using System;
namespace PashaBibko.Pacore.Editor.Caches namespace PashaBibko.Pacore.Editor.Caches
{ {
public class InspectorCallableCache public static class InspectorCallableCache
{ {
public struct AttributeInfo public struct AttributeInfo
{ {
@@ -20,7 +20,7 @@ namespace PashaBibko.Pacore.Editor.Caches
private static Dictionary<Type, AttributeInfo[]> Cache { get; } = new(); private static Dictionary<Type, AttributeInfo[]> Cache { get; } = new();
public static AttributeInfo[] GetAllAttributes(Type type) public static AttributeInfo[] GetAllAttributesOfType(Type type)
{ {
/* Checks the cache for the type */ /* Checks the cache for the type */
if (Cache.TryGetValue(type, out AttributeInfo[] attributes)) if (Cache.TryGetValue(type, out AttributeInfo[] attributes))
@@ -34,7 +34,9 @@ namespace PashaBibko.Pacore.Editor.Caches
foreach (MethodInfo method in methods) foreach (MethodInfo method in methods)
{ {
InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>(); InspectorCallableAttribute attribute
= method.GetCustomAttribute<InspectorCallableAttribute>();
if (attribute != null) if (attribute != null)
{ {
AttributeInfo info = new() AttributeInfo info = new()

View File

@@ -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<Type, FieldInfo[]> 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<FieldInfo> instances = new();
foreach (FieldInfo field in fields)
{
StaticInspectorFieldAttribute attribute
= field.GetCustomAttribute<StaticInspectorFieldAttribute>();
if (attribute != null)
{
instances.Add(field);
}
}
/* Adds the fields to the cache before returning */
FieldInfo[] array = instances.ToArray();
Cache.Add(type, array);
return array;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7c0d83f0696f4a8d92f0ad89514645f4
timeCreated: 1769350289

View File

@@ -2,6 +2,7 @@
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using System; using System;
using System.Reflection;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
namespace PashaBibko.Pacore.Editor.Drawers namespace PashaBibko.Pacore.Editor.Drawers
@@ -13,13 +14,14 @@ namespace PashaBibko.Pacore.Editor.Drawers
{ {
DrawDefaultInspector(); DrawDefaultInspector();
DrawFunctionButtons(target); DrawFunctionButtons(target);
DrawStaticFields(target);
} }
public static void DrawFunctionButtons(Object target) public static void DrawFunctionButtons(Object target)
{ {
Type type = target.GetType(); Type type = target.GetType();
InspectorCallableCache.AttributeInfo[] buttons InspectorCallableCache.AttributeInfo[] buttons
= InspectorCallableCache.GetAllAttributes(type); = InspectorCallableCache.GetAllAttributesOfType(type);
if (buttons.Length == 0) 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;
}
} }
} }

View File

@@ -0,0 +1,9 @@
using System;
namespace PashaBibko.Pacore.Attributes
{
[AttributeUsage(validOn: AttributeTargets.Field)]
public class StaticInspectorFieldAttribute : Attribute
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f4bdb9ab5a544b57a8e8ec1dae246433
timeCreated: 1769349847

View File

@@ -135,6 +135,7 @@ GameObject:
- component: {fileID: 330585544} - component: {fileID: 330585544}
- component: {fileID: 330585548} - component: {fileID: 330585548}
- component: {fileID: 330585547} - component: {fileID: 330585547}
- component: {fileID: 330585549}
m_Layer: 0 m_Layer: 0
m_Name: Main Camera m_Name: Main Camera
m_TagString: MainCamera m_TagString: MainCamera
@@ -275,6 +276,21 @@ MonoBehaviour:
go: {fileID: 0} go: {fileID: 0}
Spawnable: PashaBibko.Pacore.Editor.Drawers.DetectInspectorChangesDrawer Spawnable: PashaBibko.Pacore.Editor.Drawers.DetectInspectorChangesDrawer
Test: 0 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 --- !u!1 &410087039
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@@ -1,9 +1,7 @@
using System;
using System.Threading;
using PashaBibko.Pacore.Attributes; using PashaBibko.Pacore.Attributes;
using PashaBibko.Pacore.DevTools;
using PashaBibko.Pacore.Threading; using PashaBibko.Pacore.Threading;
using UnityEngine; using UnityEngine;
using System;
[CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour [CreateInstanceOnStart] public class TestMonoBehaviour : MonoBehaviour
{ {
@@ -14,6 +12,9 @@ using UnityEngine;
[DetectInspectorChanges("OnTestChange")] [DetectInspectorChanges("OnTestChange")]
public int Test; public int Test;
[StaticInspectorField] private static string StaticText;
[StaticInspectorField] private static string OtherStaticText;
private void OnTestChange() => LogTestValue(); private void OnTestChange() => LogTestValue();
[InspectorCallable(nameof(LogSpawnableType))] [InspectorCallable(nameof(LogSpawnableType))]