I’m trying to collect a list of classes which derive off some class that is not within a scene but within the game dll - is this possible? I want to use this list to populate an editor drop down menu so I only need to do this in an editor script. I’ve seen some scripts that iterate through files in the project but a potential issue is that some of these classes reside in the same file.
If you get a reference to the assembly (see the Assembly class for ways to do that), you can then loop over all the types (classes/structs/enums/etc) in that assembly. The Type object has members to get the classes it inherits from as well as interfaces it implements. You can use this to test if it inherits from some type you are considering. The type class has a method for doing this in one simple call (see: Type.IsAssignableFrom)
Loop over all types, get the ones that meet your requirements. Now you have references to all the types in question.
Here are some methods I use to do this very thing:
public static System.Type[] GetTypesAssignableFrom(System.Reflection.Assembly assemb, System.Type rootType)
{
var lst = new List<System.Type>();
foreach (var tp in assemb.GetTypes())
{
if (rootType.IsAssignableFrom(tp) rootType != tp) lst.Add(tp);
}
return lst.ToArray();
}
public static bool IsType(System.Type tp, System.Type assignableType)
{
return assignableType.IsAssignableFrom(tp);
}
public static bool IsType(System.Type tp, params System.Type[] assignableTypes)
{
foreach (var otp in assignableTypes)
{
if(otp.IsAssignableFrom(tp)) return true;
}
return false;
}