• 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
Question by PirateMonkey · Jul 25, 2014 at 12:09 AM · screendrawtexturescreen.width

GUI.Draw Texture Positioning

Hey,

So this might be a stupid question but I am somewhat new to Unity and find this confusing. I am currently making a game that uses a health bar and speed boost bar. I have been drawing them as rectangles on the screen using GUI.DrawTexture(xPosition, yPosition, etc...)

Where xPosition = Screen.width/2;

I would therefore expect the rectangle to be located in the middle of the screen (or just off since it moves from the bottom left corner), but instead it pops up maybe 1/4 way from the left. Why is it doing this?

I suspect it has something to do with screen space vs world space, but don't know enough about it. Any insight would be appreciated!

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Stormizin · Jul 25, 2014 at 12:09 AM

The question is, if your texture has, 220px of width and u put screen.width/2 your texture will be positioned from the extreme left corner in the givin (screen.with/2), so you need just do this: Screen.width/2 - 220, and bang! you got it in the middle.

Comment

People who like this

0 Show 3 · 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 25, 2014 at 12:29 AM 0
Share

But if I subtract, wouldn't that push the bar even further left? Here is a picture for reference: alt text

screen shot 2014-07-24 at 6.23.23 pm.png (56.4 kB)
avatar image PirateMonkey · Jul 25, 2014 at 12:31 AM 0
Share

Also, the code I am using for the boost bar (blue bar in the top left):

 var boostPercent : float;
 var xPos : float = (Screen.width / 2);
 var yPos : float = 30;
 var backgroundWidthBoost : float = Screen.width / 6;
 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);
 
 }
avatar image Stormizin · Jul 25, 2014 at 12:37 AM 0
Share

Did you need it to left? so subtract or right just use +

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Can you tell unity to resize the Game window by a button press? 2 Answers

Getting Device Screen Height and width in inches 2 Answers

Screen.currentResolution = Null? 1 Answer

How to set screen boundaries on a moving camera 1 Answer

Detect touch in screen at different resolutions 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