Search Unity

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

Discussion in 'Immediate Mode GUI (IMGUI)' started by San_Holo, Nov 19, 2014.

  1. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    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

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnergyBarScript : MonoBehaviour
    5. {
    6.  
    7.         public float native_width = 1920;
    8.         public float native_height = 1080;
    9.         public Texture backgroundTexture;
    10.         public Texture foregroundTexture;
    11.         public int healthWidth = -400;
    12.         public int healthHeight = 41;
    13.         public int healthMarginLeft = 800;
    14.         public int healthMarginTop = 1030;
    15.         public int frameWidth = -378;
    16.         public int frameHeight = 0;
    17.         public int frameMarginLeft = 792;
    18.         public int frameMarginTop = 700;
    19.    
    20.         void  OnGUI ()
    21.         {
    22.                 float rx = Screen.width / native_width;
    23.                 float ry = Screen.height / native_height;
    24.                 GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, new Vector3 (rx, ry, 1));
    25.        
    26.                 GUI.DrawTexture (new Rect (frameMarginLeft, frameMarginTop, frameMarginLeft + frameWidth, frameMarginTop + frameHeight), backgroundTexture, ScaleMode.ScaleToFit, true, 0);
    27.        
    28.                 GUI.DrawTexture (new Rect (healthMarginLeft, healthMarginTop, healthWidth + healthMarginLeft, healthHeight), foregroundTexture, ScaleMode.ScaleAndCrop, true, 0);
    29.         }
    30. }
     
  2. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    Got, use Pixel Inset

    Code (csharp):
    1. Pixel Inset
    2.  
    3. 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.
    4.  
    5. 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.
    6. Hints
    7.  
    8.     The depth of each layered GUI Texture is determined by its individual Z Transform position, not the global Z position.
    9.     GUI Textures are great for making menu screens, or pause/escape menu screens.
    10.     You should use Pixel Inset on any GUI Textures that you want to be a specific number of pixels for the width and height.
    11.  
     
  3. San_Holo

    San_Holo

    Joined:
    Sep 26, 2014
    Posts:
    152
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Why not use the GUI features in Unity 4.6?

    --Eric
     
    San_Holo likes this.