what i want is the health bar i’me making to be cropped not resized.
apparent the this code is what i need to put in to crop the GUI texture but i don’t know my how to use this in my health bar script.
GUI.BeginGroup(Rect(location of healthbar,variable to decrease on damage, height of healthbar));
GUI.Label(Rect(0,0,length,height),texture);
GUI.EndGroup()
;
this is my current healthbar script which re-sizes the GUI texture
/// <summary>
/// VitalBarHealth.cs
/// worldcrafter
///
///
/// This class is reponsible for displaying the vitals bar for the player character or spawn
/// </summary>
using UnityEngine;
using System.Collections;
public class VitalBarHealth : MonoBehaviour {
public bool _isPlayerHealthBar; //This boolean value tells us if this is the player healthbar or the mob healthbar
private int _maxBarLength; //How long vital bar will be at 100% capacity
private int _curBarLength; //The current length of the vital bar
private GUITexture _display; //health bar texture
void Awake() {
_display = gameObject.GetComponent<GUITexture>();
}
// Use this for initialization
void Start () {
// _isPlayerHealthBar = true;
_maxBarLength = (int)_display.pixelInset.width;
OnEnable();
}
// Update is called once per frame
void Update () {
}
//This method is called when the gameObject is enabled
public void OnEnable() {
if(_isPlayerHealthBar)
Messenger<int, int>.AddListener("player health update", OnChangedHealthBarLength);
else {
ToggleDisplay(false);
Messenger<int, int>.AddListener("mob health update", OnChangedHealthBarLength);
Messenger<bool>.AddListener("show mob vital bars", ToggleDisplay);
}
}
//This method is called when the gameObject is disabled
public void OnDisable() {
if(_isPlayerHealthBar)
Messenger<int, int>.RemoveListener("player health update", OnChangedHealthBarLength);
else {
Messenger<int, int>.RemoveListener("mob health update", OnChangedHealthBarLength);
Messenger<bool>.RemoveListener("show mob vital bars", ToggleDisplay);
}
}
//this will calculate the total size of the healthbar in relation to the % of health the target has left
public void OnChangedHealthBarLength(int curHealth, int maxHealth) {
// Debug.Log("We heard an event - curHealth:" + curHealth + " maxHealth:" + maxHealth);
_curBarLength = (int)((curHealth / (float)maxHealth) *_maxBarLength); //this calculates the current length bar length based on player's health percentage
// _display.pixelInset = new Rect(_display.pixelInset.x, _display.pixelInset.y, _curBarLength, _display.pixelInset.height);
_display.pixelInset = CalculatePosition();
}
//setting the healthbar to the player or mob
public void SetPlayerHealth(bool b) {
_isPlayerHealthBar = b;
}
private Rect CalculatePosition() {
float yPos = _display.pixelInset.y / 2 - 14;
if(!_isPlayerHealthBar){
float xPos = (_maxBarLength - _curBarLength) - (_maxBarLength / 4 + 10);
return new Rect(xPos, yPos, _curBarLength, _display.pixelInset.height);
}
return new Rect(_display.pixelInset.x, yPos, _curBarLength, _display.pixelInset.height);
}
private void ToggleDisplay(bool show) {
_display.enabled = show;
}
}
it would be perfectly acceptable if i was a coder… but i am asking HOW to use
GUI.BeginGroup(Rect(location of healthbar,variable to decrease on damage, height of healthbar));
GUI.Label(Rect(0,0,length,height),texture);
GUI.EndGroup()
in my script, not what to use - i dont know HOW. i am not a scripter so i dont know HOW to use that code in my code to crop the GUI texture.
no one is answering that question so so i posted another thread thinking the other was to long for people to bother reading.
if you or anyone could do this for me then i would be extremely grateful.
you are right but no one will answer me so have little choice.
thanks-thats a good point… BUT for the moment i don’t really want to spend hours learning scripting… pleeeas would someone spoonfeed me, just this once
where do i put this in my script?-i get that i have to fill in the values but i don’t know how to implement all that code into my code so that it crops it.
What do you mean where?
Like i said, what i posted is self contained, works by itself, Just put it in anywhere
Since you haven’t posted anything using OnGUI() it won’t interfere with anything
jus do it for me please-i dont know what variables are what realy, ime not actually making a game, i just want it to help with a demo of some animations so could you please work out whats what for me
Man give it a rest and give up! Hpjohn has written ALL the code for you, even put it in the gui function so it gets called. You just have to cut and paste it and set up the couple of variables he put in the code. Seriously if you cant or dont know how to assign a variable a value you have no hope at all.
ok well in that case could you give me an example of you whole script hpjhon ?, cos there is no hope for me with scripting.
i could be getting errors because theres some stuff i need to remove from my script, and yes i figured out that it needs a ; on fourth line, i may be a noob with this stuff but i do have a little basic knowledge.
The ‘whole’ script:
Note that apart from substituting my own numbers, and comments, it is exactly the same as posted 3/4 times already.
You should already have the filler at the top… right?
//-> class someScript : MonoBehaviour { <- header filler
Texture2D someTexture; //It's a 256x32 bitmap
int someHealthVariable;
void OnGUI () {
GUI.BeginGroup(new Rect(5, 5, someHealthVariable, 32)); //variable controls the horizontal size of the group
GUI.DrawTexture(new Rect(0, 0, 256, 32), someTexture); //but still a 256x32 drawn inside it
GUI.EndGroup();
}
}
And dont forget, if console shows errors, read them to find out where you went wrong