- Home /
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;
}
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.
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.
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??
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!
Thanks jahroy, $$anonymous$$uch easier to understand :) $$anonymous$$uch, much thanks :)
How do add the GUI.Buttons in the script? i know how to if u do the buttons are not inside the box
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!
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