- Home /
Gui question with android device
I am working on the gui interface for my game.
my game has a terrain of 1000X1000 so you will have to scroll to view the entire terrain.
on the terrain in certain areas there are buttons to make strong holds.
my question is this. When programming the gui, is the placement of buttons relative to screen size or camera location?
basically i want to know if i plan on moving the camera will the buttons move as well or do i need to update their positions?
Answer by Lttldude · Apr 18, 2012 at 03:51 PM
Using GUI. or GUILayout. will be based on the screen size not the camera location since it's all rendered on the GUI Layer of your camera. You should not need to update their positions. Just make the GUI Buttons positions fixed or relative to the screen width/screen height.
var sW : float;
var sH : float;
//Tip: when position GUI buttons and stuff, use an extra float variable like this
//then just put the variable into a place where you would normally have to play around
//with the numbers, then when you run it in Play mode, just change this value
var x0 : float;
function OnGUI()
{
sW = Screen.Width; //just makes it easier to reference
sH = Screen.Height;
//relative to screen size
if(GUI.Button(Rect(sW*.1, sH*.05, sW*.1, sH*.1), "Button Based on Screen Size"))
{
//do stuff
}
//fixed
if(GUI.Button(Rect(10, 5, 50, 5), "Fixed Button"))
{
//do stuff
}
//Dynamic button, change the starting x position based on the value you can change in the inspector, sometimes it's best to use multiple variables for all elements of the Rect (x1, x2, x3);
if(GUI.Button(Rect(sW*x0, 5, 50, 5), "Dynamic Button"))
{
//do stuff
}
}
Hope this kinda helps. Good Luck.
So would it be possible to add a button outside of the initial screen size and scroll the camera to it's location or should i use game objects?
No. You can't move the camera to see the GUI elements, they need to be moved. You could just change the GUI element's position in runtime to move into view. I use iTween for that, it's perfect.
Your answer
Follow this Question
Related Questions
Bigger size button help 2 Answers
c# Quit button wont quit game 1 Answer
How do i cal second question in a window? 1 Answer
Changing screen resolution with GUI Buttons 0 Answers
Unity 4.6 GUIUtility.hotControl 1 Answer