There are many Unity games out there that supporting modding. When I mean modding I’m talking about C# Modding. I am attracted to these kinds of games because It allows me and others to extend the game. Some examples are Cities: Skyline and Kerbal Space Program. Since I don’t know how they accomplish mod support they may do it completing different. But I want to know how add mod support to my game where people just drop a .dll into a folder and on the game starts loads it?
Making your game support modding is a relatively advanced area of programming (we’re adding it to our game so we know first hand).
What you’re looking to do involves loading assemblies at runtime. If you want to let modders code in C# and not worry about compiling themselves, then you can look into runtime assembly creation.
A quick google provides you with a few starting points
It’s funny you’re saying that, because my game will support mods (at least in the form of custom maps/levels). The way I’ve realized it is by having a custom level format (which is basically a binary blob of specific structure).
I’m also working on a level editor so powerful, yet easy to use, that official levels would be made in it. So basically it will be as moddable as original Doom or Quake (minus putting in custom textures since art style of the game is “untextured polygons” ;)).
Modding all the way =). We’re doing something similar. We have our own mod tool (also developed in Unity). You can create structures / ships with it, import 3D models (Collada / .dae) at runtime and a few other things relating to the game.
Instead of loading assemblies at runtime we’re going with Lua for the scripting language.
CWolf I want to thank you for your help, but that is not necessarily what im looking for. Im looking to load .dlls at runtime that will modify the game like in KSP.
He just told you how to get started. Did you even check the links?
Thanks @Ostwind .
@Tyler-Jaacks : You may have explain yourself badly if the information I gave wasn’t what you wanted exactly. .dlls for what you described are usually called assemblies. Most modded games implement their modding in their own way but the high level things tend to be the same, or similar.
In this case, if KSP and Cities use C# .dlls / assemblies then you’ll want to look into assembly loading at runtime. If this isn’t what you wanted, maybe you can explain exactly what you want in more detailed. The devs will release an API or provide the useful methods for creating things within their game, and the modders will create C# classes that hook into these - then either compile it to .dll or the devs make their game compile the C# code into the assembly as part of their mod support.
We can’t give you the answer out right as modding is too complex an area of coding and it can be done many ways but we can always point you in the right general direction.
Another thing to consider is that now with unity personal every player can export asset bundles for including custom graphics/content in his mods, which the game can then load
By any chance, has anyone gotten Unity’s mono compiler as service to work outside the editor?
CWolf I thought you meant a C# Scripting thingy without compiling a .cs. Sorry My bad
As a general suggestion you should consider making the game moddable from the start. Build your core engine, then build all your gameplay and content as mods.
I agree with @Kiwasi completely. If you want to do modding well you need to plan it from the start of development.
Is there anything like that on mac? Or will .dlls work because of mono?
Using unity to build your own sandbox engine of runtime scriptable elements sounds amazing.
@Tomnnn : I’m not too familiar with mac development but assemblies (for this discussion specifically) are managed c# so I believe they would work (or you’d need to compile them out to another mac format worst case).
We’ve taken the approach to make our game as flexible as possible when modding. The core game is coded as a ‘core’ mod (which can be disabled if the players so wish). We code it in the script / mod layer so we can battle test the mod API (Lua ↔ C#). So far it’s working very well.
EDIT: To make it clear we investigated and decided not to go with the reloading assembly route ourself for a few reasons. One main reason was wanted to keep a controlled API and prevent total control of the game code for security reasons. That’s why we’ve gone with embedded Lua.
@CWolf I should probably test it while I’ve still got a mac this week.
I hope this thread stays open, I’ve been curious about this topic for quite a while. Even great games can benefit from mods. Skyrim is pretty good (npc character development aside) but the warzone mod fills it with epic battles that people would expect from something like a game of thrones season finale!
@Tomnnn : I’d be interested in knowing the results if you do get around to testing it.
If anyone is interested in mod creation my company blogs about development. We do series of blogs and one of them I write is about modding games (only two articles so far) Mod Support | Rogue Vector. My next post will be about integrating Lua into Unity then future articles will go into Unity runtime model importing (since it’s not supported by Unity out of the box) and various other useful things.
I completely agree with you about how even great games can benefit from modding. Back in the day I came from the Fallout modding community and I feel very, very strongly about moddable games. So many benefits can come from allowing users to experiment and change things with your game. This is one of the reasons we’ve spent so much time setting up a mod framework for our game on top of Unity. We believe the easier we can make it, the more people will get into modding (another reason we picked Lua over C# since we wanted to lower the bar for entry. More people find dynamically typed languages easier to get into than statically typed).
@CWolf haha the classes.class() is something I’ve never seen before. Nice read, very interesting.
Since I can’t make any promises around this computer, I think we should conclude that since I’ve seen steam integration with unity games which requires a dll plug-in that mono covers that as well. Mono is magic.
Can you imagine someone turning besiege into an RTS via modding? That’d be pretty incredible.
Yeah, completely. I’m of the mindset that a good game developer, if they have created an open enough modding system, will see mods that are of a higher quality than the core game. Some modders are so, so dedicated to their cause =).
Going to share how I’m doing modding support, maybe it can help you or give a hint. I come from modding Id Tech based engine games, (eg. Quake, Doom 3 etc…) so I wanted to do something similar on that regard. Basicly I do the main game as a mod itself reading everything from external files.
In Unity Editor itself I do very little, a game scene is just 3 gameobjects, all is managed trough code. First I studied what Unity can do in terms of importing assets at runtime that are not from asset bundles ( consider that I started this before Unity5 release) and how I can facilitate it, like for textures. Textures are easy to import at runtime as there are helpers to do the job, but for other things I needed to write my own parsers and my own file formats, for example for fonts.
Taking fonts as example I did a simple format for it, is just a bunch of ints and floats that are used to reconstruct at runtime the font. If you check the docs you see the unity font class uses CharacterInfo which hold to all info for uv rect and char position. THen for the texture itself of the font instead of having a separate png I write down the bytes of the alpha channel of every pixel of the font bitmap and reconstruct it from that (I don’t need rgb info so I’m good with alpha only). Then the trick is just to read back the bytes from the file trough System.IO and BinaryReader and reconstruct the Font class at runtime.
This stuff is used also for the rest, like materials, entities and all other stuff with the difference that for materials and other files I use plain text and parse with StreamReader and use Reflection to find the members and fill the values.
After that you also want to be sure about performance, take for example 2 models that uses the same material in the game, you can create the material twice at runtime and assign it, but the second material would be another instance making it another drawcall, and allocating more memory. What I do is to fill a cache, which is just a class with static functions where I store shared data, I create the material once, store it in the cache and is always accessible by using a simple id search in a dictionary.
Then I had to do a dictory management system for additional mods, I have my main data folder inside StreamingAssets, and an addon folder where you can put mods in other subfolders. At runtime I check the addon ffolder to see if there is anything in there and populate a list of paths. If someone select a mod the new main path is stored in a string, so everytime the game have to check for files first look up the mod, if nothing is found there it checks the main game and if it fails there too I just put failsafe to return null temporary assets to avoid errors and crashing, so to manage that I had to do also a custom debug log to print down all warnings for missing or wrong paths on assets.
For example if a texture isn’t found I load an error texture from resources instead avoiding a crash but still telling what was wrong.
So in the end wit works in a override way, if a mod is selected checks mod folder for stuff if nothing is in there go to main. This way is possible also to have mods with same file name of different assets without worrying about conflicts. Is just important to remember to have failsafes in place.
So if you are ntot going to use assetbundles for assets you have study first what unity can do at runtime, and in the cases unity won’t cooperate then find a workaround and build your own system like I had to do with sprite animations as I can’t create animations for sprites at runtime with unity functions I had to do my own system.
This is the most important part of creating mod games. IF you are truly going to make a game that users can modify through “Editor Tools” that you will create, then you must first create these tools; and then use the same tools you created to make the default game users can play. Sort of a playable demo of your editor tools like Neverwinter Nights by Bioware.
This allows you to test your mod tools and make sure users can correctly mod the game features. Whether it be in levels, items, monsters, game play, etc., make sure it will work as you intended by creating the base modded game that comes with your editor tools.
You can still create the basic logic core of the game whether it be a fantasy game (Neverwinter Nights), a horror game (Amnesia) or a sci-fi game (Galactic Civilization 2).