How can I create a modding API that can access monobehaviours?

Hi, in Unity, I have been attempting to create a modding API just for fun. I have attempted to do this by allowing modders to make .dll files with an API, and letting them place them within a mods folder created by my game.

I have created the API by using a C# script to create a Gamemodding namespace that contains a ModdingApi class that contains a variety of functions, and an interface that allows me to create OnStart functions for the modders.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace gameModding
{
    public class ModdingApiTools
    {
        public static void TestDebugMessage(string message)
        {
            Debug.Log(message);
        }
    }
    public interface IMod
    {
        void OnLoad();
    }
}

I place that C# script in a folder with an Assembly Definition file, which compiles my API into a .dll file.

image

From there, modders can reference the .dll file in a Visual Studio Project and create their own mods in the form of .dll files using the API.

Now, this works fine when the functions within the API use definitions built into the Unity Engine (GameObject, Transform, Debug, etc.). But the API script will not allow me to access my own scripts and scriptable objects I have created for the game, and instead, throw a CS0246 error at me. (For example, in the below function, Weapondata is a scriptable object, and SpawnItem is a Monobehaviour script)
image

My question is; How can I allow my API to reference the scripts I have created? and if I can’t accomplish that with my current modding setup, is there an alternate modding structure that can allow for something like that?

If you can leave a reply, I will be most grateful.

You need to also create an Assembly Definition file for your game scripts and add that to your ModdingApi definition’s reference list

1 Like

Thanks for your response. The proposed method partially works. Placing the scripts I am trying to reference into the folder with the Assembly Definition file allows the API to access the scripts, but every script I place into the folder will require other scripts to be placed into the folder with it, resulting in almost every script needing to be placed into the modding folder.

There are also namespaces besides UnityEngine (Like UnityEngine.InputSystem) that I can no longer import if I drag the scripts that require those packages into the folder.

Is there another way I can accomplish the same thing without having to drag around all of my scripts, or at least a way I can continue to access other namespaces, or am I going to have to restructure my code to allow for this method to be feasible?

You don’t need to move your scripts, but simply create an Assembly Definition file for them and reference them from your modding api assembly definition.
You can read about how to use assembly definitions here Unity - Manual: Assembly definitions
or if you prefer video you can find several on youtube, for example https://www.youtube.com/watch?v=qprZHOPu2OI

1 Like