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
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, " ");
}
}