Unity实现RPG角色对话框

来源:互联网 发布:淘宝网电影票 编辑:程序博客网 时间:2024/03/29 21:32

因为打算制作一款RPG类的游戏demo,所以在制作的时候,会考虑到加入角色对话与剧情系统,本来是打算使用插件来做,但是又不想了。
主要的思路相信很多人在各类博客上也都有看到过,从xml文件中解析文本,然后添加到对话框中,并且显示出来,接下来我们就来实现

第一步 UI的实现
这里我做的比较简单,仅仅使用了一个panel与text,来制作了一个简单的对话框效果,然后将其重命名后制作成预置
第二步 UI的管理代码
有关UI的管理,我们要做的就是实现UI的显示,文本内容的控制,UI的消失,以及对对话框是否存在的判断,这里我命名为Dialogmanger.cs

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Dialogmanger : MonoBehaviour {    private static Dialogmanger instance;    public GameObject DialogBox;                //用于保存对话框的预置体    public GameObject Dialog;                   //用于获取场景组件中的对话框    public Transform DialogText;                //文本    /// <summary>    /// 单例    /// </summary>    /// <value>The instance.</value>    public static Dialogmanger Instance{                    get {            if(instance == null)            {                instance = new Dialogmanger();            }            return instance;        }    }    /// <summary>    /// Shows the dialog.    /// </summary>    public void showDialog( ){        DialogBox = Resources.Load<GameObject> ("prefebs/Dialogbox");        Instantiate (DialogBox);    }    /// <summary>    /// Sets the dialog text.    /// </summary>    /// <param name="sentence">Sentence.</param>    public void setDialogText(string sentence){        if (GameObject.Find ("Dialogbox(Clone)")) {            Dialog = GameObject.Find ("Dialogbox(Clone)");            DialogText = Dialog.transform.Find ("dialogText");            Text dialogtext = DialogText.GetComponent<Text> ();            dialogtext.text = sentence;        }    }    /// <summary>    /// Destories the dialog.    /// </summary>    public void DestoryDiaLog(){        if (GameObject.Find ("Dialogbox(Clone)")) {            Dialog = GameObject.Find ("Dialogbox(Clone)");            Destroy (Dialog);        }    }    /// <summary>    /// Determines whether this instance is empty dialog.    /// </summary>    /// <returns><c>true</c> if this instance is empty dialog; otherwise, <c>false</c>.</returns>    public bool IsEmptyDialog(){        if (GameObject.Find ("Dialogbox(Clone)")) {            return false;        }else{            return true;        }    }}

地址:git@code.csdn.net:snippets/2593111.git

第三步 对XML文件的解析

using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Xml;public class XmlReader : MonoBehaviour {    private static XmlDocument XMLDout;    private static XmlReader instance;    private int Index;    private string TAG;    public static XmlReader Instance{        get {            if(instance == null)            {                instance = new XmlReader();            }            return instance;        }    }    public void readXML(string path){        XMLDout = new XmlDocument ();        string url=Application.dataPath+"/"+path;        XMLDout.Load (url);    }    public int getCout(string tag,int index){        Index = index;        TAG = tag;        int inCout = XMLDout.GetElementsByTagName (tag) [index].ChildNodes.Count;        return inCout;    }    public string getXML(string tag,int cout){        string xml= XMLDout.GetElementsByTagName (tag) [Index].ChildNodes [cout].InnerText;        return xml;    }}

地址:git@code.csdn.net:snippets/2593113.git

最后就是主要的逻辑实现
XML的文件如此

<?xml version="1.0" encoding="utf-8"?>  <Xml>      <JD>          <Name>云天河:还记得吗?我们第一次来树屋时,我说以后要到山上隐居,不问江湖世事,那些……都还想是昨天的情景,可仔细想想,原来已经过了那么久啊,发生了好多好多的事……</Name>          <X>韩菱纱:我只想找个地方,静静了结自己的生命……日子久了,你和紫英会慢慢把我忘记的,忘记了,就不会伤心了……</X>          <Y>云天河:可是,人一旦要死了,发而会多很多牵挂,想起以前的事……</Y>      </JD>  </Xml> 

主要逻辑在下方连接

git@code.csdn.net:snippets/2593106.git

对话框的功能基本就实现了
这里写图片描述
后面还会实现更多的功能,现在列一个列表
1.UI对话框
2.剧情系统
3.背包系统
4.打怪以及被怪物追着打
5.与node服务器结合
目前就想到那么多..

原创粉丝点击