Health/Energy Bar, via OnGUI, any other way?

I have a working health/energy bar scrip working with some textures and it’s fine, I can control the placement a little better with the scrip below, but If i change resolutions the width of the bar scales accordingly, which isn’t what I want, I want to know where the start and ending points of my bar will be or hopefully make it static so I can place some text-meshes next to it etc…

So I wondered is there such a script/feature which doesn’t use OnGUI()? if it wasn’t for this scaling problem (lower the resolution and the bar becomes smaller) I’d love what I have, but anyway thanks, hopefully I can find a solution as I’ve been battling with the placement of this object for far too long, cheers

using UnityEngine;
using System.Collections;

public class EnergyBarScript : MonoBehaviour
{

		public float native_width = 1920;
		public float native_height = 1080;
		public Texture backgroundTexture;
		public Texture foregroundTexture;
		public int healthWidth = -400;
		public int healthHeight = 41;
		public int healthMarginLeft = 800;
		public int healthMarginTop = 1030;
		public int frameWidth = -378;
		public int frameHeight = 0;
		public int frameMarginLeft = 792;
		public int frameMarginTop = 700;
	
		void  OnGUI ()
		{
				float rx = Screen.width / native_width;
				float ry = Screen.height / native_height;
				GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, new Vector3 (rx, ry, 1));
		
				GUI.DrawTexture (new Rect (frameMarginLeft, frameMarginTop, frameMarginLeft + frameWidth, frameMarginTop + frameHeight), backgroundTexture, ScaleMode.ScaleToFit, true, 0);
		
				GUI.DrawTexture (new Rect (healthMarginLeft, healthMarginTop, healthWidth + healthMarginLeft, healthHeight), foregroundTexture, ScaleMode.ScaleAndCrop, true, 0);
		}
}

Got, use Pixel Inset

Pixel Inset

The purpose of the Pixel Inset is to prevent textures from scaling with screen resolution, and keeping them in a fixed pixel size. This allows you to render a texture without any scaling. This means that players who run your game in higher resolutions will see your textures in smaller areas of the screen, allowing them to have more screen real-estate for your gameplay graphics.

To use it effectively, you need to set the scale of the GUI Texture’s Transform to (0, 0, 0). Now, the Pixel Inset is in full control of the texture’s size and you can set the Pixel Inset values to be the exact pixel size of your Texture.
Hints

    The depth of each layered GUI Texture is determined by its individual Z Transform position, not the global Z position.
    GUI Textures are great for making menu screens, or pause/escape menu screens.
    You should use Pixel Inset on any GUI Textures that you want to be a specific number of pixels for the width and height.

forget that, it’s like boxing with unity… change resolution and my energy/health bar go’s to where ever it wants almost, I want my health bar over there under there and not to change scale or move… please, so is GUI.DrawTexture the only way I can control my graphic on screen? I want to use its transform and position it on screen at the size I want… nooo shame, i may have to dump the feature for my game if I can’t lock it down to a screen pos I want, I’ve searched around and it looks like I’m lumbered with it.

Why not use the GUI features in Unity 4.6?

–Eric

1 Like