Unity效果實現:任務系統和人物血條
阿新 • • 發佈:2021-11-29
任務系統和血條是許多RPG遊戲中必不可少的元素之一,由於製作這兩個都要用UI系統,所以放在一起來講。
1.血條
- 血條我們可以用素材中的圖片製作,扣血時根據當前血量和最大血量的比值來扣
- 將指令碼掛到畫布上
2.任務系統
- 我們需要一個任務管理類來管理任務的新增,接受與取消
程式碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIManager : MonoBehaviour { public static UIManager Instance; private Image hpBar; private PlayerControl player; private Image dialog; private int questid; private void Awake() { //獲取單例 Instance = this; hpBar = transform.Find("Head").Find("HpBar").GetComponent<Image>(); player = GameObject.FindWithTag("Player").GetComponent<PlayerControl>(); //獲取對話方塊 dialog = transform.Find("Dialog").GetComponent<Image>(); //隱藏對話方塊 dialog.gameObject.SetActive(false); } private void Update() { hpBar.fillAmount = (float)player.hp / player.MaxHp; } public void Show(string name,string content,int id=-1) { //撥出滑鼠指標 Cursor.lockState = CursorLockMode.None; dialog.gameObject.SetActive(true); dialog.transform.Find("NameText").GetComponent<Text>().text = name; questid = id; if(QuestManager.Instance.HasQuest(id)) { dialog.transform.Find("ContentText").GetComponent<Text>().text = "你已經接受該任務了"; } else { dialog.transform.Find("ContentText").GetComponent<Text>().text = content; } } public void AcceptButtonClick() { //隱藏對話方塊 dialog.gameObject.SetActive(false); //接受任務 QuestManager.Instance.AddQuest(questid); Cursor.lockState = CursorLockMode.Locked; } public void CancelButtonClick() { dialog.gameObject.SetActive(false); Cursor.lockState = CursorLockMode.Locked; } }