Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by Noxury · Aug 25, 2018 at 11:38 AM · editor-scriptingeditorwindowdraweditorguilayout

EditorGUI Get Rect of GUILayout Objects

I want to resize a draggable GUI.Window (Node) automatically to fit its content. But how can I get the defining rect of the GuiLayout Objects I want to draw in the node?

 //Gets called from EditorWindow
 public void DrawNode() {
     textContent = GUILayout.TextArea (content, GUILayout.ExpandHeight(true));
     if(GUILayout.Button("Clear")){
          textContent = "";
     }

     //rect = TextArea + Button Total size

 }

The EditorWindow draws the nodes:

 private void OnGUI(){
     BeginWindows ();
     for(int i = 0; i < nodes.Count; i++) {
         nodes[i].rect = GUI.Window(i, nodes[i].rect, DrawNodeWindow, nodes[i].title);
     }
     EndWindows ();
 }
 //function that actually draws the nodes as dragWindow
 private void DrawNodeWindow(int id) {
     if (id < nodes.Count) {
         nodes [id].DrawNode ();
         GUI.DragWindow ();
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 25, 2018 at 11:51 AM

You can use GUILayoutUtility.GetLastRect after a GUILayout control method to get the layouted rect of that control. However this will only return a valid rect when not executing the layout event. As you might know every event that is processed by OnGUI / OnInspectorGUI is paired with a layout event which is executed right before the actual event. During the layout event the rect is something like (0,0,-1,-1) afaik.


Note if you want to get the combined size of two or more elements you may want to wrap them in a seperate horizontal / vertical layout group and get the rect from that.


From your code it's not really clear where your "DrawNode" method is called from. You added the comment that it's called from "EditorWindow". Did you mean called from the GUI.Window callback?


Note that you can use GUILayout.Window instead of GUI.Window. It is able to resize itself when needed. Though it will only ever "grow" the size but never reduce it.


Note that GetLastRect does return the actual layouted rect, not the size that might be needed / wanted for the content. Layouting always depends on the space available.

Comment
Add comment · Show 2 · 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 Noxury · Aug 25, 2018 at 12:15 PM 0
Share

Thanks for your answer. I renamed DrawWindow() to DrawNode() and forgot to change that in the editorWindow too so sorry for the confusion. Edited post.

The GUILayout.Window function solved the problem for auto resizing. As you pointed out the TextArea does not shrink itself back to normal if the text is deleted, so I guess I need to wrap them in the layout group.

avatar image Noxury · Aug 25, 2018 at 12:49 PM 0
Share

I changed back to GUI.Window() to make use of the VerticalGroup but now the nodes completely disappear:

 public void DrawNode(){
     Rect contentRect = (Rect)EditorGUILayout.BeginVertical();
     textContent = GUILayout.TextArea (textContent , GUILayout.ExpandHeight(true));
     if(GUILayout.Button("Clear")){
     textContent = "";
     }
     EditorGUILayout.EndVertical();
     rect = contentRect ;
 }

Debugging prints the rect is 0,0,0,0

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

102 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Events for EditorGUILayout.Popup 1 Answer

Is it possible to store and display EditorGUILayout.Toggles? 0 Answers

Unity editor extension - create drag and drop (similar to Buildbox) 1 Answer

Custom editor window stopped showing up 1 Answer

Drawing to scene view from EditorWindow? 2 Answers


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