1.准备爱心血条
准备好红色爱心和灰色爱心的图片
2.HeartUI 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Hearts : MonoBehaviour {
[Header("爱心UI图的列表")]public List<Image> redHearts = new();
void Start ()
{
}
public void UpdateHeartUI(int HP)
{
//先将所有红色爱心全部隐藏
foreach (Image redHeart in redHearts)
{
redHeart.enabled = false;
}
//再根据HP数量先将爱心依次显示
for (int i=0; i< HP; i++)
{
redHearts[i].enabled = true;
}
}
}
3.在Inspector窗口中绑定好对象