Error loading auto-generated monobehavior at run-time
Hello,
I'm using T4 run-time generation tools to auto-generate a monobehaviour class based on user input in an editor window. After much wrangling with issues, I'm able to succesfully take in the input, pass it to T4, compile the outputted code, and write that code to a script file which shows up in the Unity editor no problem.
My issue now is I want to also generate a prefab with that component attached.
I'm able to generate a prefab no problem, and the code executes without errors. I get a prefab object at the location I specify and there's SOME script attached.
My problem is there's a Type Mismatch on the script found on the prefab so it says the script can't be loaded.
My current theory is that because the auto-generated code is compiled at run-time into a new DLL, that DLL isn't being properly loaded into Unity's AppDomain and is therefore not accessible to Unity. My suspicions are strengthened by the fact that if I loop through all currently active assemblies and output all known types, I don't see the types of the classes I generated.
Ideally, I'd love to just compile the auto-generated code directly into the executing assembly and not have to deal with merging in DLL's, but I'm not sure if that's possible. I've tried setting the target assembly of the CompileParameters() object to the current assembly, but that doesn't work.
What I do find strange, however, is that Unity can work with the script files no problem. If I re-add the scripts to my prefab after generating everything, the prefab works fine and I can enter runmode with no errors. So somehow Unity is eventually getting enough info about these types to use them, I'm just not providing it up front properly.
Anyone have any ideas?