人物血条的变化

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 ();

 

}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值