using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class PlayerInfo : MonoBehaviour { public static PlayerInfo instance; [Header("显示gold的文本")] public Text goldText; [Header("显示hp的文本")] public Text hpText; [Header("最大PlayerHp,默认为5")] public float maxPlayerHp=5; private float playerHp=0; [Header("PlayerGold,默认为0")] public float playerGold; public event Action OnGoldReachedThreshold; // Start is called before the first frame private void Awake() { instance = this; } void Start() { ChangeHp(maxPlayerHp); ChangeGold(0); } // Update is called once per frame void Update() { } public void ChangeHp(float number) { if ((this.playerHp +number)<=0) { Penal.instance.GameOver(); } this.playerHp += number; UpdateShow(); } public void ChangeGold(float number) { this.playerGold += number; if (this.playerGold>=10) { OnGoldReachedThreshold?.Invoke(); } UpdateShow(); } void UpdateShow() { goldText.text = "银币:" + playerGold.ToString(); hpText.text = "玩家Hp:" + playerHp.ToString() + "/" + maxPlayerHp.ToString(); } }