Moved editor caches to seperate folder
This commit is contained in:
3
Assets/Pacore/Editor/Caches.meta
Normal file
3
Assets/Pacore/Editor/Caches.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9eace838af5041fd9d8cb05c8fbf9ef4
|
||||||
|
timeCreated: 1769349074
|
||||||
56
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs
Normal file
56
Assets/Pacore/Editor/Caches/InspectorCallableCache.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2574e8cf9f6b45dbb4aa22f0e62b23df
|
||||||
|
timeCreated: 1769349096
|
||||||
34
Assets/Pacore/Editor/Caches/MonoScriptCache.cs
Normal file
34
Assets/Pacore/Editor/Caches/MonoScriptCache.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta
Normal file
3
Assets/Pacore/Editor/Caches/MonoScriptCache.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 96c81b83f299432fbf183bc40b69c061
|
||||||
|
timeCreated: 1769349335
|
||||||
@@ -1,62 +1,11 @@
|
|||||||
using System;
|
using PashaBibko.Pacore.Editor.Caches;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Reflection;
|
|
||||||
using PashaBibko.Pacore.Attributes;
|
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
namespace PashaBibko.Pacore.Editor.Drawers
|
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)]
|
[CustomEditor(typeof(MonoBehaviour), editorForChildClasses: true)]
|
||||||
public class MonoBehaviourDrawer : UnityEditor.Editor
|
public class MonoBehaviourDrawer : UnityEditor.Editor
|
||||||
{
|
{
|
||||||
@@ -69,8 +18,8 @@ namespace PashaBibko.Pacore.Editor.Drawers
|
|||||||
public static void DrawFunctionButtons(Object target)
|
public static void DrawFunctionButtons(Object target)
|
||||||
{
|
{
|
||||||
Type type = target.GetType();
|
Type type = target.GetType();
|
||||||
InspectorCallableAttributeCache.AttributeInfo[] buttons
|
InspectorCallableCache.AttributeInfo[] buttons
|
||||||
= InspectorCallableAttributeCache.GetAllAttributes(type);
|
= InspectorCallableCache.GetAllAttributes(type);
|
||||||
|
|
||||||
if (buttons.Length == 0)
|
if (buttons.Length == 0)
|
||||||
{
|
{
|
||||||
@@ -80,7 +29,7 @@ namespace PashaBibko.Pacore.Editor.Drawers
|
|||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel);
|
EditorGUILayout.LabelField("Functions", EditorStyles.boldLabel);
|
||||||
|
|
||||||
foreach (InspectorCallableAttributeCache.AttributeInfo button in buttons)
|
foreach (InspectorCallableCache.AttributeInfo button in buttons)
|
||||||
{
|
{
|
||||||
string name = button.Attribute.ButtonName;
|
string name = button.Attribute.ButtonName;
|
||||||
if (GUILayout.Button(name))
|
if (GUILayout.Button(name))
|
||||||
|
|||||||
@@ -1,38 +1,11 @@
|
|||||||
using PashaBibko.Pacore.Attributes;
|
using PashaBibko.Pacore.Editor.Caches;
|
||||||
using System.Collections.Generic;
|
using PashaBibko.Pacore.Attributes;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace PashaBibko.Pacore.Editor.Drawers
|
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))]
|
[CustomPropertyDrawer(typeof(MonoScriptAttribute))]
|
||||||
public class MonoScriptDrawer : PropertyDrawer
|
public class MonoScriptDrawer : PropertyDrawer
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user