How do I load code dynamically from outside sources?

Hello,

I am hoping to get some advice on how I can solve a problem I am having with how to load code dynamically into a unity game. To clarify the problem, let me explain an example scenario:

You are playing a game using a client made in Unity. Your character does not do anything because it is your job to make it do something. If code is created outside of the Unity game, how can it be loaded into the game to be used to interact with the “character”.

I am looking for technical details on how I can load this code into a game made with Unity dynamically. I would prefer if the code being imported was written in Javascript or C# and I could use that somehow. I don’t know if the answer has something to do with maybe compiling C# code instead and loading an assembly dynamically.

If any of this is unclear, please respond and I will try my best to clarify.

Thanks for any help,
John

1 Answer

1

This is not possible on iOS or the web player - on other platforms you need to create you new functionality and compile it to a DLL.

This can be achieved using a compiler like those shipped with Visual Studio or MonoDevelop or you can use Mono.CSharp DLL to create assemblies from C#.

You can then use one of the Assembly.Load methods to load the assembly into memory, at which time it’s types become available.

You might be interested in this thread: http://forum.unity3d.com/threads/102162-Mono-CSharp-Evaluator

Thanks for responding! Does this mean I am stuck using C#? Unfortunately for me, this does not seem like a very elegant solution to make programming a game at runtime possibly, which is ultimately what I am saying to do here. Would it be possible to server side the build process with something like the Mono.CSharp DLL so that code could be "submitted" to it, compiled to an assembly and that assembly spit back or even loaded into the game server?

Yes it's possible to create an assembly server side and then run it on the client (on appropriate platforms).

You might want to consider something like LUA: http://forum.unity3d.com/threads/167719-UniLua-A-pure-C-implementation-of-Lua-5-2-focus-on-compatibility-with-Unity3D

Thank you for your help and suggestions again. I will consider the LUA approach.