Hello,
So recently I have been working on EventManager and using reflection to subscribe events for easy access in the editor, what surprised me was when my editor class that has a subscribe button is created in “Test” Folder I have any instance of my class would portray that inspector and everything works fine with things like Type.GetType(“Class Name”), but once I moved my script to the Editor folder my Type.GetType now returns null… any ideas why that would happen? I linked the code below.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.Reflection;
using System;
[CustomEditor(typeof(EventManager))]
public class GetMethod : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
if (GUILayout.Button("Subscribe To Event"))
{
Subscribe();
}
serializedObject.ApplyModifiedProperties();
}
void Subscribe()
{
string[] guids = AssetDatabase.FindAssets("", new string[] { "Assets/Phonics_Scripts" });
List<string> paths = new List<string>();
foreach (string guid in guids)
{
if (AssetDatabase.GUIDToAssetPath(guid).EndsWith(".cs"))
paths.Add(AssetDatabase.GUIDToAssetPath(guid));
}
foreach (string path in paths)
{
MonoScript loadedAsset = AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript;
MethodInfo method = null;
Type type = null;
object instance = null;
if (loadedAsset.text.Contains("void SubscribeToManager()"))
{
type = System.Reflection.Assembly.GetExecutingAssembly().GetType(loadedAsset.GetClass().Name);
if (!type.IsAbstract)
instance = Activator.CreateInstance(type);
method = loadedAsset.GetClass().GetMethod("SubscribeToManager", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
}
if (method != null)
{
method.Invoke(instance, null);
}
}
}
}
EDIT: I tried moving my subscribe() function to my EventManager Class and it worked fine, can anyone explain why getting the type of a class does not work in an editor class? and how come editor classes work even outside the Editor Folder now?!