【Unity】2D 对话模块的实现

news/2024/10/24 0:17:31/

对话模块主要参考 【Unity教程】剧情对话系统 实现。

在这次模块的构建将基于 unity ui 组件 和 C#代码实现一个从excel 文件中按照相应规则读取数据并展示的逻辑。这套代码不仅能实现正常的对话,也实现了对话中可以通过选择不同选项达到不同效果的分支对话功能。

整一套代码分为分为两部分,一部分和库存模块一样通过 Collider 2D 和 Unity Event 构建了一个范围内可互动的功能,这一部分可以参考之前的库存模块。

剩下一部分就是对话框模块整体逻辑,先看一下效果:

在这里插入图片描述

在这里插入图片描述

从上图中,可以看出整个对话框可以分为五个部分:头像、说话人名字、普通对话内容、跳转到下一句的按钮、和 选择对话框。可以简单将 普通对话内容和跳转按钮 划分成一个逻辑组件,而选择对话框划分成另一个逻辑组件,这样划分的原因在于两者实现方式不一样。

接下来内容将分为三个段落,我将自下而上一一实现:从Excel中读取对话内容的方式、普通对话实现 和 选择对话 这三个步骤:

从Excel中读取对话内容的方式

整一个Excel的内容分为了五个部分,如下图所述,分别是、

对话的id,用来快速定位到一个对话类,可以方便我们进行查找和使用

说话人的名字,用来展示在 对话框中的UI组件

说话人的头像,也是用来展示在对话框中的 UI 和 名字不同的是他是代表一个文件的路径

对话内容,用来展示在 对话框中的UI组件

下一句对话id,用来做跳转使用

在这里插入图片描述

定义好具体数据的Excel之后,需要将Excel导出成Unity可以识别的编码格式,否则在Unity中会被识别成乱码(这一步可以通过 txt 文本另存为的方式进行变更)。将另存为的文本保存在unity 项目中的 Assets/Resources/Dialogue/路径下,以便项目能够读取到。

在这里插入图片描述

接下来,在C#中定义一个对应的class,用来接住Excel读取出的数据,并在C#中通过Resources.Load的方法来读取。Resoruces.Load的方法会将整个文本以 string的方式进行读取,所以还需要对每一行文本进行拆分,才能处理成我们需要的对话类。最终,将 对话 id 和 对话实体保存到一个 dictionary里,方便后面的步骤进行调用。

// 对话类class Dialogue{public int dialogueId;public string characterName;public string characterIcon;public string dialogue;public List<int> toDialogueIdList;public Dialogue(int dialogueId, string characterName, string characterIcon, string dialogue, List<int> toDialogueIdList){this.dialogueId = dialogueId;this.characterName = characterName;this.characterIcon = characterIcon;this.dialogue = dialogue;this.toDialogueIdList = toDialogueIdList;}}//public void eventDialogueFileProcess(int eventId){   // 文件处理成 对话 id 和 对话对象 的映射dialogueDictionary.Clear();string filePath = dialogueFilePrefix + eventId;TextAsset textFile = Resources.Load<TextAsset>(filePath);processDialogueFile(textFile);    }private void processDialogueFile(TextAsset dialogueFile){string[] dialogueArrays = dialogueFile.text.Split("\r\n");foreach(string strDialogue in dialogueArrays){if(string.IsNullOrEmpty(strDialogue)){continue;}string[] cols = strDialogue.Split(',');string[] strToDialogueIds = cols[4].Split('|');List<int> toDialogueIdList = new List<int>();foreach(string strToDialogueId in strToDialogueIds){   if(string.IsNullOrEmpty(strToDialogueId)){break;}Debug.Log(strToDialogueId);toDialogueIdList.Add(int.Parse(strToDialogueId));}Dialogue dialogue = new Dialogue(int.Parse(cols[0]), cols[1], cols[2], cols[3], toDialogueIdList);dialogueDictionary.Add(dialogue.dialogueId, dialogue);}}

普通对话实现

普通对话实现的主要方式就是将当前对话的 id 保存为一个成员对象,这样在触发按钮的点击事件之后,便能通过事件监听的方式调用这个对话id来获取下一段对话。

// 正常对话int nextDialogueId = dialogueIdList[0];if(dialogueDictionary.TryGetValue(nextDialogueId, out Dialogue dialogue)){// 隐藏多选dialogueMutliBG.enabled = false;// 显示正常对话dialogueText.enabled = true;nextButton.gameObject.SetActive(true);characterIcon.sprite = Resources.Load<Sprite>(characterIconPrefix + dialogue.characterIcon);characterName.text = dialogue.characterName;dialogueText.text = dialogue.dialogue;currentDialogueIndex = dialogue.dialogueId; }

选择对话

选择对话相比普通对话来说,实现有一些复杂,主要在于需要用C# 的 delegate 代理一个函数,来达成下一步对话的操作。这样做的原因在于选择对话每一个对话都会有一个下一个对话的选项,导致没有办法直接使用普通对话定义的当前对话id变量。

// 可选对话选择List<Dialogue> dialogues = new List<Dialogue>();foreach(int index in dialogueIdList){if(dialogueDictionary.TryGetValue(index, out Dialogue dialogue)){dialogues.Add(dialogue);}}// 隐藏正常对话dialogueText.enabled = false;nextButton.gameObject.SetActive(false);// 显示多选dialogueMutliBG.enabled = true;foreach(Dialogue dialogue in dialogues){// 依次生成对话 ButtonoptionButton = Instantiate(optionButton, dialogueMutliBG.transform);TextMeshProUGUI textMeshPro = optionButton.GetComponentInChildren<TextMeshProUGUI>();textMeshPro.text = dialogue.dialogue;// 添加事件监听optionButton.GetComponent<Button>().onClick.AddListener(delegate {// 点完以后 销毁选择对话框Button[] optionalButtons = dialogueMutliBG.GetComponentsInChildren<Button>();foreach(Button button in optionalButtons){Destroy(button.gameObject);}// 显示下一句对话showDialogue(dialogue.toDialogueIdList);});}

将这两部分合入一个函数中,通过下一段对话id 的数量进行判断到底是 普通对话还是选择对话,合并后的代码如下:


using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;public class DialogueControl : MonoBehaviour
{class Dialogue{public int dialogueId;public string characterName;public string characterIcon;public string dialogue;public List<int> toDialogueIdList;public Dialogue(int dialogueId, string characterName, string characterIcon, string dialogue, List<int> toDialogueIdList){this.dialogueId = dialogueId;this.characterName = characterName;this.characterIcon = characterIcon;this.dialogue = dialogue;this.toDialogueIdList = toDialogueIdList;}}public Image characterIcon;public TextMeshProUGUI characterName;public TextMeshProUGUI dialogueText;private static string dialogueFilePrefix = "Dialogue/event_dialogue_";private static string characterIconPrefix = "Character/";private Dictionary<int, Dialogue> dialogueDictionary = new Dictionary<int, Dialogue>();private int currentDialogueIndex = 1;public Image dialogueMutliBG;public Button optionButton;public Button nextButton;public int lastEvent;// Start is called before the first frame updatevoid Start(){transform.gameObject.SetActive(false);}// Update is called once per framevoid Update(){}public void eventDialogueFileProcess(int eventId){   transform.gameObject.SetActive(true);// 文件处理成 对话 id 和 对话对象 的映射if(!lastEvent.Equals(eventId)){dialogueDictionary.Clear();string filePath = dialogueFilePrefix + eventId;TextAsset textFile = Resources.Load<TextAsset>(filePath);processDialogueFile(textFile);}// 显示第一段对话List<int> dialogueIdList = new List<int>(1);dialogueIdList.Add(1);showDialogue(dialogueIdList);lastEvent = eventId;}private void processDialogueFile(TextAsset dialogueFile){string[] dialogueArrays = dialogueFile.text.Split("\r\n");foreach(string strDialogue in dialogueArrays){if(string.IsNullOrEmpty(strDialogue)){continue;}string[] cols = strDialogue.Split(',');string[] strToDialogueIds = cols[4].Split('|');List<int> toDialogueIdList = new List<int>();foreach(string strToDialogueId in strToDialogueIds){   if(string.IsNullOrEmpty(strToDialogueId)){break;}Debug.Log(strToDialogueId);toDialogueIdList.Add(int.Parse(strToDialogueId));}Dialogue dialogue = new Dialogue(int.Parse(cols[0]), cols[1], cols[2], cols[3], toDialogueIdList);dialogueDictionary.Add(dialogue.dialogueId, dialogue);}}private void showDialogue(List<int> dialogueIdList){if(dialogueIdList.Count == 0){return;}if(dialogueIdList.Count > 1){// 可选对话选择List<Dialogue> dialogues = new List<Dialogue>();foreach(int index in dialogueIdList){if(dialogueDictionary.TryGetValue(index, out Dialogue dialogue)){dialogues.Add(dialogue);}}// 隐藏正常对话dialogueText.enabled = false;nextButton.gameObject.SetActive(false);// 显示多选dialogueMutliBG.enabled = true;foreach(Dialogue dialogue in dialogues){// 依次生成对话 ButtonoptionButton = Instantiate(optionButton, dialogueMutliBG.transform);TextMeshProUGUI textMeshPro = optionButton.GetComponentInChildren<TextMeshProUGUI>();textMeshPro.text = dialogue.dialogue;// 添加事件监听optionButton.GetComponent<Button>().onClick.AddListener(delegate {// 点完以后 销毁选择对话框Button[] optionalButtons = dialogueMutliBG.GetComponentsInChildren<Button>();foreach(Button button in optionalButtons){Destroy(button.gameObject);}// 显示下一句对话showDialogue(dialogue.toDialogueIdList);});}}else {// 正常对话int nextDialogueId = dialogueIdList[0];if(dialogueDictionary.TryGetValue(nextDialogueId, out Dialogue dialogue)){// 隐藏多选dialogueMutliBG.enabled = false;// 显示正常对话dialogueText.enabled = true;nextButton.gameObject.SetActive(true);characterIcon.sprite = Resources.Load<Sprite>(characterIconPrefix + dialogue.characterIcon);characterName.text = dialogue.characterName;dialogueText.text = dialogue.dialogue;currentDialogueIndex = dialogue.dialogueId;}}}public void nextDialogue(){if(dialogueDictionary.TryGetValue(currentDialogueIndex, out Dialogue dialogue)){if(dialogue.toDialogueIdList.Count == 0){transform.gameObject.SetActive(false);nextButton.gameObject.SetActive(false);}showDialogue(dialogue.toDialogueIdList);}}}

来看一下最终效果

请添加图片描述


http://www.ppmy.cn/news/1114606.html

相关文章

【rtp-benchmarks】读取本地文件基于uvgRtp实现多线程发送

input 文件做内存映射 : get_mem D:\XTRANS\soup\uvg-rtp-dev\rtp-benchmarks\util\util.cc 文件中读取chunksize 到 vector 里作为chunks 创建多个线程进行发送 std::vector<std::thread*> threads;

Linux常见指令和基础知识

文章目录 前言一、Linux下基本指令1、ls2、pwd3、cd4、touch5、mkdir6、rmdir7、 rm8、man9、cp10、mv11、echo12、cat 及 tac13、more14、less15、head16、tail17、find18、grep19、zip/unzip20、tar21、常用热键 二、Linux下拓展指令1、date2、Cal3、bc4、uname5、top6、关机…

3D Web轻量化引擎HOOPS:轻松解决OSGB模型复杂性与性能挑战!

在当今的数字时代&#xff0c;三维模型的创建和展示对于众多行业都至关重要。无论是用于游戏开发、虚拟现实体验、建筑设计还是工程仿真&#xff0c;高质量的3D模型都能够提供更真实的视觉效果和更精确的数据表示。 然而&#xff0c;随着模型的复杂性增加&#xff0c;其数据量…

目标检测笔记(十五): 使用YOLOX完成对图像的目标检测任务(从数据准备到训练测试部署的完整流程)

文章目录 一、目标检测介绍二、YOLOX介绍三、源码获取四、环境搭建4.1 环境检测 五、数据集准备六、模型训练七、模型验证八、模型测试 一、目标检测介绍 目标检测&#xff08;Object Detection&#xff09;是计算机视觉领域的一项重要技术&#xff0c;旨在识别图像或视频中的…

【算法与数据结构】108、LeetCode将有序数组转换为二叉搜索树

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;这道题给我们的是一个有序数组&#xff0c;并要求构成一个平衡二叉搜索树&#xff0c;二叉搜索树的很容…

SpringBoot携带Jre绿色部署项目[Linux服务器]

文章目录 SpringBoot携带Jre绿色部署项目[Linux服务器]1. 实现步骤2. 自测成功&#xff0c;如下2-1 环境准备2-2 运行项目 SpringBoot携带Jre绿色部署项目[Linux服务器] 说明&#xff1a; 实际应用的不方便场景&#xff1a;1. 实际项目部属时&#xff0c;现有服务器可能已安装…

ubuntu 配置NTP时间服务器

sudo apt update 显示秒&#xff08;进入Top Bar 打开second&#xff09; sudo apt install gnome-tweaks 安装ntp服务器 sudo apt install ntp 在服务端修改ntp配置开放客户端所在的网段 sudo gedit /etc/ntp.conf restrict 172.19.7.0 mask 255.255.255.0 nomodify notrap 重…

GICv3学习

GICv3学习 参考文档&#xff1a; 《corelink_gic600_generic_interrupt_controller_technical_reference_manual_100336_0106_00_en》 《IHI0069H_gic_architecture_specification》 《ECM0495013B_GIC_Stream_Protocol》 一、GICv3寄存器接口 接口如下图所示&#xff1a…