Unity效果实现:任务系统和人物血条

任务系统和血条是许多RPG游戏中必不可少的元素之一,由于制作这两个都要用UI系统,所以放在一起来讲。

1.血条

  • 血条我们可以用素材中的图片制作,扣血时根据当前血量和最大血量的比值来排
  • 将脚本挂到画布上

2.任务系统

  • 我们需要一个任务管理类来管理任务的添加,接受与取消
  • 任务内容这里采用XML语句来编写,代码如下
    using System.Collections.Generic;
    using System.Xml;
    using UnityEngine;
    public class QuestData
    {
        public int id;
        public string name;
        public int enemyid;
        public int count;
        public int currentCount;
        public int money;
    }
    
    public class QuestionDtaManager : MonoBehaviour
    {
        private static QuestionDtaManager instance;
        //对外
        public static QuestionDtaManager Instance
        {
            get
            {
                if(instance==null)
                {
                    instance = new QuestionDtaManager();
                }
                return instance;
            }
        }
        //任务集合
        public Dictionary<int, QuestData> QuestDic = new Dictionary<int, QuestData>();
    
        private QuestionDtaManager()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.dataPath+"/XML/quest.xml");
            //根元素
            XmlElement rootEle = doc.LastChild as XmlElement;
            foreach(XmlElement questELE in rootEle)
            {
                QuestData qd = new QuestData();
                //设置任务id
                qd.id = int.Parse(questELE.GetElementsByTagName("id")[0].InnerText);
                //设置任务名称
                qd.name = questELE.GetElementsByTagName("name")[0].InnerText;
                //设置敌人id
                qd.enemyid = int.Parse(questELE.GetElementsByTagName("enemyid")[0].InnerText);
                //需要攻击的敌人个数
                qd.count = int.Parse(questELE.GetElementsByTagName("count")[0].InnerText);
                //添加到任务id
                QuestDic.Add(qd.id, qd);
            }
        }
    }
    
  • 任务管理类的脚本如下
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class QuestManager
    {
        private static QuestManager instance;
        public static QuestManager Instance
        {
            get
            {
                if (instance == null)
                    instance = new QuestManager();
                return instance;
            }
        }
        //任务列表
        private List<QuestData> QuestsList = new List<QuestData>();
    
        //是否接受任务
        public bool HasQuest(int id)
        {
            foreach(QuestData qd in QuestsList)
            {
                if(qd.id==id)
                {
                    return true;
                }
            }
            return false;
        }
    
        //添加任务
        public void AddQuest(int id)
        {
            if(!HasQuest(id))
            {
                QuestsList.Add(QuestionDtaManager.Instance.QuestDic[id]);
            }
        }
    
        public void AddEnemy(int enemyid)
        {
            for(int i=0;i<QuestsList.Count;i++)
            {
                QuestData qd = QuestsList[i];
                //遍历任务中是否有该击杀敌人任务完成数量
                qd.currentCount++;
                if(qd.currentCount>=qd.count)
                {
                    Debug.Log("任务完成,这里可以制作任务奖励,光效等内容");
                    //删除任务
                    qd.currentCount = 0;
                    QuestsList.Remove(qd);
                    //这里让主角显示一个光效
                    GameObject effect = Resources.Load<GameObject>("fx_hr_arthur_pskill_03_2");
                    //获取主角
                    Transform player = GameObject.FindWithTag("Player").transform;
                    //创建光效
                    GameObject.Instantiate(effect, player.position, player.rotation);
                }
            }
        }
    }

3.UI部分

代码如下

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;
    }
}
posted @ 2021-11-29 18:30  不进育碧不改名  阅读(511)  评论(0编辑  收藏  举报