- Home /
There has to be an easier way to place buttons
So For one can someone tell me what each number means(ex. (50, 70, 50, 75)which is x and y (first 2 right?)(next 2 scale right?)). Then can you please tell me if there is an easier way to place buttons. Im using this script: @script ExecuteInEditMode; function OnGUI() { if (GUI.Button(Rect(50,70,50,75),"Level 1")){ Application.LoadLevel("lvl1"); } if (GUI.Button(Rect(50,50,200,75),"Level 2")){ Application.LoadLevel("lvl2"); } if (GUI.Button(Rect(50,0,50,75),"Level 3")){ Application.LoadLevel("lvl3"); } if (GUI.Button(Rect(50,70,50,75),"Level 4")){ Application.LoadLevel("lvl4"); } if (GUI.Button(Rect(50,70,50,30),"Level 5")){ Application.LoadLevel("lvl5"); } if (GUI.Button(Rect(10,70,50,30),"Level 6")){ Application.LoadLevel("lvl6"); } }
Please format the code using Ctlr+$$anonymous$$ .. its unreadable right now.
Please format your code. If you don't know what something means, read the documentation.
Sorry BenProductions1 I did thats where i got this from i was just stuck as im a noob
Answer by RyanZimmerman87 · Mar 21, 2014 at 04:11 AM
The GUI.Button works like this:
if ((GUI.Button (new Rect (buttonPosition.x, buttonPosition.y, buttonSize.x, buttonSize.y), buttonTexture, GUIButtonStyle))
{
//button press logic
}
I tried to name the variables so they are self explanatory. You mentioned scale for one of the numbers which is incorrect.
buttonTexture could be replaced by a text string, generally you would want a texture there that looks like a button though.
GUIButtonStyle is a public GUIStyle variable for example:
public GUIStyle GUIButtonStyle;
You should try to format your questions though with the "Code Sample" button while you highlight the code as others mentioned.
Generally you should be able to figure out the basic questions with the documentation, but I've had a lot of trouble with learning effectively from the documentation when I was first learning all the syntax, so I'll give you the benefit of the doubt.
For example the Rect information that you need:
http://docs.unity3d.com/Documentation/ScriptReference/Rect-ctor.html
Can not be seen directly on the GUI.Button page:
http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html
All it says is "static bool Button(Rect position, Texture image);".
"Rect position" by itself may be a little misleading for a beginner because it does not make any mention of size.
Answer by Foestar · Mar 21, 2014 at 05:11 AM
Yeah I don't do things that way. I just create an GUI Texture object and call it my button. Then I add a script to it like so to enable mouse rollover to change the buttons image to highlighted and such.
function OnMouseEnter () {
guiTexture.texture = CBHover;
}
function OnMouseExit () {
guiTexture.texture = CBNormal;
}
Then when you want to do something you put it in a OnMouseUp function. To disable the button just do
gameObject.Find("ButtonName").guiTexture.enabled = false;
Also note that this can't be viewed in the scene view tab. You have to go to the game tab to see the gui textures in work.
Creating an object for every single UI element is not scalable and will take a dump on your performance.
Hasn't slown my performance down any yet and I'm using quite a few. I just disable each when not used.
@Foestar There is an overhead that comes with each GameObject
in the hierarchy (and not in the hierarchy). With only a couple objects this overhead doesn't make much of a difference, but when you consider that every single global Unity event runs in O(N)
time, you should question using a new object for every UI element, when they could really easily be coded in directly. This is even worse on lower-end platforms, such as mobile devices.
When your project stays small this doesn't matter, but when working with large projects, as I stated above, it's just not a scalable option. Actually Unity's UI interface is not scalable to begin with, but that's beside the point.