Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 Dreamblur · Nov 21, 2010 at 03:02 AM · prefabscreenpauseguitextcenter

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).

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

2 Replies

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

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.

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 Dreamblur · Nov 21, 2010 at 03:23 AM 0
Share

Thanks for the reply. That's exactly what I needed.

avatar image
1

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);

}

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 Dreamblur · Nov 21, 2010 at 03:22 AM 0
Share

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

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

No one has followed this question yet.

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


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