Unity 怪名稱、血條和飄血跟隨
阿新 • • 發佈:2019-02-20
public abstract class RoleEvent : MonoBehaviour
{
public GameObject prefabUIHead;
public GameObject prefabUILife;
public Transform headbarPos;//放在怪下面的空物件
public Transform lifebarPos;//放在怪下面的血條物件
private Transform uiHead;
private Transform uiLife;
private HUDText hudText;//飄血效果
public string name;
public virtual void Start()
{
uiHead = ((GameObject)Instantiate(prefabUIHead)).transform;
uiHead.SetParent(GameObject.Find("HeadbarRoot").transform);
uiHead.localScale = new Vector3(1, 1, 1);
uiHead.FindChild("nickName").GetComponent<UILabel>().text = name;
hudText = uiHead.FindChild("HUD").GetComponent<HUDText>();
if (prefabUILife == null || lifebarPos == null)
{
uiHead.FindChild("Lifebar").gameObject.SetActive(false);
}
}
public virtual void Update()
{
//世界座標點轉換為視口座標點
Vector3 pos = Camera.main.WorldToViewportPoint(headbarPos.position);
//視口座標點轉換為世界座標點
Vector3 uiPos = UICamera.mainCamera.ViewportToWorldPoint(pos);
uiHead.position = uiPos;
}
public void Hurt(int value)
{
hudText.Add(string.Format("-{0}", value), Color.red, 0.1f);
}
{
public GameObject prefabUIHead;
public GameObject prefabUILife;
public Transform headbarPos;//放在怪下面的空物件
public Transform lifebarPos;//放在怪下面的血條物件
private Transform uiHead;
private Transform uiLife;
private HUDText hudText;//飄血效果
public string name;
public virtual void Start()
{
uiHead = ((GameObject)Instantiate(prefabUIHead)).transform;
uiHead.SetParent(GameObject.Find("HeadbarRoot").transform);
uiHead.localScale = new Vector3(1, 1, 1);
uiHead.FindChild("nickName").GetComponent<UILabel>().text = name;
hudText = uiHead.FindChild("HUD").GetComponent<HUDText>();
if (prefabUILife == null || lifebarPos == null)
{
uiHead.FindChild("Lifebar").gameObject.SetActive(false);
}
}
public virtual void Update()
{
//世界座標點轉換為視口座標點
Vector3 pos = Camera.main.WorldToViewportPoint(headbarPos.position);
//視口座標點轉換為世界座標點
Vector3 uiPos = UICamera.mainCamera.ViewportToWorldPoint(pos);
uiHead.position = uiPos;
}
public void Hurt(int value)
{
hudText.Add(string.Format("-{0}", value), Color.red, 0.1f);
}
}
public class RoleMonsterAI : RoleEvent{
public override void Start()
{
name = "小怪";
base.Start();
}
public override void Update()
{
base.Update();
if (Input.GetMouseButtonDown(2))
{
Hurt(20);
}
}
}