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 PirateMonkey · Jul 26, 2014 at 09:57 PM · guidrawtexture

GUI.DrawTexture isn't centred. Help?

I've been trying to fix this issue for a while, but for some reason can't find an answer. In my game, I have a speed boost bar that I would like centred at the top of the screen. I have been using the following script:

 var boostPercent : float;
 var xPos : float = (Screen.width/2);
 var yPos : float = 30;
 var backgroundWidthBoost : float = Screen.width/5;
 var boostWidth : float;
 var height : float = backgroundWidthBoost/15;
 var boostRemainingBehindTexture : Texture;
 var boostRemainingTexture : Texture;
 
 function OnGUI()
 {
 
 boostPercent = GameObject.Find("Suber").GetComponent(SubControl).boostTimer / GameObject.Find("Suber").GetComponent(SubControl).boostTime;
 boostWidth = (boostPercent * backgroundWidthBoost * -1) + backgroundWidthBoost;
 
 //Draws a rectangle of x,y position, rec width and height, and wrapped in a specified texture
 GUI.DrawTexture(Rect(xPos,yPos,backgroundWidthBoost,height), boostRemainingBehindTexture, ScaleMode.StretchToFill, true, 1.0f);
 GUI.DrawTexture(Rect(xPos,yPos,boostWidth,height), boostRemainingTexture, ScaleMode.StretchToFill, true, 1.0f);
 
 }


For some reason, even though I specify the x position as 0.5 * Screen.width, the bar shows up way in the left hand corner (see image below). Any insight would be appreciated. Thanks!

alt text

screen shot 2014-07-26 at 3.54.55 pm.png (52.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 26, 2014 at 10:31 PM

First, a Rect is (x, y, dx, dy), so the x and y are the upper left corner of what you need to display, so to fix you need to subtract 1/2 of the width (dx) from x to move it back. Second, the way you are calculating xPos will happen only once when the script is attached to a game object and use whatever width you have for the screen in the editor. You need to move these calculations into the Start() function or even into OnGUI() if there the device you are targeting can change screen resolution (i.e. be rotated). And since these are calculated values and not set in the inspector, make them private:

 private var xPos : float;

One way I often do these calculations is to create a rect of the right size with a position of (0,0):

  var rect : Rect = Rect(0,0,someWidth, someHeight);

Then I can place the center of the rect at some specific position by:

  rect.center = new Vector2(Screen.width/2, 40);
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 PirateMonkey · Jul 27, 2014 at 05:31 PM 0
Share

Awesome! Putting xPos into OnGUI() solved everything

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Doubt with GUILayout 0 Answers

Fading an image drawn with GUI.DrawTexture 1 Answer

texCoords rect for GUI.DrawTextureWithTexCoords 3 Answers

Scale Car Speedometer with Screen Size (made by Andeeee) 1 Answer

GUI Rotation and Depth problem 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