Tutorial [Tutorial] Unity mod menu automatic scale according to Screen

IZeuz

Platinian On Fire
Original poster
Aug 18, 2017
227
174
43
28
Germany
This is a simple way to made your mod menu scale according on the user phone screen size.

Request:
-know c#
-know Unity
-brain.

This tutorial are based for who make mod menu with their own Class

1)

When start make your class, add a variable to define native size of screen:
C#:
public class kkwModMenu : MonoBehaviour {


    //declare native size of screen

    Vector2 nativeSize = new Vector2(1024, 768);

2)

in the function OnGUI add this code(explication are in the code comment)
C#:
void OnGUI()  
    {
        //get scale value
        Vector3 scale = new Vector3 (Screen.width / nativeSize.x, Screen.height / nativeSize.y, 1.0f);
     
        //and apply the scale to all gui scripted here
        GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, scale);
        // write your own code
        if (GUI.Button(new Rect(20, 20, 160, 20), "Scaled"))
        {
            //your code
        }
    }
this is the most simply way i find to make mod menu aviable and at correct size for most device.

i hope this help you.
 
  • Like
Reactions: Sagar123sagar