Unity剧情对话XML实现

Unity剧情对话XML实现

小生正在做一款剧情冒险游戏,剧情游戏最少不了的便是对话系统。
那么今天我们就来说一下具体实现。
1.创建对话Canvas
我的排版是三个图片,依次为npc头像,对话的背景,主角头像。
在这里插入图片描述
2.创建人物
我们先创建主角,挂载脚本,不方便贴出所有代码,讲下思路。
有一个Player脚本,其中设置一个函数包括所有点击行走鼠标射线检测到的第一个layer,分情况讨论,如果是路,主角寻路,如果是npc,则走到npc面前进行对话。
在npc上挂载脚本,用于读取xml文件内容并显示。

3.xml文件编写
桌面新建一个TXT文档,后缀改为.xml文件。

<?xml version="1.0" encoding="utf-8"?>
<dialogues>
  <dialogue>
    <role>A</role>
    <detail>你来了,我的勇士!</detail>
  </dialogue>
  <dialogue>
    <role>B</role>
    <detail>请问您是。。。</detail>
  </dialogue>
  <dialogue>
    <role>A</role>
    <detail>我是谁你不需要知道,我是来帮你的。</detail>
  </dialogue>
  <dialogue>
    <role>A</role>
    <detail>你需要收集三块符石碎片,才能打开通往后面的道路。</detail>
  </dialogue>
  <dialogue>
    <role>B</role>
    <detail>这样啊,谢谢您。</detail>
  </dialogue>
</dialogues>

当然,这些标签你可以自己定义,把脚本中相应的读取操作修改即可。

4.编写对话脚本
编写挂载到npc上的脚本

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
using UnityEngine.UI;

public class ShowWords : MonoBehaviour {

    private List<string> chatList;
    private int chatCount,index=0;
    private GameObject npc, me, text,player;
    public string xmlName=null;
    private bool hasXml =false;

    // Use this for initialization
    void Start () {
        //获取三个物体
        npc = gameObject.transform.GetChild(0).gameObject;
        text = gameObject.transform.GetChild(1).gameObject;
        me = gameObject.transform.GetChild(2).gameObject;
        player = GameObject.FindGameObjectWithTag("Player");//获取到主角
        //xml编辑器
        xmlName = "none";
        //储存句子
        chatList = new List<string>();
        //string data = Resources.Load("Txt/shop").ToString();
        //xmlDocument.Load(data);
        //这样读取不到文件,不知道为什么。。。只能用Appication.datapath
	}
	
	// Update is called once per frame
	void Update () {
        if (!hasXml && xmlName!="none")
        {
            //print(hasXml+" " + xmlName);
            
            //获取到npc名字,将相应的脚本取为相同名字,这样取到对话内容比较方便。
            
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(Application.dataPath + "/Resources/Txt/" + xmlName + ".xml");
           // print(Application.dataPath + "/Resources/Txt/" + xmlName + ".xml");
            //找到dialogues的所有子节点
            XmlNodeList xmlNodeList = xmlDocument.SelectSingleNode("dialogues").ChildNodes;
            
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                XmlElement xmlElement = (XmlElement)xmlNode;
                chatList.Add(xmlElement.ChildNodes.Item(0).InnerText + "\n" + xmlElement.ChildNodes.Item(1).InnerText);
            }
            chatCount = chatList.Count;
            chat_handle(0);//显示第一句话
            hasXml = true;
        }
        if (Input.GetMouseButtonDown(0))//如果点击了鼠标左键
        {
            index++;//对话跳到一下个
            if (index < chatCount)//如果对话还没有完
            {
                chat_handle(index);//那就载入下一条对话
            }
            else
            {
                //对话完了              
                player.GetComponent<ChatWith>().isChat = false;
                gameObject.SetActive(false);
                
            }
        }

       
	}
    
    private void chat_handle(int index)
    {
        //切割数组
        string[] role_detail_array = chatList[index].Split('\n');
        //list中每一个对话格式就是“角色名\n对话”
        string role = role_detail_array[0];
        string role_detail = role_detail_array[1];
        if (role == "A")
        {
            npc.SetActive(true);
            me.SetActive(false);
            //显示图片
        }
        else
        {
            me.SetActive(true);
            npc.SetActive(false);
        }
        text.transform.GetChild(0).GetComponent<Text>().text = role_detail;
      加载对话内容
    }


}

大致就是这样,若有不对的或者不明白的可以私信我。

猜你喜欢

转载自blog.csdn.net/weixin_42927264/article/details/89098225