I am building a game in Unity using C# and visual studio. I want to make it possible for users to write code in some scripting (for modding purposes) without necessarily opening the source. What are my best options?

I'd prefer well-supported and battle-tested options that integrate well with Unity and visual studio, but I am willing to do a fair amount of work myself as long as it makes scripting as seamless as possible for future modders.


3 Answers 3


A somewhat blunt approach would be to let modders write C# code and then have your game read & compile those scripts, using them as though they were code the game was built with.

This has some advantages:

  • Modders can use the existing Unity API, so it's already familiar to many, with extensive documentation and tutorials.

  • It's very fast to get up and running, exposing a very large feature set, with very little API development or supporting code you need to provide.

...And some disadvantages:

  • The exposed feature set is very large, roughly everything C# can do. As described in the linked answer, a modder with this kind of access can do basically anything they want to your game. I don't know any ironclad way to lock them out of particular functionality or constrain what they can mod once you've let them execute arbitrary code.

  • This means that installing a mod written by a stranger requires a high level of trust. They might have coded-in spyware, or other malicious content, so your game can be used as an attack vector against unwary players. Not necessarily a situation you want to be in.

  • As described in the linked answer, compiling at runtime is not supported on all platforms Unity can target, and requires additional plugins on some.

A more robust solution, as mentioned at the link, is to build your own virtual machine — using eg. The Bytecode Pattern — so you can execute scripts you've loaded as data at runtime, with strict control over what they're allowed to do.


You could do it like Kerbal Space Program does it: Allow your game to load AssetBundles at runtime by having it read config files which tell it where and in what way the assets in the bundle are supposed to appear in the game.

Then tell your modders to get Unity Personal Edition and use it to build their mod as one such asset bundles with an appropriate config file. You might need to open source some of your interfaces and offer them as a "mod SDK" asset bundle for your modders in order to interface properly with the rest of your game.

This method has the advantage that your modders can do anything Unity can do, which is a lot of nice stuff. But unfortunately, this method has the disadvantage that modders can do anything Unity can do, which also includes a lot of malicious stuff. So you need to educate your players to not download any mod they find.

If you want more secure modding, then you need to invest some more work. A good method to properly sandbox mods is by using a scripting language which allows you to selectively expose certain functionality of your game. You could write it yourself, but designing a good programming language and writing an interpreter for it is much more difficult than it seems. A better solution can be to use an existing scripting engine. There are scripting engines for C# available for pretty much any well-known scripting language.


As more advanced and more powerful alternative you could try using either MAF or MEF frameworks for C#. Among other things it handles for you automatic mod discovery, runtime loading and unloading, controllable security and improves stability (can run in separate domains).
The downside is that designing addin-aware architecture is much harder than simple compiling text file. While powerful it also imposes some requirements on loaded code as it is made primarily for plugin/DI systems which you might find too restricting - or ideal, depending on what are the mods supposed to do/add/modify.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .