This guide assumes you are using Visual Studio 2022.

BepInEx Template

Follow this guide to generate a plugin template. This is basically the shell of a mod, and is fully functional by itself.

  • Make sure BepInEx is installed for your game.
  • Follow this guide until you get to the step of generating the plugin template
  • Run the following command to generate the plugin template
    dotnet new bepinex5plugin -n MyFirstPlugin -T netstandard2.1 -U 2022.3.0
    NOTE
    If you have .NET 8 SDK installed, you will get the error `Object reference not set to an instance of an object`
    when attempting to run the above command.

    To resolve this issue, follow these steps:
    • Ensure you have a copy of the .NET 6 or 7 SDK installed. If you're not sure, go install it
    • Run the following command and copy the full, exact version of the latest .NET 6 or 7 SDK (e.g "7.0.407")
      dotnet --list-sdks
    • Run the following command
      dotnet new globaljson --sdk-version [exact sdk version]
    • Try to generate the plugin template again
  • Once the template is generated, open the .csproj file that was created using Visual Studio.
  • Build the mod (Top menu -> Build -> Build Solution)
  • In the Output, it will show a path to a generated .dll file
  • Copy this .dll file to your games BepInEx/plugins folder
    NOTE
    You may want to make BepInEx show the console when opening the game for testing purposes. To do this, follow these steps:
    • Open [game folder]/BepInEx/config/BepInEx.cfg
    • Set "Enabled" to "True" under "[Logging.Console]"

What Now?

Now you probably want to actually interact with the existing game code. BepInEx plugins hook directly into Unity, and becomes a MonoBehaviour in the game scene when loaded. However, your BepInEx environment doesn't have any reference to the code that makes up the game.

DLL References

You can add dll files from the game as references to your BepInEx project to directly interact with the game code.
  • In the Solution Explorer, right click Dependencies
  • Click Add Project Reference -> Browse...
  • Add "Assembly-CSharp.dll" found at [game install directory]/[game name]_Data/
    NOTE
    This file will usually contain almost all of the games code. To skim through this code, you can open the dll file with dnSpy

Publicizer

A publicizer will allow you to access private members in existing game code. For example, if there is a private method on a MonoBehaviour, you would be able to call this method from your mod. It is possible that you don't need this, so I usually don't install it until I actually find a private member I want to access.
  • Top menu -> Tools -> Nuget Package Manager -> Manage Nuget Packages
  • Go to the Browse tab, make sure Package Source is "All"
  • Search for "BepInEx.AssemblyPublicizer.MSBuild" and install it
  • Double click your mod name in the Solution Explorer
  • Find the Reference entry you want to publicize and add the property "Publicize="True""
    (e.g <Reference Include="Assembly-CSharp" Publicize="True">)

Other Tools / Tips

Article information

Added on

Edited on

Written by

grumpythe1st

0 comments