- Home /
How to Position a GuiText Prefab at Center of Screen
I want the game to display a prefab (GUIText: "GAME PAUSED") on the center of the screen whenever the game gets paused no matter what the resolution of the game window is. How do I go about doing that?
Below is the script I have written so far:
var bGamePaused : boolean;
var gamePausedPrefab : GameObject; var gamePausedText : GameObject;
function Update() { if(Input.GetKeyUp(KeyCode.P)) { if(!bGamePaused) { Time.timeScale = 0;
gamePausedText = Instantiate(gamePausedPrefab, Vector3(Screen.width / 2, Screen.height / 2, camera.nearClipPlane), Quaternion.identity);
}
else
{
Time.timeScale = 1;
Destroy(gamePausedText);
}
bGamePaused = !bGamePaused;
}
}
This script isn't displaying the prefab anywhere on the screen at the moment. I know that I also have to subtract half the width of my prefab from the x-position and the half the height of my prefab from the y-position, but that's currently the least of my worries as the prefab itself is not being displayed in front of the camera anyway. I know that the code works because the prefab shows up on the lower left corner of the screen when I instantiate the prefab at a position of (0, 0, 0).
Answer by Eric5h5 · Nov 21, 2010 at 03:08 AM
GUIText and GUITexture objects use viewport space, which means X=1 is the right side and Y=1 is the top. So (.5, .5) is the middle.
Answer by denewbie · Nov 21, 2010 at 03:07 AM
Its easier if you just attach it to the script and just call it to show when you pause. Something like this:
var myTexture:Texture2D; var myTextureWidth:float = 200; var myTextureHeight:float = 100; var isPaused = false;
function OnGUI(){
if (isPaused)
GUI.drawTexture(Rect( (Screen.width - myTextureWidth)*0.5,(Screen.height - myTextureHeight)*0.5, myTextureWidth, myTextureHeight), myTexture);
}
Thanks for the reply. I'll be sure to remember your suggestion the next time I need to display a GUIText. But for now, Eric5h5's reply satisfies my inquiry.
Your answer
Follow this Question
Related Questions
C#, How do I make the GUIText Show up if Instantiate from a prefab. 1 Answer
Aiming to center 0 Answers
Need help getting the screen to fade. 2 Answers
How Do I Center A GUI Label? 5 Answers
Problem Trying To Get GUI.Label Centred 2 Answers