Hello, my buttons are using a custom style. I have been able to place them exactly where I want using code. However, when I changed the game window size the buttons of course were in the wrong spots and not scaled correctly. Is there any simple way to dynamically change the placement and scale of the buttons without using a bunch of if statements with different bounding rectangles for each? Thanks for any help.
Here’s what I use, scales the GUI depending on the size of the window
private var scale : Vector3;
var originalWidth = 854.0; // define here the original resolution
var originalHeight = 480.0; // you used to create the GUI contents
function OnGUI (){
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, scale); // substitute matrix - only scale is altered from standard
//code here
GUI.matrix = svMat; // restore matrix
}
Okay thanks for the response. So here is my code as it stands. How would I adapt my code to work like yours. I understand your code I’m just having a hard time making mine look like yours.
using UnityEngine;
using System.Collections;
public class MainMenuGUI : MonoBehaviour {
//Variables
public GUIStyle PlayButton;
public GUIStyle OptionsButton;
public GUIStyle InstructionsButton;
public GUIStyle ExitButton;
public Texture Background;
bool doWindow0 = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
doWindow0 = false;
}
void OnGUI () {
//Draw Background
GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),Background,ScaleMode.StretchToFill, false, 0.0f);
//Draw PlayButton
if (GUI.Button(new Rect(30,180,235,590),"",PlayButton))
{
Application.LoadLevel("LoginScreen");
}
//Draw OptionsButton
GUI.Button(new Rect(260,330,220,440),"",OptionsButton);
//Draw InstructionsButton
if (GUI.Button(new Rect(1160,330,145,440),"",InstructionsButton))
{
doWindow0 = true;
}
//Draw ExitButton
if (GUI.Button(new Rect(1320,180,265,590),"",ExitButton))
{
Application.Quit();
}
if (doWindow0 == true)
GUI.Window (0, new Rect(Screen.width/2-240,Screen.height/2-100,800,120), DoMyWindow, "Instructions:");
}
void DoMyWindow (int windowID) {
GUI.Label (new Rect (10, 20, 800, 40), "Welcome to RedLightLife! Use WASD keys to move your character and use KL to rotate the camera. Use the J key to interact with people and objects. To have your character jump use the SPACEBAR. To pause your game, press the P key.");
if (GUI.Button (new Rect (10,80,100,20), "Close"))
doWindow0 = false;
}
}