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
2
Question by Aram-Azhari · Nov 06, 2013 at 06:41 PM · buttonpositionguilayout

How to get a control position on a guilayout?

How can I get the position of a control that was created using GUILayout? Assume the code:

 void OnGUI()
     {
     GUILayout.BeginHorizontal();
     if (GUILayout.Button("Button"))
       {
       //...
       }
     GUILayout.EndHorizontal();
     }


Is it possible to get the position of the displayed button within OnGUI?

Thanks

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Radivarig · Oct 19, 2014 at 06:36 PM

 void OnGUI(){
     Rect lastRect;
     GUILayout.BeginHorizontal();
     {
         if (GUILayout.Button("Button")){/*...*/}
         lastRect = GUILayoutUtility.GetLastRect();    //button rect    
     }
     GUILayout.EndHorizontal();
     lastRect = GUILayoutUtility.GetLastRect();    //horizontal area rect
 }

GetLastRect

Comment
Add comment · Show 1 · 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 Rodolfo-Rubens · Nov 04, 2016 at 01:07 AM 0
Share

I'm trying to get the rect after a EndVertical but I'm getting inconsistent values (I'm using printing the values and I'm getting 3 different rect values). I also tried checking if the current event is a repaint but still not luck. Any idea? my code frag:

Event e = Event.current;
//...
GUILayout.EndVertical();
if(e.type == EventType.Repaint) lastRect = GUILayoutUtility.GetLastRect();
Debug.Log(lastRect);
//...

avatar image
0

Answer by RyanZimmerman87 · Nov 06, 2013 at 09:21 PM

You need to define the position of the button on your GUI layout so you don't need to get the position.

For example:

 public GUIStyle styleExample;
 
 public Texture2D buttonTexture;
 
 void OnGUI()
 {
 if (GUI.Button (new Rect (0, 0, 100, 100), buttonTexture, styleExample))
 {
 //code for button press
 } 
 }

In that example you are creating a button which will look like the buttonTexture (.png imports work fine for this).

You are using the styleExample to help format the button automatically so it has no strange borders around it. You can further customize your fonts for GUI.Label or other GUI visual properties in the styleExample from your script in the Unity Editor.

And then you are placing the button at position (0, 0) which is the top left corner.

The button is sized (100, 100) which is 100x100 pixels.

So that is how I go about knowing where the position and size of the button will be.

There is a lot more details of how you can make it format to any screen size, resolution etc. which you can find in many other answers here.

If you are looking for other quick references I believe I tried to explain how I use the OnGUI() system in more detail in this thread:

http://answers.unity3d.com/questions/540946/alternate-solution-to-ongui-for-gui.html

Comment
Add comment · Show 1 · 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 Aram-Azhari · Nov 06, 2013 at 10:05 PM 0
Share

Thanks ryan. I will have a look at that. Although I will need to use guilayout.begin and end and the number of buttons I add are not fixed. If I define a Rect Array then it may solve the issue partially.

The whole point is to use guilayout and not fall back on the rect parameter of gui.

In the meantime, I found another function that helps me to get the correct rectangle without manual definition of rect:

 var TheRect = GUILayoutUtility.GetRect(new GUIContent("Button"),GUI.skin.button);
 // Then
 if(GUI.Button(TheRect,"Button",GUI.skin.button))
 {
    ...
 }

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

18 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

Related Questions

Finding position of button in guilayout grid 0 Answers

How to Position a GUI at the Top-Right of the Screen? 2 Answers

How do you find the rect of a GUILayout item? 1 Answer

Another draw call question 1 Answer

How do you change the margin, padding, and border size of GUILayout buttons? 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