Access game classes from within Editor extension?

Hello everyone,

I’m currently working on an Editor extension for Unity, and therefore the classes I’m working with reside in the “Editor” folder. I now need to access classes from the actual project DLL (in other words: the classes within the project outside the Editor folder). To make more clear what I mean, here’s what VisualStudio displays:


Assembly-CSharp-Editor-vs this is where my classes are placed

Assembly-CSharp-vs this is the assembly I want to access


The question is: What is the best way to do this? I imagine that one could find out the project location on the OS file system, then navigate to

projectRoot/obj/Debug/Assembly-CSharp.dll

… and grab the assembly DLL file from there, but is there a more elegant way to solve this? By the way, since this editor extension should work for all projects, I need to do this via reflection of course.

Thanks,

Alan

EDIT: To be more precise about the problem, here’s what I’m trying to achieve. I want to call “Type.GetType(name)” from within an editor class, where “name” contains the type name of a type that resides outside the editor folder (therefore, in the “normal” game DLL, not in the editor DLL). However, this call will always return null from within an editor class, whether the class in question exists outside the editor folder or not. I concluded that the editor therefore does not load the game assembly by default and now I want to do this myself.

All project assemblies are accessible by editor scripts since editor scripts are in the last compilation group. If you don’t have a certain classname you want to access, just use reflection. The classes are all there.

Here’s an extension method for the Type class which gives you a list of all derived classes:

public static List< Type > GetAllDerivedClasses(this Type aBaseClass, string[] aExcludeAssemblies)
{
    List< Type > result = new List< Type >();
    foreach (Assembly A in AppDomain.CurrentDomain.GetAssemblies())
    {
        bool exclude = false;
        foreach (string S in aExcludeAssemblies)
        {
            if (A.GetName().FullName.StartsWith(S))
            {
                exclude = true;
                break;
            }
        }
        if (exclude)
            continue;
        if (aBaseClass.IsInterface)
        {
            foreach (Type C in A.GetExportedTypes())
                foreach(Type I in C.GetInterfaces())
                    if (aBaseClass == I)
                    {
                        result.Add(C);
                        break;
                    }
        }
        else
        {
            foreach (Type C in A.GetExportedTypes())
                if (C.IsSubclassOf(aBaseClass))
                    result.Add(C);
        }
    }
    return result;
}

public static List< Type > GetAllDerivedClasses(this Type aBaseClass)
{
    return GetAllDerivedClasses(aBaseClass, new string[0]);
}