Pun-01/Assets/Scripts/PlayerUI.cs
2022-07-08 09:14:55 +08:00

53 lines
1.4 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace cc.ivanli.pun01
{
public class PlayerUI : MonoBehaviour
{
[Tooltip("UI Text to display Player's Name")]
[SerializeField]
private TMP_Text playerNameText;
[Tooltip("UI Slider to display Player's Health")]
[SerializeField]
private Slider playerHealthSlider;
private PlayerManager target;
public void SetTarget(PlayerManager _target)
{
if (_target == null)
{
Debug.LogError("<Color=Red><a>Missing</a></Color> PlayMakerManager target for PlayerUI.SetTarget.", this);
return;
}
// Cache references for efficiency
target = _target;
if (playerNameText != null)
{
playerNameText.text = target.photonView.Owner.NickName;
}
}
void Awake()
{
this.transform.SetParent(GameObject.Find("Canvas").GetComponent<Transform>(), false);
}
void Update()
{
if (target == null)
{
Destroy(gameObject);
return;
}
// Reflect the Player Health
if (playerHealthSlider != null)
{
playerHealthSlider.value = target.Health;
}
}
}
}