Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by John Sartain · Mar 21, 2014 at 03:54 AM · buttonbuttonsleveleasy

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"); } }

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abhishekdeb · Mar 21, 2014 at 03:58 AM 0
Share

Please format the code using Ctlr+$$anonymous$$ .. its unreadable right now.

avatar image Benproductions1 · Mar 21, 2014 at 03:59 AM 0
Share

Please format your code. If you don't know what something means, read the documentation.

avatar image John Sartain · Mar 26, 2014 at 02:18 AM 0
Share

Sorry BenProductions1 I did thats where i got this from i was just stuck as im a noob

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Mar 21, 2014 at 08:31 AM 0
Share

Creating an object for every single UI element is not scalable and will take a dump on your performance.

avatar image Foestar · Mar 21, 2014 at 07:03 PM 0
Share

Hasn't slown my performance down any yet and I'm using quite a few. I just disable each when not used.

avatar image Benproductions1 · Mar 26, 2014 at 09:30 AM 0
Share

@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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i set screenshot image of Scene2 to Scene1(Button) in unity. Thanks in adavance 1 Answer

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

How can I stop getting mouse position of Ui Elements? 0 Answers

Creating a level switch by using mouse function in a certain area 1 Answer

Changing Color of Material with Bool and Unity UI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges