Unity中血条ui制作(二)
代码 :
using UnityEngine;
public class HealthBar : MonoBehaviour
{
public float healthValue = 100f; //血量上限
/// <summary>
/// 当前血量属性,外部访问器
/// </summary>
public float Health
{
get
{
return health;
}
set
{
if (health != value)
{
Refresh();
health = value;
}
}
}
private float health;
private void Start()
{
health = healthValue;
transform.localScale.Normalize();
}
void Refresh()
{
transform.localScale = new Vector3(Health / healthValue, 1, 1);
}
}
思路 :
根据血量控制血条Bar物体的transform.localScale的x轴值.
脚本要挂在Bar物体上.
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
HealthBar healthBar;
private void Start()
{
healthBar = GetComponentInChildren<HealthBar>();
}
void Update()
{
healthBar.Health -= Time.deltaTime;
}
}
血条挂在玩家物体上,可以通过改变Health值影响血条.
来源:CSDN
作者:#Impulse
链接:https://blog.csdn.net/weixin_43455008/article/details/104167591