Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Health bar GUI Texture croping?

Discussion in 'Scripting' started by Deleted User, Mar 24, 2013.

  1. Deleted User

    Deleted User

    Guest

    Firstly I'me a noob with scripting and C# so please dont be complicated if pos but C# is what ime using so Here is my problem...
    Ive been folowing these Hack And slash rpg tutorials
    and I've hit a problem in the making of the heath bar.
    what i want is the health bar to be cropped not resized... the aim of this is for the health bar green stuff not to be visible through the transparent background-it would be good if i could do this with "masks" ive no idea what they are but from what ive seen that seems less complicated than scripting.
    here is my green health bar bit script
    Code (csharp):
    1. /// <summary>
    2. /// VitalBarHealth.cs
    3. /// worldcrafter
    4. ///
    5. ///
    6. /// This class is reponsible for displaying the vitals bar for the player character or spawn
    7. /// </summary>
    8.  
    9. using UnityEngine;
    10. using System.Collections;
    11.  
    12. public class VitalBarHealth : MonoBehaviour {
    13.    
    14.     public bool _isPlayerHealthBar;     //This boolean value tells us if this is the player healthbar or the mob healthbar
    15.    
    16.     private int _maxBarLength;              //How long vital bar will be at 100% capacity
    17.     private int _curBarLength;              //The current length of the vital bar
    18.     private GUITexture _display;            //health bar texture
    19.    
    20.     void Awake() {
    21.         _display = gameObject.GetComponent<GUITexture>();
    22.     }
    23.    
    24.     // Use this for initialization
    25.     void Start () {
    26. //      _isPlayerHealthBar = true;
    27.        
    28.        
    29.        
    30.         _maxBarLength = (int)_display.pixelInset.width;
    31.        
    32.        
    33.         OnEnable();
    34.    
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.    
    40.     }
    41.    
    42.     //This method is called when the gameObject is enabled
    43.     public void OnEnable() {
    44.         if(_isPlayerHealthBar)
    45.             Messenger<int, int>.AddListener("player health update", OnChangedHealthBarLength);
    46.         else {
    47.             ToggleDisplay(false);
    48.             Messenger<int, int>.AddListener("mob health update", OnChangedHealthBarLength);
    49.             Messenger<bool>.AddListener("show mob vital bars", ToggleDisplay);
    50.         }
    51.     }
    52.    
    53.     //This method is called when the gameObject is disabled
    54.     public void OnDisable() {
    55.         if(_isPlayerHealthBar)
    56.             Messenger<int, int>.RemoveListener("player health update", OnChangedHealthBarLength);
    57.         else {
    58.             Messenger<int, int>.RemoveListener("mob health update", OnChangedHealthBarLength);
    59.             Messenger<bool>.RemoveListener("show mob vital bars", ToggleDisplay);
    60.         }
    61.        
    62.     }
    63.    
    64.     //this will calculate the total size of the healthbar in relation to the % of health the target has left
    65.     public void OnChangedHealthBarLength(int curHealth, int maxHealth) {
    66. //      Debug.Log("We heard an event - curHealth:" + curHealth + " maxHealth:" + maxHealth);
    67.         _curBarLength = (int)((curHealth / (float)maxHealth) *_maxBarLength);       //this calculates the current length bar length based on player's health percentage
    68. //      _display.pixelInset = new Rect(_display.pixelInset.x, _display.pixelInset.y, _curBarLength, _display.pixelInset.height);
    69.         _display.pixelInset = CalculatePosition();
    70.     }
    71.    
    72.     //setting the healthbar to the player or mob
    73.     public void SetPlayerHealth(bool b) {
    74.         _isPlayerHealthBar = b;
    75.     }
    76.    
    77.     private Rect CalculatePosition() {
    78.         float yPos = _display.pixelInset.y / 2 - 14;
    79.        
    80.         if(!_isPlayerHealthBar){
    81.             float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength / 4 + 10);
    82.             return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height);    
    83.         }
    84.         return new Rect(_display.pixelInset.x, yPos, _curBarLength, _display.pixelInset.height);   
    85.     }
    86.    
    87.     private void ToggleDisplay(bool show) {
    88.         _display.enabled = show;
    89.     }
    90. }
    and here is my green bit of the health bar background which is transparent

    Code (csharp):
    1. /// <summary>
    2. /// PlayerVitalBarBG.cs
    3. /// worldcrafter
    4. ///
    5. ///
    6. /// This class is reponsible for displaying the vitals bar for the player character or spawn
    7. /// </summary>
    8.  
    9. using UnityEngine;
    10. using System.Collections;
    11.  
    12. public class PlayerVitalBarBG : MonoBehaviour {
    13.    
    14.     public bool _isPlayerHealthBar;     //This boolean value tells us if this is the player healthbar or the mob healthbar
    15.    
    16.     private int _maxBarLength;              //How long vital bar will be at 100% capacity
    17.     private int _curBarLength;              //The current length of the vital bar
    18.     private GUITexture _display;            //health bar texture
    19.    
    20.     void Awake() {
    21.         _display = gameObject.GetComponent<GUITexture>();
    22.     }
    23.    
    24.     // Use this for initialization
    25.     void Start () {
    26. //      _isPlayerHealthBar = true;
    27.        
    28.        
    29.        
    30.         _maxBarLength = (int)_display.pixelInset.width;
    31.        
    32.        
    33.         OnEnable();
    34.    
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.    
    40.     }
    41.    
    42.     //This method is called when the gameObject is enabled
    43.     public void OnEnable() {
    44.         if(_isPlayerHealthBar)
    45.             Messenger<bool>.RemoveListener("show mob vital bars", ToggleDisplay);
    46.        
    47.     }
    48.    
    49.    
    50.     //setting the healthbar to the player or mob
    51.     public void SetPlayerHealth(bool b) {
    52.         _isPlayerHealthBar = b;
    53.     }
    54.    
    55.     private Rect CalculatePosition() {
    56.         float yPos = _display.pixelInset.y / 2 - 14;
    57.        
    58.         if(!_isPlayerHealthBar){
    59.             float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength / 4 + 10);
    60.             return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height);    
    61.         }
    62.         return new Rect(_display.pixelInset.x, yPos, _curBarLength, _display.pixelInset.height);   
    63.     }
    64.    
    65.     private void ToggleDisplay(bool show) {
    66.         _display.enabled = show;
    67.     }
    68. }
    this is something like want my healthbar to look like
    $Player Health Bar.png

    please try to help, any help is helpful :)