- Home /
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.
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"
Thank you. That would make sense why there isn't some scale function.
Your answer
Follow this Question
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