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

相关文章

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

maven中的maven-antrun-plugin插件示例详解

《maven中的maven-antrun-plugin插件示例详解》maven-antrun-plugin是Maven生态中一个强大的工具,尤其适合需要复用Ant脚本或实现复杂构建逻辑的场景... 目录1. 核心功能2. 典型使用场景3. 配置示例4. 关键配置项5. 优缺点分析6. 最佳实践7. 常见问题