Unity简单实用的文本对话插件,可以做任务说明框

2023-12-07 12:10

本文主要是介绍Unity简单实用的文本对话插件,可以做任务说明框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现需求:类似对话,
开始对话,
1.播放问题文字,打字机动画播放文字,鼠标点击播完所有文字,
2.再次点击,播放回答文字。
3.再次播放问题文字。
4.如果第2步没有对应文字,则跳到第1步,第1步没有对应文字则跳到第2步。

引用了DoTween插件和特性拓展插件NaughtyAttributes。
https://github.com/dbrizov/NaughtyAttributes
末尾有百度云链接
在这里插入图片描述
在这里插入图片描述

核心代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using NaughtyAttributes;
using UnityEngine.Events;
using UnityEditor;
/// <summary>
/// 对话,需引用dotween
/// 设置好,使用play即可
/// </summary>
public enum DialogueType
{speek,answer
}
public class DialogueCrt : MonoBehaviour
{private DialData dataFlag = new DialData();public bool showList = true;[ShowIf("showList")][ReorderableList]public List<DialogueData> dialogueList;private GameObject speakPrefab;private GameObject answerPrefab;private List<GameObject> speakList = new List<GameObject>();private List<GameObject> answerList = new List<GameObject>();private GameObject mNowObj;private int index = 0;private UnityAction mAction;private bool isEnd = false;private bool isKeyDown =false;public bool isUseJson = false;[ShowIf("isUseJson")]public TextAsset json;[Button]private void Json2List(){if (json == null || !isUseJson)throw new System.Exception("json未添加,或者没勾选isUseJson,请勾选isUseJson并添加json");else{Undo.RegisterFullObjectHierarchyUndo(gameObject, "json");dataFlag = JsonUtility.FromJson<DialData>(json.text);dialogueList = dataFlag.dialogueList;showList = true;Debug.Log("成功将json添加到list");}}/// <summary>/// 播放dialogue/// </summary>public void Play(){Debug.Log("play");Remove();isEnd = false;gameObject.SetActive(true);if (speakList.Count > 0){ShowObjText(speakList[index], DialogueType.speek);}}/// <summary>/// 播放dialogue,并添加结束事件/// </summary>/// <param name="action">结束调用事件</param>public void Play(UnityAction action){Debug.Log("playAction");Remove();isEnd = false;gameObject.SetActive(true);if (speakList.Count > 0){ShowObjText(speakList[index], DialogueType.speek);}mAction = action;}/// <summary>/// 结束dialogue/// </summary>public void Close(){DiaEnd();}private void Init(){speakPrefab = Resources.Load<GameObject>("Speak");answerPrefab = Resources.Load<GameObject>("Answer");foreach (DialogueData d in dialogueList){if (true){GameObject obj = Instantiate(speakPrefab, transform);speakList.Add(obj);}if (true){GameObject obj = Instantiate(answerPrefab, transform);answerList.Add(obj);}}Remove();}private void Remove(){EndObjText(mNowObj);index = 0;for (int i = 0; i < speakList.Count; i++){speakList[i].SetActive(false);}for (int i = 0; i < answerList.Count; i++){answerList[i].SetActive(false);}}void Awake(){Init();gameObject.SetActive(false);}void Update(){if (isEnd)return;if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space)){if(!isTweenEnd)EndObjText(mNowObj);elseNext();}}private void DiaEnd(){Debug.Log("完成");isEnd = true;gameObject.SetActive(false);if (mAction != null)mAction.Invoke();}private void Next(){if (mNowObj == speakList[index]){ShowObjText(answerList[index], DialogueType.answer);}else if (mNowObj == answerList[index]){index++;if (index >= dialogueList.Count){DiaEnd();return;}ShowObjText(speakList[index], DialogueType.speek);}}private bool isTweenEnd = false;private void ShowObjText(GameObject obj, DialogueType type){mNowObj = obj;string str = null;if (!obj.activeSelf){switch (type){case DialogueType.speek:str = dialogueList[index].speakStr;break;case DialogueType.answer:str = dialogueList[index].answerStr;break;}obj.GetComponentInChildren<Text>().ShowText(str,()=> { isTweenEnd = true; });if (str == null || str == ""){EndObjText(mNowObj);Next();return;}isTweenEnd = false;obj.SetActive(true);}}private void EndObjText(GameObject obj){if (obj != null){obj.GetComponentInChildren<Text>().EndTextAnim();isTweenEnd = true;}}
}[System.Serializable]
public class DialData
{public List<DialogueData> dialogueList;
}
[System.Serializable]
public class DialogueData
{public string speakStr;public string answerStr;}

DoTween动画代码

using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.Events;
using UnityEngine;public static class TextDT
{public static void ShowText(this Text _text, string _str,TweenCallback cb, float _time = 0.4f){_text.text = "";_text.DOText(_str, _time * _str.Length).OnComplete(cb);}public static void EndTextAnim(this Text _text){_text.DOGoto(1000f, false);}
}

使用2018.4.2制作
百度云盘:链接:https://pan.baidu.com/s/1jkk2Ru_FdnRJ_OZkf8frtw 密码:rg1w

如果文章对你有帮助,不妨关注我一下,点个赞。
我会一直分享Unity开发中解决的坑,分享学到的技术,也会分享读书心得和理财心得,谢谢大家。

这篇关于Unity简单实用的文本对话插件,可以做任务说明框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/465820

相关文章

C#TextBox设置提示文本方式(SetHintText)

《C#TextBox设置提示文本方式(SetHintText)》:本文主要介绍C#TextBox设置提示文本方式(SetHintText),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录C#TextBox设置提示文本效果展示核心代码总结C#TextBox设置提示文本效果展示核心代

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

使用Python实现文本转语音(TTS)并播放音频

《使用Python实现文本转语音(TTS)并播放音频》在开发涉及语音交互或需要语音提示的应用时,文本转语音(TTS)技术是一个非常实用的工具,下面我们来看看如何使用gTTS和playsound库将文本... 目录什么是 gTTS 和 playsound安装依赖库实现步骤 1. 导入库2. 定义文本和语言 3

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

Python实现常用文本内容提取

《Python实现常用文本内容提取》在日常工作和学习中,我们经常需要从PDF、Word文档中提取文本,本文将介绍如何使用Python编写一个文本内容提取工具,有需要的小伙伴可以参考下... 目录一、引言二、文本内容提取的原理三、文本内容提取的设计四、文本内容提取的实现五、完整代码示例一、引言在日常工作和学

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

Java实现将Markdown转换为纯文本

《Java实现将Markdown转换为纯文本》这篇文章主要为大家详细介绍了两种在Java中实现Markdown转纯文本的主流方法,文中的示例代码讲解详细,大家可以根据需求选择适合的方案... 目录方法一:使用正则表达式(轻量级方案)方法二:使用 Flexmark-Java 库(专业方案)1. 添加依赖(Ma