45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
|
|
using TMPro;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class HUD_HP : MonoBehaviour
|
||
|
|
{
|
||
|
|
public TextMeshProUGUI t_hp;
|
||
|
|
|
||
|
|
protected Camera m_Camera;
|
||
|
|
protected Transform m_Actor;
|
||
|
|
RectTransform m_RectTransform;
|
||
|
|
RectTransform CanvasRect;
|
||
|
|
public Vector2 offset = new Vector2(-5f, 0f);
|
||
|
|
|
||
|
|
void Init()
|
||
|
|
{
|
||
|
|
if (m_Camera == null) m_Camera = Camera.main;
|
||
|
|
if (CanvasRect == null) CanvasRect = GameUI.Ins.GetComponent<RectTransform>();
|
||
|
|
m_RectTransform = GetComponent<RectTransform>();
|
||
|
|
transform.localScale = Vector3.one * 0.5f;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Set(Transform tf, int hp)
|
||
|
|
{
|
||
|
|
Set(hp);
|
||
|
|
Init();
|
||
|
|
m_Actor = tf;
|
||
|
|
t_hp.text = hp.ToString();
|
||
|
|
}
|
||
|
|
public void Set(int hp)
|
||
|
|
{
|
||
|
|
t_hp.text = hp.ToString();
|
||
|
|
gameObject.SetActive(hp > 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update()
|
||
|
|
{
|
||
|
|
if (m_Camera == null) m_Camera = Camera.main;
|
||
|
|
if (m_Actor)
|
||
|
|
{
|
||
|
|
Vector2 ViewportPosition = m_Camera.WorldToViewportPoint(m_Actor.position);
|
||
|
|
Vector2 WorldObject_ScreenPosition = new Vector2((ViewportPosition.x * CanvasRect.sizeDelta.x) - (CanvasRect.sizeDelta.x * 0.5f), (ViewportPosition.y * CanvasRect.sizeDelta.y) - (CanvasRect.sizeDelta.y * 0.5f) - 15f);
|
||
|
|
m_RectTransform.anchoredPosition = WorldObject_ScreenPosition + offset;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|