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 SirGive · Sep 26, 2010 at 11:20 PM · guitexturescaling

Scaling a GUI image

How does one scale GUI textures without using GUITexture object?

// GameHUD: Platformer Tutorial Master GUI script. // This script handles the in-game HUD, showing the lives, etc.

var guiSkin: GUISkin; var nativeVerticalResolution = 1200.0;

// main decoration textures: var healthImage: Texture2D; var healthImageOffset = Vector2(0, 0);

// Assign someTexture to the guiTexture var healthBar: Texture2D; var healthBarOffset = Vector2(0, 0);

// the lives count is displayed in the health image as a text counter var livesCountOffset = Vector2(450, 160);

// Cache link to player's state management script for later use. function Awake() { playerInfo = FindObjectOfType(ThirdPersonStatus);

 if (!playerInfo)
     Debug.Log("No link to player's state manager.");

}

function OnGUI () { // Set up gui skin GUI.skin = guiSkin;

 // Our GUI is laid out for a 1920 x 1200 pixel display (16:10 aspect). The next line makes sure it rescales nicely to other resolutions.
 GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1)); 

 // Health & lives info.
 DrawImageBottomAligned( healthImageOffset, healthImage); // main image with a red bar to go under hp bar
 DrawImageBottomAligned( healthBarOffset, healthBar);// green hp bar

 // Displays lives left as a number. 
 DrawLabelBottomAligned( livesCountOffset, playerInfo.lives.ToString() );    

}

function DrawImageBottomAligned (pos : Vector2, image : Texture2D) { GUI.Label(Rect (pos.x, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image); }

function DrawLabelBottomAligned (pos : Vector2, text : String) { GUI.Label(Rect (pos.x, nativeVerticalResolution - pos.y, 100, 100), text); }

function DrawImageBottomRightAligned (pos : Vector2, image : Texture2D) { var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width; GUI.Label(Rect (scaledResolutionWidth - pos.x - image.width, nativeVerticalResolution - image.height - pos.y, image.width, image.height), image); }

function DrawLabelBottomRightAligned (pos : Vector2, text : String) { var scaledResolutionWidth = nativeVerticalResolution / Screen.height * Screen.width; GUI.Label(Rect (scaledResolutionWidth - pos.x, nativeVerticalResolution - pos.y, 100, 100), text); }`

I'm currently working on a GUI for a simple 3D adventure game. I am able to place the textures on the screen. I want to create a green GUI texture on top of a red one and scale it down as the player takes damage. I used the script for the Lerpz tutorial, but I gutted it a bit. That is what I currently have.

Now I want to put the green bar onto of the health image and scale it. This would work for a GUITexure, as decreasing the width shrinks it accordingly. The only problem is that it is behind the health image. I would prefer to add the green bar in the code and scale it that way, but I have been unable to find anything that helps me to scale the Texture2D variables that I have created. Any suggestions?

This is in JavaScript, by the way.

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
3
Best Answer

Answer by Dreamora · Sep 27, 2010 at 12:27 AM

You don't scale the image. What you do is scale the rect to which it outputs (the first parameter of the GUI.Label call), so it does no longer output to the full size. if you want to "cut it off" you would just draw it to the full length and instead use GUI.BeginArea and GUI.EndArea around the GUI.Label call to facilitate the "cutoff"

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 SirGive · Sep 27, 2010 at 04:01 AM 0
Share

Thank you. That would make sense why there isn't some scale function.

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

Right constrained scalable GUITexture 0 Answers

Scaling bg gui texture without stretching it in unity 4.6 0 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

scaling guiTextures to always take up the same percentage of space on the screen? 1 Answer

Unity3d GUI Element Scaling 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