- Home /
Problem with GUI.Button
Hi all, I'm starting with Unity and since yesteraday I have a problem that I can't resolve. I'm using GUI.Button to create 7 buttons, and I should be able to move this buttons with the mouse (like if this button was the piece of a puzzle). Can somebody explain me how to do that?
Thanks!! Ana
Do you want the buttons to also act like normal buttons? In any case, I can see a way to do it, but it's not nearly as straightforward as I think you think it is. $$anonymous$$ind if I answer in C#? I'm not very good at JS.
The game that I'm trying to do have a set of 7 button that represent: move to front, move to left, move to right, jump, light, function1 and function2. The idea is that the user move this buttons and put them in order, so the sequence of steps represent the movement of a robot. $$anonymous$$y first intention is move this buttons around the screen, and another partner is working on the movements of the robot. I will appreciate your help! Thanks in advance!
Ana.
Answer by Hurricanes · Nov 14, 2011 at 09:19 PM
Do you really need these to be buttons ? I mean, a button is meant to be a clickable area on the screen. It seems what you want to do is just have 7 boxes displayed on the screen, which can be moved by the user in a certain order. If you don't need the "click" functionality of a button, you might want to use simple planes with a 2D texture to represent your 7 states.
Answer by cj_coimbra · Nov 14, 2011 at 03:14 PM
Try this:
private Rect sampleButton = new Rect (100,100,80,25);
private bool sampleButtonSelected = false;
void OnGUI()
{
if (sampleButtonSelected)
{
sampleButton.x = Input.mousePosition.x - 40;
sampleButton.y = Screen.height - Input.mousePosition.y - 10;
if (GUI.Button(sampleButton, "Sample!"))
{
sampleButtonSelected = !sampleButtonSelected;
//do something
}
}
else
{
if (GUI.Button(sampleButton, "Sample!"))
{
sampleButtonSelected = !sampleButtonSelected;
//do something
}
}
}
This is just one button sample, but if you get the idea, you sure can reproduce it to the others...
Thank you so much for the help!! I'm going to try this as soon as I get home! :)
Your answer

Follow this Question
Related Questions
GUI Buttons and game objects? 1 Answer
What method should I use for making a drag 'n drop based game? 1 Answer
How to change the game-window style? 0 Answers
Touch on GUI.Button HELP!!!! 3 Answers
Change GUILayout.Button fontsize 0 Answers