- Home /
How can I dynamically changed where buttons are placed?
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.
Answer by chrisall76 · Sep 20, 2012 at 12:10 AM
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
}
Thanks for the response. However I can't get my code to look like yours. If you could, check my reply for more information.
This is the C# version of his answer:
// [...]
private Vector3 scale;
public float originalWidth = 854.0f;
public float originalHeight = 480.0f;
void OnGUI ()
{
scale.x = Screen.width / originalWidth;
scale.y = Screen.height / originalHeight;
scale.z = 1.0f;
var sv$$anonymous$$at = GUI.matrix;
GUI.matrix = $$anonymous$$atrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, scale);
//code here
GUI.matrix = sv$$anonymous$$at; // restore matrix
}
Btw: yes, sv$$anonymous$$at is of type $$anonymous$$atrix4x4, however C# also supports type inference ;)
Okay if I post a link to my code, can you tell me why it doesn't work?
Answer by kmccmk9 · Sep 20, 2012 at 12:57 AM
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;
}
}
Ok okay. Thanks for the help. I will do my best to take your pseudo code and turn it into c#
@kmccmk9: You did not post a "reply", you posted an answer. This is not a forum, this is a Q&A site. Answers should answer the question. when you want to improve your question, edit it.
Also chrisall76 didn't posted pseudo code. This is valid UnityScript code. Unityscript is Unity's Javascript-like language which can also be used besides C# and boo.
What type should sv$$anonymous$$at be? $$anonymous$$atrix4x4?
Your answer
Follow this Question
Related Questions
use iPhoneUtils.PlayMovie. Change size? 0 Answers
Can you scale a Mesh independently of it's Collider? 1 Answer
Difference between sizes 1 Answer
Max scale in unity3d 2 Answers