• 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
Question by Demigiant · Dec 01, 2011 at 03:27 PM · c#guiwindowguilayout

Having a GUILayout Window position immediately at screen center

Hello,

I need to have a GUILayout Window appear at the exact center of the screen, and I'm using this (where winRect is a stored Rect variable):

 winRect = GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );
 winRect.x = (int) ( Screen.width * 0.5f - winRect.width * 0.5f );
 winRect.y = (int) ( Screen.height * 0.5f - winRect.height * 0.5f );

Problem is, the window will be correctly positioned only since the second time it's drawn (because the first time the window rect has not yet been generated), and thus it will appear for an instant in an incorrect position.

Anyone knows a way to reposition a window AFTER GUILayout.Window has been called, but BEFORE it is actually drawn? I suppose this is not possible, but: any workarounds? Please note that I'm talking about liquid/GUILayout windows, where width and height is not known until they're created.

Thanks for any help :)

Comment
vigrid

People who like this

1 Show 4
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 vigrid · Dec 07, 2012 at 08:07 PM 0
Share

Good question - I'm dealing with exactly the same problem at the moment. I'd upvote if I could.

avatar image Demigiant · Dec 07, 2012 at 08:14 PM 0
Share

Personally, I didn't find a solution, and since then stopped using Unity's GUI (which anyway is a system hog) :P

avatar image vigrid · Dec 07, 2012 at 09:12 PM 0
Share

I've got an answer to this question, although I'd like to ask what did you switch to? NGUI?

avatar image Demigiant · Dec 07, 2012 at 09:23 PM 1
Share

I heard lots of good stuff about NGUI, so I'd recommend it even if I never used it (alos, its developer was just hired by Unity to work on the new version of Unity GUI). Personally, since I have and love 2D Toolkit, I built a framework to easily use it also for the GUI (by the way, I'm remaking it from scratch and posted it on Google Code among my HOUnityLibs, in case you're interested - though for now it's good only for buttons: http://code.google.com/p/hounitylibs/).

5 Replies

  • Sort: 
avatar image
Best Answer

Answer by vigrid · Dec 07, 2012 at 09:15 PM

Calling `GUILayout.Window` again repositions the window without any flicker, nor extra draw calls.

 winRect = GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );
 winRect.x = (int) ( Screen.width * 0.5f - winRect.width * 0.5f );
 winRect.y = (int) ( Screen.height * 0.5f - winRect.height * 0.5f );

 // Added
 GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );

Rationale: My guess is Unity doesn't actually draw the Window, but manages its position in this call, while building a draw call list to be sent. Calling this again invalidates the list created previously for that window.

Comment
Demigiant

People who like this

1 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 Demigiant · Dec 07, 2012 at 09:19 PM 0
Share

I like this one even more! Marked :)

avatar image

Answer by vigrid · Dec 07, 2012 at 08:38 PM

Is it? Can you explain how is it a system hog? Also, a workaround I would do - I would draw the GUI on the first frame it pops up with Alpha set to zero.

Comment
Bunny83
Demigiant

People who like this

2 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 Demigiant · Dec 07, 2012 at 08:53 PM 0
Share

Smart workaround. If you write it as a complete answer, I will vote it as the correct one :) About Unity GUI being a system hog, you can check anywhere on the internet, since everybody agrees: especially for mobile development it is definitely un-performant.

avatar image Demigiant · Dec 07, 2012 at 09:25 PM 0
Share

P.S. ah, never realized there was a "convert comment to answer" button :P

avatar image

Answer by belvita · Nov 29, 2013 at 04:02 PM

do not use GUILayout it is a waste of time .try this

private void OnGUI()

 {

GUI.Box(new Rect(Screen.width / 2-75, Screen.height / 2-160, 200, 100), "");

         //if the button is pressed means if exists
         if(GUI.Button(new Rect (Screen.width / 2-75,Screen.height / 2-160,30,30),sand))
         {
 }
         // Make the second button
         GUI.Button(new Rect (Screen.width / 2-45,Screen.height / 2-160,30,30),"");

     }
         
     
 } 
 
Comment
MrProfessorTroll

People who like this

1 Show 0 · 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

Answer by Robacks · Nov 29, 2013 at 12:07 AM

Calling GUILayout.Window again repositions the window without any flicker, nor extra draw calls.

 winRect = GUILayout.Window( winId, winRect, WindowFunction, "Window Title" );
 winRect.x = (int) ( Screen.width * 0.5f - winRect.width * 0.5f );
 winRect.y = (int) ( Screen.height * 0.5f - winRect.height * 0.5f );
 // Added
 GUILayout.Window( winId, winRect, WindowFunction, "Window Title" ); 

Rationale: My guess is Unity doesn't actually draw the Window, but manages its position in this call, while building a draw call list to be sent. Calling this again invalidates the list created previously for that window.

When you use "EditorGUILayout" inside your "WindowFunction", this workaround will make all controll positions performed inside "WindowFunction" invalide. Therefore this solution is not valide for a proper functional gui extension.

Comment

People who like this

0 Show 0 · 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

Answer by Robacks · Nov 29, 2013 at 12:04 AM

I found a working aproach, which does not currupt the "EditorGUILayout" inside your "WindowFunction":

 lockRect = GUILayout.Window(0, lockRect, WindowFunction, "Map Editor Modes");
 lockRect.x = (Screen.width*0.5f - lockRect.width*0.5f);

this will work with "lockRect" beeing a class member variable.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying a tooltip with GUILayout.Window 1 Answer

Multiple Cars not working 1 Answer

Is it possible to make EditorGuiLayout.ObjectField(Texture2D) look like ObjectField(String,Texture2D)? 2 Answers

Help with GUILayout and tooltip(C#) 1 Answer

Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 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