So I’ve got a script which works with the actual health script for the character, which decreases and increases the box’s size from 0 - 100
using UnityEngine;
using System.Collections;
public class LifeBar : MonoBehaviour
{
public GameObject player;
public int maxHealth = 100;
public float curHealth = 100;
public float healthBarLength;
void Start()
{
healthBarLength = Screen.width/2;
}
void Update()
{
AddjustCurrentHealth(0);
}
void OnGUI()
{
PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();
if (playerHealth != null)
{
float health = playerHealth.playerHealth;
// Debug.Log(health);
curHealth = (int) health;
}
UnityEngine.GUI.Box(new Rect(10,10, healthBarLength ,20), curHealth + "/" + maxHealth );
}
public void AddjustCurrentHealth(int adj)
{
curHealth += adj;
healthBarLength = (Screen.width/2)*(curHealth/(float) maxHealth);
}
}
But I’ve been having immense trouble with hooking it up to the following script so that instead of a box, it’s a texture that doesn’t stretch.
using UnityEngine;
using System.Collections;
public class GuiLoadTest : MonoBehaviour
{
public float barDisplay; //current progress
public Vector2 pos = new Vector2(20, 40);
public Vector2 size = new Vector2(40, 20);
public Texture2D emptyTex;
public Texture2D fullTex;
void OnGUI()
{
//draw the background:
UnityEngine.GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
UnityEngine.GUI.DrawTexture(new Rect(0, 0, size.x, size.y), emptyTex);
//draw the filled-in part:
UnityEngine.GUI.BeginGroup(new Rect(0, 0, size.x * barDisplay, size.y));
UnityEngine.GUI.DrawTexture(new Rect(0, 0, size.x, size.y), fullTex);
UnityEngine.GUI.EndGroup();
UnityEngine.GUI.EndGroup();
}
void Update()
{
barDisplay = Time.time * 0.02f;
//Stuff is meant to go here I think.
}
}