• 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 dendens2 · Apr 02, 2013 at 11:06 PM · javascriptguitexture2dscale

Scale GUI Texture From the Center.

Hi, I have a circle texture imported into unity for the crosshair. I have it become bigger every time you shoot. However, I don't know how to make it so that it scales from the center and not from the top-right corner. Also, if it was imported as an image, can I still change the color with GUI.backgroundColor? I am asking t$$anonymous$$s because when I imported it it became lighter. Thanks!

 var crosshairTexture: Texture2D;
 var crosshairPosition: Rect;
 var crosshairActive = true;
 var playerWalkScript: PlayerMovementScript;
 var gunScript: GunScript;
 
 function Start()
 {
 
     playerWalkScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);    
 
 }
 
 function Update()
 {
     
     gunScript = playerWalkScript.currentGun.GetComponent(GunScript);
     crosshairPosition = Rect(((Screen.width )  / 2), ((Screen.height ) / 2), crosshairTexture.width * gunScript.currentAccuracy * 3, crosshairTexture.height * gunScript.currentAccuracy * 3); 
 
 }
 
 function OnGUI()
 {
 
     if(crosshairActive == true)
     {
     
         GUI.DrawTexture(crosshairPosition, crosshairTexture);
     
     }
     
     if (gunScript.ammoClip == 0)
     {
     
         GUI.backgroundColor = Color.red;
         
     }
     
 
 }
Comment

People who like this

0 Show 3
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 dendens2 · Apr 03, 2013 at 04:09 AM 0
Share

anyone? help :(

avatar image hirenkacha · Apr 03, 2013 at 04:20 AM 0
Share

Do your gunScript.currentAccuracy updates per frame in the script?

avatar image dendens2 · Apr 03, 2013 at 04:56 AM 0
Share

Yes, but that's not the problem. It scales, just not in the correct way. It scales from the top left and not from the center, which is what I want.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Apr 03, 2013 at 06:09 AM

One easy way to t$$anonymous$$ng about t$$anonymous$$s problem is that you want to "inflate" the rect w$$anonymous$$le maintain the same center position. It means you want to offset the anchor by 1/2 of the increase in width and height, and you want to increase the width and height by the same amount. Say you want to increase the width by 100 pixels and the height by 80 pixels.

 halfWidth = 100 / 2;
 halfHeight = 80 / 2;
 
 crosshairPosition.x -= halfWidth;
 crosshairPosition.y += halfHeight;
 crosshairPosition.width += halfWidth;
 crosshairPosition.height += halfHeight;

The resulting rectangle will have the same center point but is now bigger.

Note you can roll these calculations into a single step rather than first calculating crosshairPosition and then inflating the rectangle, but t$$anonymous$$s is a way to t$$anonymous$$nk about the problem.

Comment

People who like this

0 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 dendens2 · Apr 03, 2013 at 08:24 AM 0
Share

Thank you!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnGUI and Event.current 2 Answers

GUI opacity. 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Scaling a 2D Texture (Image) 0 Answers

How do I use function OnGui to display a 2D texture? 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