1
\$\begingroup\$

I want to write a Unity app that doesn't use the Editor. That's the goal. Please don't ask why or tell me the Editor "is really great, you should try it", I know all that, I love the Editor, I'd personally love to WORK on the Editor (at Unity). That's not how this particular project goes, however.

The goal is a 3D prototyping app. In the app, you make design changes to a set of 3D models, and the changes you make show up "live" in a Unity app. When you're done, you "bake" the changes into an actual Unity app, complete w/ the Unity project source code, so you can open it and further refine it. Sounds goofy, I know, I was assigned this project from somebody else.

I know that for the live changes shown in Unity, I could of course make a nearly blank Unity project, and add GameObjects at runtime, and set their attributes. That much is obvious.

What's not obvious is how to add C# script behaviors at runtime. If the Unity "live" app is running all the time, and I'm adding GameObjects at runtime, how then do I add compiled C# dlls at runtime, so the dynamically created GameObjects use the newly compiled C# code?

I'm thinking the GameObjects would call out to a C# dll by some kind of name-binding, each time the C# code is updated, the dll is written out with a GUID/hash, and in the GameObject code, it dynamically looks for that Dll and loads it, then steps into it. seems tricky and complicated, right?

Has anybody done this and had some insight into it? Before anybody references "Futile", keep in mind I don't care much about looking into a 2D only answer for this, and don't want to dig though that 2D code even if it has the answer in it. I'd like to discuss the answer here, on this forum, about thoughts about how to approach this, in the hopes it may help other people too.

Goals: 1. I provide 3rd party users a prototyping app that throws together an "editor" or a "prototyper" which shows on one winodw, with live in-Unity results in another window. Let's say the "prototyper" object in question is a GIANT terrain, populated with houses, trees, humans, etc. You can move around, see what the generated stuff looks like. Then, when you're happy with the look, you press "bake" and out pops a Unity project that you can go edit and flesh out. 2. I need the live Unity view to accept c# code (over some kind of communication mechanism) that will get compiled and run by the live view. Examples for why might be trees in the scene waving in the wind, running waterfalls, etc.

Thoughts?

\$\endgroup\$

marked as duplicate by DMGregory unity Feb 28 '18 at 14:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ I might be happy if I can just get the objects within the live Unity app to use Javascript instead of already-compiled C#. Is that possible? \$\endgroup\$ – eric frazer Dec 21 '17 at 15:44
  • \$\begingroup\$ The JavaScript ends up being compiled into IL anyway, so at runtime it makes no difference. \$\endgroup\$ – Ed Marty Dec 21 '17 at 16:35
  • 1
    \$\begingroup\$ If your problem is specifically adding new C# scripts at runtime, this previous question about runtime compilation might be useful to you. \$\endgroup\$ – DMGregory Dec 21 '17 at 17:54
  • \$\begingroup\$ Your end-user seems to have Unity installed. So would it be OK if the end-user would have to start your application from within Unity as a scene in Play Mode? I am asking because there are a lot of APIs in the UnityEditor namespace which could be very useful for this. But they are only available from the editor or in test play mode. \$\endgroup\$ – Philipp Jan 23 '18 at 13:43
0
\$\begingroup\$

To compile C# code, you may want to look into Microsoft.CSharp.CSharpCodeProvider but Unity doesn't normally reference the assembly that class is in, and it won't work on non-Windows systems. An alternative is Mono.CSharp.Evaluator but that doesn't allow creating new classes.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.