- Home /
move a gui element
hi everybody, I'm trying to move vertically a simple gui button, but I can't figure it out. I'm not a programmer and my gui is very simple, I've tried to move a cube through animation and itween and it works, but how i can move the button? here's my code
var controlTexture : Texture;
var customSkin : GUISkin;
function OnGUI () {
GUI.Label (Rect (Screen.width/2-250, Screen.height/2-65, 500, 150), controlTexture);
GUI.Box (Rect (0,0,150,100), "Top-left");
GUI.Box (Rect (Screen.width - 150,0,150,100), "Top-right");
GUI.Box (Rect (0,Screen.height - 100,150,100), "Bottom-left");
GUI.Box (Rect (Screen.width - 150,Screen.height - 100,150,100), "Bottom-right");
if (GUI.Button (Rect (Screen.width/2-250, Screen.height/2 - 170, 500, 100), "collocazione A")) {
isLoading = true;
Application.LoadLevel("level1"); // load the game level.
print ("you clicked collocazione A");
}
if (GUI.Button (Rect (Screen.width/2-250, Screen.height/2 + 70, 500, 100), "collocazione B")) {
print ("you clicked collocazione B");
}
}
and another question: I used Application.LoadLevel to load a level with another gui, how can I simply load a new window or label when I click the button?
thank you!
Actualy the GUI is reloaded with each frame, so if the only thing you want to change is the GUI you only have to make a new function for each additional GUI you need, and then call the corresponding function. As for your moving button, i don't quite undestand what you're trying to do here, even with the code. Can you explain it in more detail?
thank you for the answers. The code is just my actual gui, I want to move out of the camera view the button "collocazione A" when I click it. I've looked for a solution through the web and I've found that a solution could be itween but with my little knowledge of program$$anonymous$$g I can't use it on a gui.button, only on a cube.. do you know other ways to move a gui element? I've tried also simple keyframe animation but it still works only on the cube..
Answer by cj_coimbra · Dec 02, 2011 at 03:38 PM
private bool showButton = true;
void OnGUI()
{
if (showButton)
{
if (GUI.Button(new Rect(100,100, 120, 25), "Sample button..."))
showButton = false;
}
}
Maybe this is what you are looking for? The only problem is that whenever you want your button back, you will need to find a suitable place to set showButton variable to true again.
thank you for the answer. It's not really what I'm looking for, because I would like to move the button, not simply "hide" it. I would like to create an animation, like a vertical sliding. e.g the "Animating UnityGUI (Label, Button, Box, TextArea, etc...)" example in http://itween.pixelplacement.com/examples.php#movingonpath
I think I'm gonna buy the example from this page, but then I would like to use it correctly! XD
Hmm ok. Try this (click to move out of screen, press return to move back into the screen space):
void Start ()
{
buttonRect = new Rect(buttonX, buttonY, buttonWidth, buttonHeight);
}
void Update () {
if (moveOut)
{
if (buttonRect.x != escapeX)
buttonRect.x = $$anonymous$$athf.$$anonymous$$oveTowards(buttonRect.x, escapeX, buttonSpeed * Time.deltaTime);
else
moveOut = false;
}
if (moveIn)
{
if (buttonRect.x != originalX)
buttonRect.x = $$anonymous$$athf.$$anonymous$$oveTowards(buttonRect.x, originalX, buttonSpeed * Time.deltaTime);
else
moveIn = false;
}
if (Input.Get$$anonymous$$ey("return"))
{
moveIn = true;
moveOut = false;
}
}
private Rect buttonRect;
private float buttonSpeed = 500.0f;
private float buttonX = 200;
private float buttonY = 200;
private float buttonWidth = 120;
private float buttonHeight = 30;
private float escapeX = -120;
private float escapeY = 200;
private float originalX = 200;
private float originalY = 200;
private bool moveOut = false;
private bool moveIn = false;
void OnGUI()
{
if (GUI.Button(buttonRect, "Sample button..."))
{
moveOut = true;
}
}
thanks man for your help, really appreciated.. but it's like just reading in japanese for me XD it's no js but cs right? because in both version (js and cs) I'm having error such "A namespace can only contain..", it should be a formatting error. I find easier to write in js, so I think (or it's just what I have undestood) that I have to declare a var for every value and then a moveOut method to trigger like in your previous example.. I'm looking around to solve, thank you again
PS. do you know any unity scripting tutorial for newbie??
The idea is to control your GUI element rectangle´s coordinates. If you click the button, it will set true the value of moveOut, so the function $$anonymous$$athf.$$anonymous$$oveTowards will move it towards the escapeX variable position. The same when you press return. In this case, moveIn will be set true and then it will move towards the originalX. You usualy put stuff that need to happen over time in the Update function. Those inner conditions are there to check if the GUI element rectangle is already at the target destination. That´s just an example. Perhaps if you describe your concrete case I can help you more on this issue.
I've successfully put your script and it works correctly. my idea is to create a simple augmented reality app, when the camera detects the marker I want the gui to appear (and this is the first animation, a simple scaling of a window). then when I click the button "collocazione A" or "collocazione B" I want to open other windows (and this is the second animation, the current windows slide away and other windows slide in)... the examples from itween fits exactly what I want to do, but I can't figure out how to do them.
Your answer
Follow this Question
Related Questions
Editing buttons created in 'for' loop. 0 Answers
GUI.Label positioning for many device resolutions 1 Answer
GUI, making main page lead into an instructions page 2 Answers
How to get Rect from scene Button ? 1 Answer
Unity 5 GUI system 1 Answer