Tutorial How to create simple mod menu in Unity games

AndnixSH

PMT Elite Modder
Original poster
Staff member
Modding-Team
Jun 27, 2017
4,756
300,744
1,213
Modding World

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 Unity

Go to Unity - Store 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.

BB5wvZK.png


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:

O8KtUec.png


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:

C#:
public class ModMenuScript : MonoBehaviour {

    void OnGUI() {

    }

}
Add some fields above OnGUI() so you can use it later

Code:
public bool hack1;
public string string1;
public bool ShowHide = false;
Create a button "SHOW/HIDE"

C#:
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

C#:
if (GUI.Button(new Rect(20, 20, 160, 20), "SHOW/HIDE"))
{
    ShowHide = !ShowHide;
}
Add new if statement code method to design menu

C#:
if (ShowHide)
{

}
Add GUI box with title inside if statement code

C#:
GUI.Box(new Rect(20, 50, 170, 150), "iAndroHacker's mod menu");
And add the button with operator bwloe GUI box

C#:
if (GUI.Button(new Rect(25, 80, 160f, 30f), string1))
{
    hack1 = !hack1;
}
Add new if-else-statement code that changes the text

C#:
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

Script template: Sign in · GitLab

FfrYfZV.gif


See some useful documentation:

Styled text: Unity - Manual: Rich Text

Immediate Mode GUI (IMGUI): Unity - Manual: Rich Text

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

gHcTkWV.png


Name it OnGUI and click OK.

Right click on OnGUI(); and edit method

XPmxKqs.png


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

C#:
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

8RwLWyU.png


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:

C#:
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>'"

00XFkit.png


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:

C#:
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)

C#:
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:

C#:
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 :)

Video example: bandicam 2018-01-26 18-59-25-119 - Streamable

Credit:
iAndroHacker
 
Last edited:
S

sai shashank

Guest
Severity Code Description File Line
Error CS0117 'ModMenuScript' does not contain a definition for 'MyGUI' main.cs 14
 

Arifin79

Awesome Active Platinian
Dec 3, 2017
109
1,619
193
Is there something missing in the tutorial? Why the hack doesnt work when i turn on/off?

Code:
using UnityEngine;

public class ModMenuScript : MonoBehaviour {

    public static bool hack1;
    public static string string1;
    public static bool ShowHide = false;

    public static void MyGUI() {

        if (GUI.Button(new Rect(20, 20, 130f, 30f), "CHEATS"))
        {
            ShowHide = !ShowHide;
        }

        if (ShowHide)
        {
            GUI.Box(new Rect(20, 50, 180, 150), "Arifin79 Menu");
            if (GUI.Button(new Rect(25, 80, 170f, 30f), string1))
            {
                hack1 = !hack1;
            }
        }

        if (hack1)
        {
            string1 = "One Hit <color=green>ON</color>";
            hack1 = false;
        }
        else
        {
            string1 = "One Hit <color=red>OFF</color>";
            hack1 = true;
        }
    }
}
Code:
public static int Damage(int level, int count)
    {
        int num = (10 + 5 * (level - 1)) * count;
        if (ModMenuScript.hack1)
        {
            return (int)((float)num * (1f + (float)ManagerBase<GlobalStatsManager>.Instance.m_BonusDamage / 100f)) * 999999;
        }
        return (int)((float)num * (1f + (float)ManagerBase<GlobalStatsManager>.Instance.m_BonusDamage / 100f));
    }
Lastly for showing the gui, i use GameEventManager:

Code:
public partial class GameEventManager : MonoSingleton<GameEventManager>
{
    public void OnGUI()
    {
          ModMenuScript.MyGUI();
    }

    // Token: 0x17000305 RID: 773
    // (get) Token: 0x06001BAC RID: 7084 RVA: 0x00013591 File Offset: 0x00011791
    public bool IsEventLoadingComplete
    {
        get
        {
            return this.m_completeEventLoading;
        }
    }
}
 

AndnixSH

PMT Elite Modder
Original poster
Staff member
Modding-Team
Jun 27, 2017
4,756
300,744
1,213
Modding World
@Arifin79 haven't tried hotkeys yet. i will look into it. about reverting value back to original, you have to make a code that backups original value before it enable hack and revert back if disabled. i tried it but it sometime fails.
 

Arifin79

Awesome Active Platinian
Dec 3, 2017
109
1,619
193
@Arifin79 haven't tried hotkeys yet. i will look into it. about reverting value back to original, you have to make a code that backups original value before it enable hack and revert back if disabled. i tried it but it sometime fails.
I'm still confused.... how to make the backup original code?

The value never changes after i turn it on but when i remove the script "
if (ModMenuScript.hack1)" it worked. I think the menu didnt link properly from menu script module with the module where the damage is located.
 

Arifin79

Awesome Active Platinian
Dec 3, 2017
109
1,619
193
Like this?

Code:
public static int Damage(int level, int count)

    {
        int testvalue;
        int num = (10 + 5 * (level - 1)) * count;
        if (ModMenuScript.hack1)
        {
            testvalue= 9999999;
        } else {
            testvalue= 0;
        }
      
        return (int)((float)num * (1f + (float)ManagerBase<GlobalStatsManager>.Instance.m_BonusDamage / 100f)) * testvalue;
    }
I think the script doesnt link properly between menu module with module where damage is located.

I'm still new to apk modding so cant think better logic. xD
 

Arifin79

Awesome Active Platinian
Dec 3, 2017
109
1,619
193
Maybe shouldnt remove the "extern"? I code hack in dll form in c++ in the past and i usually use extern to link my cheats between cpp and header files.
 

Arifin79

Awesome Active Platinian
Dec 3, 2017
109
1,619
193
Found the problem.... it has to do with GUI location script in GameEventManager.

When i active the hack during loading screen, the hack worked. But when i active the hack after loading ends it became not working.

So basically to solve this i just need to find which class to show my GUI all the time and that allows me to activate the hack during the game.

OK, no more question. I think im done here. Btw, dont forget about the hotkey thing :p
 

AndnixSH

PMT Elite Modder
Original poster
Staff member
Modding-Team
Jun 27, 2017
4,756
300,744
1,213
Modding World
Found the problem.... it has to do with GUI location script in GameEventManager.

When i active the hack during loading screen, the hack worked. But when i active the hack after loading ends it became not working.

So basically to solve this i just need to find which class to show my GUI all the time and that allows me to activate the hack during the game.

OK, no more question. I think im done here. Btw, dont forget about the hotkey thing :p
maybe try this code

Code:
if ( Input.GetKeyDown(KeyCode.Y) )
     {
         if (isKeysEnabled)
         {
             isKeysEnabled = false;
         }
         else
         {
             isKeysEnabled = true;
         }
     }
Unity - Scripting API: KeyCode

Would it be remotely possible to create a mod menu for non unity games?
Yes, you need android studio and ndk to develop mod menu for non unity games but that's something i don't have knowledge.
 

KingT

Retired
Jun 13, 2017
152
8,806
0
maybe try this code

Code:
if ( Input.GetKeyDown(KeyCode.Y) )
     {
         if (isKeysEnabled)
         {
             isKeysEnabled = false;
         }
         else
         {
             isKeysEnabled = true;
         }
     }
Unity - Scripting API: KeyCode



Yes, you need android studio and ndk to develop mod menu for non unity games but that's something i don't have knowledge.
Great Tutorial but i have a little problem can discord me bro ? Thanks in advanced