43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
![]() |
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using Sirenix.OdinInspector;
|
|||
|
using UnityEngine.UI;
|
|||
|
using DG.Tweening;
|
|||
|
|
|||
|
public class PlayerHpBar : MonoBehaviour
|
|||
|
{
|
|||
|
[SerializeField]
|
|||
|
private float HPLeft = -1f;
|
|||
|
private MyPlayer player;
|
|||
|
[SerializeField][ListDrawerSettings]
|
|||
|
private List<Image> hearts;
|
|||
|
public Sprite full;
|
|||
|
public Sprite empty;
|
|||
|
|
|||
|
void Start(){
|
|||
|
hearts = new List<Image>();
|
|||
|
//找到必要的物体和组件
|
|||
|
player = FindObjectOfType<MyPlayer>();
|
|||
|
for(int i = 0; i < 10; i++){
|
|||
|
hearts.Add(transform.GetChild(i).GetComponent<Image>());
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void Update(){
|
|||
|
//每帧检查,当UI剩余血量和玩家真实剩余血量不一致,触发重刷新UI功能
|
|||
|
if(HPLeft != player.HPLeft) RefrashUI((int) player.HPLeft);
|
|||
|
HPLeft = player.HPLeft;
|
|||
|
}
|
|||
|
|
|||
|
void RefrashUI(int left){
|
|||
|
Debug.Log("触发");
|
|||
|
foreach(Image t in hearts){
|
|||
|
if(left > 0) t.sprite = full;
|
|||
|
else t.sprite = empty;
|
|||
|
left--;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|