- Home /
GUI and buttons
Can i make a GUI by myself and put it into a prefab, so then i can spawn it? If yes, can it's sizes be adjustable to screen-size? And how can i check for button pressing not by giving GUI.Button(new rect(...)) coordinates of rectangle, but by asking for it's name in GUI prefab?
Answer by Piflik · May 15, 2012 at 08:55 AM
'GUI.Button(new Rect(coordinates))' is the declaration of the button, so without it, you don't have one. You can try to store the button in a variable and use that in an if statement, but I have no idea, if that works (it should...).
testButton = GUI.Button(new Rect(coordinates));
if(testButton) {
//do stuff
}
No... Im talking not about putting "new Rect()" into variable. I've got a prefab with many daugther-buttons in it... How can i use if() asking it by it's name in prefab?
You have to first give the button a name. The displayed label is just a string, it doesn't identify the button. Try what I said. If it works, it works, if not it should give you an error.
The thing u are trying to explain me is to make all the GUI by the script. Here is another thing: i've got READY GUI, contained into prefab. And this prefab has all the lables/textures/buttons and so on as daughter-objects... Each button has it own name into Inspector. By pressing key i place this prefab on the game scene. So can i use the name of button, given in prefab, into Inspector, to call for fucntion?
Answer by KingCharizard · May 15, 2012 at 03:28 AM
Yes u can make a empty gameObject with a GUI script on it and make it a prefab. Yes the gui can be resized. About your last question I dont know what your asking but try looking at this for more info about working with the GUI
to find and use scripts on other prefabs, try : http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
http://www.youtube.com/watch?v=jC3QO8QID80&feature=relmfu
and possibly : http://unity3d.com/support/documentation/ScriptReference/GameObject.Send$$anonymous$$essage.html
http://www.youtube.com/watch?v=4j-lh3C_w1Q&list=UUxvmZ5LIvbIZ7SVyGI6o6Xg∈dex=4&feature=plcp
Answer by kolban · May 15, 2012 at 03:27 AM
Have a good read at the GUI Scripting Guide found here.
http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html
The GUI is defined in Script and a Script is then attached to a GameObject and the OnGUI() method called on the Script to render the GUI. So ... with this in mind ... you can create a Script, attached that to a Game Object and then make a Prefab out of the Game Object. However, having achieved all of this, it still isn't cleat what it is you actually want to achieve.
I'm talking about the pressing the button. By googling i found the method: if(GUI.Button(new Rect(coordinates))){ } The GUI prefab, that i want to make, will contain many buttons. How can i remake this method to find press no by coordinates, but by it's name?
Your answer
Follow this Question
Related Questions
GUI Creates a prefab? 1 Answer
How To Make GUI/Dragable Object - Att: Video 3 Answers
On Click paramaters disappear from button prefab? 5 Answers