I am trying to find all types that inherit from monobehaviour inside my Unity project. So not the instances of them but the actual types in the assembly. I've tried this little piece of code and I don't understand why it is not getting the types. The debug is logging 0.
Also I am going to use this in editor scripting. But for testing purposes I am running the code in the start method of a component.
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;
using System;
public class TypeGetter : MonoBehaviour
{
private Dictionary<Type, FieldInfo[]> monobehavioursInfo = new Dictionary<Type, FieldInfo[]>();
private void Start()
{
monobehavioursInfo = GetTypes<MonoBehaviour>();
Debug.Log(monobehavioursInfo.Count);
}
private Dictionary<Type, FieldInfo[]> GetTypes<T>()
{
Dictionary<Type, FieldInfo[]> temp = new Dictionary<Type, FieldInfo[]>();
foreach (Type t in Assembly.GetAssembly(typeof(T)).GetTypes()
.Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T))))
{
FieldInfo[] fields = t.GetFields();
temp.Add(t, fields);
}
return temp;
}
}