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
0
Question by Michael 12 · Dec 02, 2011 at 11:56 PM · gui

Center GUI Window to screen Middle?

I actually have two questions here and I'm not sure if I should trouble people too much with asking twice so I'll post both my problems with this script here:

1- How can I make this so it fits in the middle of the screen for all screen resolutions? I noticed today that it fits perfectly on my screen middle when played on the stand alone player at 1680 X 1050 But NOT centered correctly if viewed at any other screen resolution???

2- I am having extreme difficulty trying to figure out and find any information on how to freeze my background when my pop up window is activated. So far all I've managed to do is stop the fps player from being able to walk around and limited the mouselook to only move horizontally so I'm still chasing my window around the screen in order to click on any of the buttons... Very frustrating.

3- I noticed that another problem I'm having with this is it seems to ignore this part of the code where I create a duplicate of this script for another T Shirt Pop Up Window and re name the "doWindow0" to "doWindow2"???

 if (doWindow0) {
 
          obj.GetComponent(FPSInputController).enabled = false;
 
          GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
 
        } 
 
        
 
           if (!doWindow0) {
 
          obj.GetComponent(FPSInputController).enabled = true;
 
          GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
 
        }

I iave no idea why it's doing this. Perhaps there is a better way of handling these pop up windows as I need to create a hell of a lot of them??

Here is my Code I have done so far:

     var doWindow0 : boolean = false;
 
 
 
 var aTexture : Texture;
 
 var closeIcon : Texture2D;
 
 var buyNowButton : Texture2D;
 
 
 
 var obj : GameObject; // first person controller
 
 function OnGUI () {
 
 GUI.backgroundColor = Color.black; //Background Color of Window
 
 GUI.contentColor = Color.yellow;   //Color of text within my Window
 
 
 // Make sure we only call GUI.Window if doWindow0 is true.
 
 if (doWindow0)
 
 GUI.Window (0, Rect (350,250,800,500), DoMyWindow, "Suicide of T-Bear T-Shirt.");
 
 
 
 ///// Stops player from walking around while viewing the pop up window
 
 if (doWindow0) {
 
              obj.GetComponent(FPSInputController).enabled = false;
 
              GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
 
            } 
 
            
 
               if (!doWindow0) {
 
              obj.GetComponent(FPSInputController).enabled = true;
 
              GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
 
            }   
 
 }
 
 // Make the contents of the window
 
 function DoMyWindow (windowID : int) {
 
 //This is where I place my Product Photo @ 402 X 402 pixels in size
 
 if(!aTexture){
 
 Debug.LogError("Assign a Texture in the inspector.");
 
 return;
 
 }
 
 GUI.DrawTexture(Rect(10,70,402,402), aTexture);
 
 // This is where I add my descriptive product text NOTE: Be sure to include the "\n" in between your lines of text
 var stringToEdit : String = "The attempted suicide of T. Bear T-Shirt\nInspird by a T.V. episode of Supernatural\nComfortable, casual and loose fitting, our heavyweight t-shirt will quickly\nbecome one of your favorites. Made from 6.0 oz, pre-shrunk 100% cotton,\nit wears well on anyone. We’ve double-needle stitched the bottom and\nsleeve hems for extra durability. Imported.";
 
 stringToEdit = GUI.TextArea(Rect(425,75,360,300), stringToEdit, 500);
 
 //This is my Web Link Button
 
 if (GUI.Button( Rect(610,430,179,60), buyNowButton)) {
 
 Application.OpenURL("http://www.zazzle.com/t_bear_suicide_t_shirt-235455214631112735");
 
 }
 if (GUI.Button (Rect (752,20,30,30), closeIcon))
 
 doWindow0 = false;
 }
 
 //This is my Close GUI Window Button
 
 function OnMouseDown()
 
 {
 
 doWindow0 = true;
 }












Comment
Add comment · Show 1
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 jahroy · Dec 03, 2011 at 07:28 PM 1
Share

You shouldn't keep adding questions to your original question.

The point of Unity Answers is to ask questions that can be helpful to others.

It's not meant to provide a personal tutorial zone.

You should separate each question and try to ask it in a way that might help other people having similar issues.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jahroy · Dec 03, 2011 at 02:53 AM

Here's a simple function that returns a centered rectangle.

The input is a rectangle. The output is the input rectangle centered.

Below is an example of how you might use it in an OnGUI function...

/* return the centered version of someRect */

function centerRectangle ( someRect : Rect ) : Rect { someRect.x = ( Screen.width - someRect.width ) / 2; someRect.y = ( Screen.height - someRect.height ) / 2;

 return someRect;

}

/ example usage /

function OnGUI () { var theRect : Rect = Rect(350, 250, 800, 500); var theMsg : String = "Happy Dancing Bear T-Shirt"; var winId : int = 0;

 /* center the rectangle */

 var centeredRect : Rect = centerRectangle(theRect);

 GUI.Window (winId, centeredRect, DoMyWindow, theMsg);

}

For question 2...

You can just place the code that draws your window inside of an if statement like this:

if ( isWindowVisible ) {
    GUI.Window(theId, theRect, theFunction, theTitle);
}

Elsewhere you can just ignore certain code if isWindowVisible is true.

It looks like you're already doing that within the local script.

You probably just have to make another script do the same thing, which will probably involve getting two scripts communicate with each other... Luckilly that's one of the most covered topics on this site.

There are over one bazillion questions about that...

There's also some nifty documentation.

Comment
Add comment · Show 4 · 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 Michael 12 · Dec 03, 2011 at 02:01 PM 0
Share

Thanks jahroy, but I'm not really getting how I intergrate this with what I have, I can;t seem to figure out where to place things??

avatar image jahroy · Dec 03, 2011 at 07:17 PM 0
Share

I've shown you a function named centerRectangle that you can use anywhere.

The idea is to use that function to adjust your rectangles before you use them to draw GUI controls.

The OnGUI function in my code is an example of how you might use it.

$$anonymous$$ore specifically, where you have this in your code:

 GUI.Window (0, Rect (350,250,800,500), Do$$anonymous$$yWindow, "Suicide...");

You could replace it with this:

 GUI.Window (0, centerRectangle(Rect (350,250,800,500)), Do$$anonymous$$yWindow, "S...");


It's even easier to understand if you write it lke this:

 var windowId  :  int     =   0;
 var someRect  :  Rect    =   Rect(350, 250, 800, 500);
 var someTxt   :  String  =  "Happy Bear Likes Clean Code";

 /* center the window rectangle */

 someRect = centerRectangle(someRect);

 /* pass the centered, scaled rect to the window function */

 GUI.Window(windowId, someRect, Do$$anonymous$$yWindow, someTxt);


Notice I pass the rectangle that you've declared into my function and pass the result to the GUI.Window function.

The basic idea is this: You take the rectangle that you're declaring with hard coded numbers and you pass it to the function named centerRectangle. That function returns a rectangle that is centered and scaled based on the screen resolution.

In general it's a very bad idea to use hard coded numbers in your code. It makes things hard to maintain if they need to be changed.

One thing that's great about Unity is the Inspector.

In s$$anonymous$$d of typing all those numbers, you can put something like this at the top of your script:

 var windowRectangle : Rect;

This will cause a slot named Window Rectangle to appear in the Inspector when you select an object with your script attached to it.

If you use code like this (in s$$anonymous$$d of your number-based approach), you will be able to tweak the size and location of your rectangle in the Inspector while playing and testing your game:

 GUI.Window(0, windowRectangle, Do$$anonymous$$yWindow, "Happy Bear!");

Hope this helps!

avatar image Michael 12 · Dec 04, 2011 at 01:41 AM 0
Share

Thanks jahroy, $$anonymous$$uch easier to understand :) $$anonymous$$uch, much thanks :)

avatar image Smoke Tear · Aug 24, 2012 at 01:46 AM 0
Share

How do add the GUI.Buttons in the script? i know how to if u do the buttons are not inside the box

avatar image
0

Answer by Aleron · Dec 03, 2011 at 12:05 AM

It's because you are using absolute coordinates in your window Rect. To have it calculate based on the actual screen size use:

 GUI.Window (0, Rect (Screen.width - 400, Screen.height - 250,800,500), DoMyWindow, "Suicide of T-Bear T-Shirt.");

The first two parameters for the Rect constructor are the screen width minus half your window width and the screen height minus half your window height (for reference if you use a different size window at some point).

I hope that helps!

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 Michael 12 · Dec 03, 2011 at 12:59 AM 0
Share

Thanks I changed it to: GUI.Window (0, Rect (Screen.width - 840, Screen.height - 525,800,500), Do$$anonymous$$yWindow, "Suicide of T-Bear T-Shirt.");

Based on my game resolution of 1680 X 1050 and in the editor it still seems to stuff it down in the lower righthand corner of the screen?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Showing Time With GUI.Label 1 Answer

GUI works flawlessly in editor, but will not show in standalone build. 1 Answer

GUI Over AI characters 1 Answer

3D Button? 1 Answer

Is there anyway to only show a part of a GUI image during the game? (C#) 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