using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HpMove : MonoBehaviour {
private RectTransform rec;
private float hp=221;//血量最大值
private float mp=204;//法力最大值
public float speed=5f;//加血与减血速度
public bool isHp=false;
float hor;//横轴
float ver;//纵轴
public Text hpText;
public Text mpText;
void Start(){
rec = GetComponent<RectTransform> ();
}
void Update(){
hor = Input.GetAxis ("Horizontal");//定义横轴
ver = Input.GetAxis ("Vertical");//定义纵轴
if (isHp) {
float currenthp = rec.sizeDelta.x + hor * Time.deltaTime * speed;
currenthp = Mathf.Clamp (currenthp, 0, hp);
rec.sizeDelta = new Vector2 (currenthp, rec.sizeDelta.y);
hpText.text = Mathf.Round((currenthp / hp) * 100) + "/" + 100;
} else {
float currentmp=rec.sizeDelta.x + ver * Time.deltaTime * speed;
currentmp = Mathf.Clamp (currentmp, 0, mp);
rec.sizeDelta = new Vector2 (currentmp, rec.sizeDelta.y);
mpText.text = Mathf.Round((currentmp / mp) * 100 )+ "/" + 100;
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
//判断当前对象是血条还是蓝条;
public bool isHp=false;
//虚拟轴:横轴,纵轴;
private float hor,ver;
//血条宽度最大值。
private float hpMaxWd = 300;
//蓝条宽度最大值。
private float mpMaxWd = 210;
//当前的虚拟轴
private float currentAxis;
//当前的最大宽度
private float currentMaxWd;
//rencTransformz组件
private RectTransform rect;
public float speed=100;
//血条,蓝条文本。
public Text hpText;
public Text mpText;
//血条,蓝条最大值‘
public float hpMAxValue=1000f;
public float mpMAxValue=500f;
//当前条的最大值。
private float currentMaxValue;
//当前修改的文本显示框。
private Text currentText;
void Start(){
rect = GetComponent<RectTransform> ();
}
void Update(){
hor=Input.GetAxis("Horizontal");
ver=Input.GetAxis("Vertical");
if (isHp) {
currentAxis = hor;
currentMaxWd = hpMaxWd;
currentText = hpText;
currentMaxValue = hpMAxValue;
} else {
currentAxis = ver;
currentMaxWd = mpMaxWd;
currentText = mpText;
currentMaxValue = mpMAxValue;
}
//根据用户的操作,计算当前条的宽度。
float newWd = rect.sizeDelta.x +
currentAxis * Time.deltaTime * speed;
float realWd = Mathf.Clamp (newWd, 0, currentMaxWd);
//赋值给transfo组件中。
rect.sizeDelta=new Vector2 (realWd,rect.sizeDelta.y);
//将数值信息显示到Text。
float currentValue = (realWd / currentMaxWd) * currentMaxValue;
currentValue = Mathf.Round (currentValue);
currentText.text = currentValue.ToString () + "/" +
currentMaxValue.ToString ();
}
}