Unity简单实现RPG游戏对话框
发表于2018-11-08
在RPG游戏剧情中出现最多的该属对话框吧,那么这个对话框功能在开发中是如何被实现的呢,这篇文章就给大家分享下RPG游戏中对话框功能的实现教程,一起来看看吧。
直接上效果图
可以支持多个选项
对话框的类结构
工程基于GameFrame框架,主要逻辑如下:
void ChangeScence() { Singleton<SceneManager>.GetInstance().LoadScene("Login", () => { Singleton<WindowManager>.GetInstance().OpenWindow("Dialogue",true,true,new DialogueContent("dialoguedata.json","node1")); }); }
UI框架的DialogueContent的类结构如下: 支持当前对话 的模型头像的一些支持 比如当前说话的角色头像模型变大之类的
public class DialogueContent : WindowContext { public string FileName ; public string StartNode; public Sprite LeftIcon; public GameObject LeftModel; public Sprite RightIcon; public GameObject RightModel; public int ShowType = 0;// 不显示 1 显示ICON 2 显示 MODEL public DialogueContent(string file,string node,Sprite lefticon = null ,Sprite righticon = null,GameObject leftmodle = null,GameObject rightmodel = null,int show = 0) { FileName = file; StartNode = node; LeftIcon = lefticon; RightIcon = righticon; LeftModel = leftmodle; RightModel = rightmodel; ShowType = show; } }
//初始化按钮的监听事件 for (int i = 0; i < 4; i++) { Answers[i] = CacheTransform.Find("Content/AnswerBtn" + (i + 1)).gameObject; EventTriggerListener.Get(Answers[i].gameObject).SetEventHandle(EnumTouchEventType.OnClick, (a, b, c) => { StopContentText(); string nextNode = NodeData.Answers[(int) (c[0])].Target; if (nextNode == "end") { //关闭界面 Singleton<WindowManager>.GetInstance().CloseWindow(false,"Dialogue"); return; } NodeData = GetDialogue(nextNode); Coroutine = SingletonMono<GameFrameWork>.GetInstance().StartCoroutine(ShowContentText()); },i); } //更新全局的当前对话的数据 private DialogueNodeData GetDialogue(string nodename) { for (int i = 0; i < Data.Nodes.Length; i++) { if (Data.Nodes[i].Character == nodename) { return Data.Nodes[i]; } } return null; } //显示按钮与对话文字 private IEnumerator ShowContentText() { TitleText.text = NodeData.Character; //显示按钮 for (int i = 0; i < 4; i++) { if (NodeData.Answers.Length > i) { Answers[i].SetActive(true); Answers[i].transform.Find("Text").gameObject.GetComponent<Text>().text = NodeData.Answers[i].Option; } else { Answers[i].SetActive(false); } } ShowText.text = ""; char[] letters = NodeData.Dialogue.ToCharArray(); for (int i = 0; i < letters.Length; i++) { ShowText.text += letters[i]; yield return new WaitForEndOfFrame(); } }
简单实现了一个编辑对话框的Editor扩展
效果如下: