A sample Unity project demonstrating a runtime mod loader. It compiles C# source files using Roslyn (Microsoft.CodeAnalysis.CSharp) and loads them into the game.
- Drop
.csfiles intoMods/YourModDirectory/. - Add a
mod.jsonwith metadata (name, author, entry point, etc.). - When game starts, the system compiles all mods into DLLs and invokes their
Main()methods if they were found. - Mods get a basic API surface (
ModAPI.Utils) to interact with the game. Implement it yourself.
Assets/Scripts/
├── MainMenuLoader.cs # Entry point — compiles & loads mods on Start
├── Paths.cs # Path resolution (Mods/, ModCompilations/)
├── ModLoader/
│ ├── ModLoader.cs # Loads compiled DLLs
│ ├── ModCompiler.cs # Roslyn compilation pipeline
│ ├── ModConstants.cs # Mod metadata model
│ ├── Utils.cs # JSON (de)serialization, validation
│ └── ModAPI/
│ └── Utils.cs # Public API surface for mods
// mod.json
{
"ModName": "MyMod",
"Author": "me",
"Version": "1.0",
"EntryPoint": "MyMod.ModMain"
}// mod.cs
using ModAPI;
namespace MyMod
{
public static class ModMain
{
public static void Main()
{
// your mod logic here
}
}
}- Requires
Microsoft.CodeAnalysis.CSharpNuGet package (included inPackages/) - Mods are compiled to
ModCompilations/and loaded viaAssembly.LoadFile - Unloading mods at runtime is not supported — Mono/.NET Framework doesn't allow unloading assemblies