Moved editor caches to seperate folder

This commit is contained in:
2026-01-25 13:57:25 +00:00
parent abe3a4149b
commit f8b49427f1
7 changed files with 106 additions and 85 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9eace838af5041fd9d8cb05c8fbf9ef4
timeCreated: 1769349074

View File

@@ -0,0 +1,56 @@
using PashaBibko.Pacore.Attributes;
using System.Collections.Generic;
using System.Reflection;
using System;
namespace PashaBibko.Pacore.Editor.Caches
{
public class InspectorCallableCache
{
public struct AttributeInfo
{
public InspectorCallableAttribute Attribute;
public MethodInfo AttachedMethod;
}
private const BindingFlags BINDING_FLAGS =
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
private static Dictionary<Type, AttributeInfo[]> Cache { get; } = new();
public static AttributeInfo[] GetAllAttributes(Type type)
{
/* Checks the cache for the type */
if (Cache.TryGetValue(type, out AttributeInfo[] attributes))
{
return attributes;
}
/* Finds all the functions with the attribute */
MethodInfo[] methods = type.GetMethods(BINDING_FLAGS);
List<AttributeInfo> buttons = new();
foreach (MethodInfo method in methods)
{
InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>();
if (attribute != null)
{
AttributeInfo info = new()
{
Attribute = attribute,
AttachedMethod = method,
};
buttons.Add(info);
}
}
/* Adds it to the cache before returning */
AttributeInfo[] array = buttons.ToArray();
Cache.Add(type, array);
return array;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2574e8cf9f6b45dbb4aa22f0e62b23df
timeCreated: 1769349096

View File

@@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System;
namespace PashaBibko.Pacore.Editor.Caches
{
public static class MonoScriptCache
{
private static Dictionary<string, MonoScript> Cache { get; } = new();
static MonoScriptCache()
{
/* Finds all MonoScripts and adds them to the dictionary by name */
MonoScript[] scripts = Resources.FindObjectsOfTypeAll<MonoScript>();
foreach (MonoScript script in scripts)
{
Type scriptType = script.GetClass();
if (scriptType is not null)
{
string name = scriptType.FullName;
Cache.TryAdd(name, script);
}
}
}
public static MonoScript Get(string name)
{
/* Fetches the value (if there is one) without creating a default val */
Cache.TryGetValue(name, out MonoScript script);
return script;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 96c81b83f299432fbf183bc40b69c061
timeCreated: 1769349335

View File

@@ -1,62 +1,11 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using PashaBibko.Pacore.Attributes;
using PashaBibko.Pacore.Editor.Caches;
using UnityEditor;
using UnityEngine;
using System;
using Object = UnityEngine.Object;
namespace PashaBibko.Pacore.Editor.Drawers
{
public static class InspectorCallableAttributeCache
{
public struct AttributeInfo
{
public InspectorCallableAttribute Attribute;
public MethodInfo AttachedMethod;
}
private const BindingFlags BINDING_FLAGS =
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance;
private static Dictionary<Type, AttributeInfo[]> Cache { get; } = new();
public static AttributeInfo[] GetAllAttributes(Type type)
{
/* Checks the cache for the type */
if (Cache.TryGetValue(type, out AttributeInfo[] attributes))
{
return attributes;
}
/* Finds all the functions with the attribute */
MethodInfo[] methods = type.GetMethods(BINDING_FLAGS);
List<AttributeInfo> buttons = new();
foreach (MethodInfo method in methods)
{
InspectorCallableAttribute attribute = method.GetCustomAttribute<InspectorCallableAttribute>();
if (attribute != null)
{
AttributeInfo info = new()
{
Attribute = attribute,
AttachedMethod = method,
};
buttons.Add(info);
}
}
/* Adds it to the cache before returning */
AttributeInfo[] array = buttons.ToArray();
Cache.Add(type, array);
return array;
}
}
[CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)]
public class MonoBehaviourDrawer : UnityEditor.Editor
{
@@ -69,8 +18,8 @@ namespace PashaBibko.Pacore.Editor.Drawers
public static void DrawFunctionButtons(Object target)
{
Type type = target.GetType();
InspectorCallableAttributeCache.AttributeInfo[] buttons
= InspectorCallableAttributeCache.GetAllAttributes(type);
InspectorCallableCache.AttributeInfo[] buttons
= InspectorCallableCache.GetAllAttributes(type);
if (buttons.Length == 0)
{
@@ -80,7 +29,7 @@ namespace PashaBibko.Pacore.Editor.Drawers
EditorGUILayout.Space();
EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel);
foreach (InspectorCallableAttributeCache.AttributeInfo button in buttons)
foreach (InspectorCallableCache.AttributeInfo button in buttons)
{
string name = button.Attribute.ButtonName;
if (GUILayout.Button(name))

View File

@@ -1,38 +1,11 @@
using PashaBibko.Pacore.Attributes;
using System.Collections.Generic;
using PashaBibko.Pacore.Editor.Caches;
using PashaBibko.Pacore.Attributes;
using UnityEditor;
using UnityEngine;
using System;
namespace PashaBibko.Pacore.Editor.Drawers
{
public static class MonoScriptCache
{
private static Dictionary<string, MonoScript> Cache { get; } = new();
static MonoScriptCache()
{
/* Finds all MonoScripts and adds them to the dictionary by name */
MonoScript[] scripts = Resources.FindObjectsOfTypeAll<MonoScript>();
foreach (MonoScript script in scripts)
{
Type scriptType = script.GetClass();
if (scriptType is not null)
{
string name = scriptType.FullName;
Cache.TryAdd(name, script);
}
}
}
public static MonoScript Get(string name)
{
/* Fetches the value (if there is one) without creating a default val */
Cache.TryGetValue(name, out MonoScript script);
return script;
}
}
[CustomPropertyDrawer(typeof(MonoScriptAttribute))]
public class MonoScriptDrawer : PropertyDrawer
{