- Home /
How do i scale health bar with resolution
How can i edit this So this scales with the resolution
static var playerHealth : int = 100;
var HealthyTexture : Texture2D;
var UnHealthyTexture : Texture2D;
var CurrentTexture : Texture2D;
GUI.Label(Rect(Screen.width/ 1.15, Screen.height/1.05, 50, 20),"Health:");
GUI.DrawTexture(Rect(Screen.width/ 1.12, Screen.height/1.05, (200 * (playerHealth / HealthMax)), 20),CurrentTexture);
GUI.Box(Rect(Screen.width/ 1.156, Screen.height/1.13, 255, 100)," ");
This was written for a 1920x1080 display
doing that messed up the positions and wont go back no matter what i do
Answer by Fornoreason1000 · Nov 02, 2013 at 03:26 AM
here is what i would do... setup a native resolution...myDisplay is 1080p but most games will run at 720p, so
Native = 1280x720
lets say you bump it up to 1080p which is fully supported the ratio between them is in this case Yration and Xratio are the same, but this will not alway be the case(e.g640x480). so we give it two vars.
Xratio = 1920 / Native.x Yratio = 1080 / Native.y
you multiply Xratio to your width and x coordinate and the latter for Yratio.
public class MyGUISystem : MonoBehaviour
{
Rect LabelRect;
Rect TextureRect;
Rect BoxRect;
Texture2D CurrentTexture;
int HealthMax = 100;
static int playerHealth = 100;
/// <summary>
/// Use this fopr Initialization
/// </summary>
public void Start()
{
UpdateRects();
}
/// <summary>
/// Call this when you chagne resoltution
/// </summary>
public void UpdateRects()
{
LabelRect = new Rect(Screen.width / 1.15f * (Screen.width / 1920.0),
Screen.height / 1.05f * (Screen.height / 1080.0),
50.0 * (Screen.width / 1920.0),
20.0 * (Screen.height / 1080.0));
TextureRect = new Rect(Screen.width / 1.12f * (Screen.width / 1920.0),
Screen.height / 1.05f * (Screen.height / 1080.0),
(200.0 * (MyGUISystem.playerHealth / HealthMax) * (Screen.width / 1920)),
20.0 * (Screen.height / 1080.0));
BoxRect = new Rect(Screen.width/ 1.156f * (Screen.width / 1920.0),
Screen.height/1.13f * (Screen.height / 1080.0),
255.0 * (Screen.width / 1920.0),
100.0 * (Screen.height / 1080.0));
}
public void OnGUI() {
GUI.Label(LabelRect, "Health");
GUI.DrawTexture(TextureRect, CurrentTexture);
GUI.Box(BoxRect, " ");
}
}
thanks for the script will go over it
also do i just put the script on my player or do i merge it into my current script
its the same script you posted... but in C# and it scales with your Resolution
O$$anonymous$$ it did not want to work in C# so i converted it to JavaScript thanks
oh yeah... make sure each value is a float not an int... you need a decimal or float because you a multiplying values that can be less than 1, if its an int, it will multiply by zero...
var LabelRect : Rect;
var TextureRect : Rect;
var BoxRect : Rect;
var CurrentTexture : Texture2D;
var Health$$anonymous$$ax : int = 100;
static var playerHealth : int = 100;
/// <summary>
/// Use this fopr Initialization
/// </summary>
function Start()
{
UpdateRects();
}
/// <summary>
/// Call this when you chagne resoltution
/// </summary>
function UpdateRects()
{
LabelRect = Rect((Screen.width / 1.15) * (Screen.width / 1920.0),
(Screen.height / 1.05) * (Screen.height / 1080.0),
50.0 * (Screen.width / 1920.0),
20.0 * (Screen.height / 1080.0));
TextureRect = Rect(Screen.width / 1.12 * (Screen.width / 1920.0),
Screen.height / 1.05 * (Screen.height / 1080.0),
(200.0 * ($$anonymous$$yGUISystem.playerHealth / Health$$anonymous$$ax) * (Screen.width / 1920.0)),
20.1 * (Screen.height / 1080.1));
BoxRect = Rect((Screen.width/ 1.156) * (Screen.width / 1920.0),
Screen.height/1.13 * (Screen.height / 1080.0),
255.0 * (Screen.width / 1920.0),
100.0 * (Screen.height / 1080.0));
}
function OnGUI() {
GUI.Label(LabelRect, "Health");
GUI.DrawTexture(TextureRect, CurrentTexture);
GUI.Box(BoxRect, " ");
}
you wil also need to adjust the font size of that Label, or it will clipo and cut over itself, as the font itself isnt adjsut... you can do this in the same manner as i did with the Rects