先来张效果图:
接下来看看是如何用Unity3D实现的吧!
using UnityEngine;
using System.Collections;
public class TestBoold : MonoBehaviour {
[SerializeField] private Transform _parentCanvas;
[SerializeField] private Transform _targetTransform;
[SerializeField] private GameObject _bloodPrefab;
[SerializeField] private UIBloodFollow _uiBloodFollow;
[SerializeField] private int _roleHp = 100;
/// <summary>
/// 加血的方法
/// </summary>
public void onAddBlood(){
//如果血量>100,满血 不执行加血
if (_roleHp >= 100) {
return;
}
var changeHp = Random.Range (10, 30);//每次加血量 在0-20之间
hpCalculate (true,changeHp);
getPrefabTransform ().onLossBloodAnimation (changeHp);
}
public void onReductionBlood(){
//如果血量<0,敌人死亡,便不再掉血
if (_roleHp <= 0) {
return;
}
var changeHp = Random.Range(0, -20);//每次掉血量 在0-20之间
hpCalculate (false,changeHp);
getPrefabTransform ().onLossBloodAnimation (changeHp);
}
//计算当前血量
void hpCalculate (bool isAddBlood, int changeHp = 0) {
_roleHp = isAddBlood ? _roleHp += changeHp : _roleHp += changeHp;
_uiBloodFollow.setHp (_roleHp);
}
LossBloodAnimation getPrefabTransform(){
GameObject go = Instantiate<GameObject> (_bloodPrefab);
go.transform.SetParent (_parentCanvas);
go.transform.position = Camera.main.WorldToScreenPoint (_targetTransform.position);
var lossBloodAnimation = go.GetComponent<LossBloodAnimation> ();
go.SetActive (true);
return lossBloodAnimation;
}
}
陈老师在代码中标注了详细的注释,相信大家都能看明白。
文章原创,未经许可禁止转载。如需转载请联系我。