How to create simple mod menu in Unity games



I have gotten a lot of request for a tutorial how to make mod menu but haven't got time and almost forgot it. I had to pause all my other protects and start working with this tutorial straight away.

With Mod menu, it allows the player to choose what hacks to use. Mod menu can be useful in multiplayer games that player can enable and disable hack quickly to avoid being reported by legit players

So let's get started.

Requirements:
- Basic Unity modding expernence
- Familar with C# and dnSpy
- dnSpy installed on your computer
- Unity editor installed on your computer
- Visual Studio 2017 installed on your computer (Optional.)

Installing Unity editor:
First, create an account or sign in with Google or Facebook at https://unity3d.com/

Go to https://store.unity.com/ and select "Try personal". It is completely free. Download and install it. If you like to have Visaul Studio 2017 installed, make sure to select it in installer. If not installed, you will use MonoDevelop.

Setting up Unity editor:
Launch Unity, login with your account and complete your survey. After that you can create a new project. Name it you want and keep 3D selected. Click "Create project"


The dashboard will launch.

Create a new C# script:
Click on Assets -> Create -> C# script or right click on Project section Create -> C# script


Drag your script and drop on top of Main Camera below Untitled to make sure your script is shown in game scene.

Designing and testing GUI:
Tip: While you programming, you can click play and edit code while the game scene is running. The editor will freeze a while when you open it.

Remove Start() and Update() methods and add a OnGUI() method like this:


public class ModMenuScript : MonoBehaviour {

    void OnGUI() {

    }
}

Add some fields above OnGUI() so you can use it later
public bool hack1;
public string string1;
public bool ShowHide = false;

Create a button " SHOW/HIDE"
GUI.Button(new Rect(20, 20, 160, 20), "SHOW/HIDE"

The numbers (20, 20, 160f, 20f) means (x, y, width, height)

Add if-statement on GUI button with operator so you can hide/show menu by clicking on button
if (GUI.Button(new Rect(20, 20, 160, 20), "SHOW/HIDE"))
{
    ShowHide = !ShowHide;
}

Add new if statement code method to design menu


if (ShowHide)
{

}


Add GUI box with title inside if statement code
GUI.Box(new Rect(20, 50, 170, 150), "iAndroHacker's mod menu");
And add the button with operator bwloe GUI box
if (GUI.Button(new Rect(25, 80, 160f, 30f), string1))
{
    hack1 = !hack1;
}

Add new if-else-statement code that changes the text

if (hack1)
{
    string1 = "Unlimited health <color=green>ON</color>";
    hack1 = false;
}
else
{
    string1 = "Unlimited health <color=red>OFF</color>";
    hack1 = true;
}

Congratulations, you have created a very simple mod menu. You can now customize and add more button easly by yourself and view it in game scene J




See some useful documentation:


Adding mod menu into assembly-sharp.dll file:
This is a tricky part because there are many ways to add your own code like adding OnGUI method on any classes (Loading, LoadingScreen, MainMenu, etc…) or add code on existing OnGUI methods.

Method 1: Adding OnGUI method
In this example, I will create OnGUI and add my code in UserInterfaceManager on Super Dungeon Bros.

Find an active class or search "Loading" and pick one of loading method. In my example, I picked DisplayLoadingScreen located in UserInterfaceManager. Right click on UserInterfaceManager and create a method.

Note: Make sure the class have Instance method. If it does not have Instance, you have to create your own class


Name it OnGUI and click OK.


Right click on OnGUI(); and edit method


Remove "extern" and modify code so it looks like this:

public void OnGUI() {

}

You can paste your code here and compile it.


Remove "static" modifier if mod menu doesn't appear

Method 2: Create a new class for GUI


Add a class on empty namespace


Add your whole code and name your class. Make sure you add public and static modifier like below so other classes can access the fields. Your method name must not be called OnGUI, call it MyGUI or whatever. It is to avoid confusion with another OnGUI since Unity always read something if it recognize OnGUI methods.
Example:


public void OnGUI()
{
    
ModMenuScript.OnGUI(); //makes problem
}

if you don't add static modifer, you will get an error "An object reference is required for the non-static field, method, or property '<code>'"


Save the class. Sometimes dnSpy can't find class you created so you have to save the assembly as Assembly-Csharp1.dll or whatever, Close all dlls and open Assembly-Csharp1.dll

To make your mod menu appear in-game, find an active class or search "Loading" and pick one of loading method, create OnGUI method and edit the code like:

public void OnGUI()
{
    
ModMenuScript.MyGUI();
}

Hacking functions
Now search some functions to hack. If you had added OnGUI in an existing class with Instance method, modify the code like this:
(I modify this getter method as an example)

public float ArmorCapacity
{
    get
    
{
        
if (UserInterfaceManager.Instance.hack1)
        {

            
return 999f;
        }

        
return this._armorCapacity;
    }

    
set
    {
    }
}

If you had added your own classes, modify the code like this:

public int get_SupplyCost()
{
    
if (ModMenuScript.hack1)
    
{
        
return 999;
   
 }
    
return this.UnitInstance.SupplyCost;
}

There are other ways to hack it but these methods are the best way to hack for beginners.


Save the dll file and test your mod menu J

Credit:
iAndroHacker

Kommentarer