Search Unity

Can't get health bar vertical in OnGui in C#

Discussion in 'Scripting' started by clearrose, May 22, 2015.

  1. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Can't get health bar vertical in OnGui in C#.... we tried google and endless amount of time researching how to get our current health bar vertical instead of horizontal.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AirBar : MonoBehaviour {
    5.  
    6.     public float air = 10;
    7.     public float maxAir = 10;
    8.     public float airBurnRate = 1f;
    9.     public Texture2D bgTexture;
    10.     public Texture2D airBarTexture;
    11.     public int iconWidth =32;
    12.     public Vector2 airOffset = new Vector2(10, 10);
    13.  
    14.     private GameObject Player;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.         Player = GameObject.FindWithTag("Player");
    20.    
    21.     }
    22.  
    23.     void OnGUI(){
    24.  
    25.         var percent = Mathf.Clamp01 (air / maxAir);
    26.  
    27.         if (!Player)
    28.             percent = 0;
    29.  
    30.         DrawMeter (airOffset.x, airOffset.y, airBarTexture, bgTexture, percent);
    31.  
    32.     }
    33.  
    34.     void DrawMeter(float x, float y, Texture2D texture, Texture2D background, float percent){
    35.  
    36.         var bgW = background.width;
    37.         var bgH = background.height;
    38.  
    39.         GUI.DrawTexture (new Rect (x, y, bgW, bgH), background);
    40.  
    41.         var nW = ((bgW - iconWidth) * percent) + iconWidth;
    42.  
    43.         GUI.BeginGroup (new Rect (x, y, nW, bgH));
    44.         GUI.DrawTexture (new Rect (0, 0, bgW, bgH), texture);
    45.         GUI.EndGroup ();
    46.  
    47.  
    48.     }
    49.  
    50.     // Update is called once per frame
    51.     void Update () {
    52.  
    53.         if (!Player)
    54.             return;
    55.  
    56.         if (air > 0) {
    57.             air -= Time.deltaTime * airBurnRate;
    58.  
    59.         } else {
    60.             LevelManager script = Player.GetComponent<LevelManager> ();
    61.             script.StopLevel();
    62.         }
    63.     }
    64. }
    65.  
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Line 43 can just be
    Code (CSharp):
    1.   GUI.BeginGroup (new Rect (x, y, bgW, bgH*percent));
    for top to bottom
    Its a little more complicated for bottom to top (Typed here and now, not tested, and mostly guessworked)
    Code (CSharp):
    1. float dY = bgH * (1-percent);
    2. GUI.BeginGroup(new Rect(x, y + dY, bgW, bgH * percent));
    3. GUI.DrawTexture(new Rect(0, -bgH * percent, bgW, bgH), texture);
    4. GUI.EndGroup();
     
  3. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thanks a lot for your answer, however the bar just disappeared when I tested the code.
     
  4. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Ah, see the mistake is that you should be using Unity 4.6+ UI.

    Problem solved.