当前位置:网站首页>Unity简单实现对话功能
Unity简单实现对话功能
2022-07-28 03:06:00 【MaNongSa】
提示:以下是本篇文章正文内容
简单说明
主要使用TextAsset组件
TextAsse相关使用
- 描述
- 文本文件资源。
- 您可以将项目中的原始文本文件用作资源,通过此类获取其 内容。此外,如果要从二进制文件访问数据,可将这种文件作为原始字节数组进行访问。请参阅 Text Asset 手册页,了解有关将文本和二进制文件作为文本资源导入项目的更多详细信息。
说明:可以使用的格式

- 变量
| 变量 | 作用 |
|---|---|
| bytes | 文本资源的原始字节。(只读) |
| text | 字符串形式的 .txt 文件的文本内容。(只读) |
- 构造函数
| 函数名 | 作用 |
|---|---|
| TextAsset | 使用指定文本内容创建一个新 TextAsset。此构造函数创建 TextAsset,它与纯文本文件不同。当使用 AssetDatabase 类保存到磁盘时,应使用 .asset 扩展名保存 TextAsset。 |
- 公共函数
| 函数名 | 作用 |
|---|---|
| ToString | 返回 TextAsset 的内容。 |
- 继承的成员
- 变量
| 变量 | 作用 |
|---|---|
| hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
| name | 对象的名称。 |
- 公共函数
| 公共函数 | 作用 |
|---|---|
| GetInstanceID | 返回对象的实例 ID。 |
- 静态函数
| 函数名 | 作用 |
|---|---|
| Destroy | 移除 GameObject、组件或资源。 |
| DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
| DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
| FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
| FindObjectsOfType | 返回所有类型为 type 的已加载的激活对象的列表。 |
| Instantiate | 克隆 original 对象并返回克隆对象。 |
协程的相关使用
MonoBehaviour.StartCoroutine
public Coroutine StartCoroutine (IEnumerator routine);
- 描述
- 启动协程。
- 可以使用 yield 语句,随时暂停协程的执行。使用 yield 语句时,协程会暂停执行,并在下一帧自动恢复。请参阅协程文档以了解更多详细信息。
- 协程非常适合用于在若干帧中对行为建模。StartCoroutine 方法在第一个 yield 返回时返回,不过可以生成结果,这会等到协程完成执行。即使多个协程在同一帧中完成,也不能保证它们按照与启动相同的顺序结束。
- 生成的任何类型(包括 null)都会导致执行在后面的帧返回,除非协程已停止或完成。
注意:可以使用 MonoBehaviour.StopCoroutine 和 MonoBehaviour.StopAllCoroutines 停止协程。销毁 MonoBehaviour 时,或是如果 MonoBehaviour 所附加到的 GameObject 已禁用,也会停止协程。 禁用 MonoBehaviour 时,不会停止协程。
相关素材
因为相关素材需要9.99$,这里就直接分享Github链接了
GitHub链接:点击跳转到GitHub下载素材
UI相关创建


创建TextAsset支持的格式对话文本
如:
1、 创建Dialogue脚本,按下R进行简单对话
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dialogue : MonoBehaviour
{
public Image image;
public Text text;
public TextAsset textAsset;
public int index;
private List<string> strings = new List<string>();
private void Awake()
{
DialogueS(textAsset);
index = 0;
}
private void OnEnable()
{
text.text = strings[index];
index++;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R) && strings.Count == index)
{
gameObject.SetActive(false);
index = 0;
return;
}
if(Input.GetKeyDown(KeyCode.R))
{
text.text = strings[index];
index++;
}
}
private void DialogueS(TextAsset textAsset)
{
strings.Clear();
index = 0;
var textList=textAsset.text.Split('\n');
for (int i = 0; i < textList.Length; i++)
{
strings.Add(textList[i]);
}
}
}
效果如下:

2、 通过协程使文章文本逐渐显示、快速显示、图片切换以及跳过。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Dialogue : MonoBehaviour
{
public Image image;
public Text text;
public TextAsset textAsset;
public GameObject DialogueObj;
public int index;
public float showTextTimer;
private bool isOnClickR;
private bool isWhetherFast;
public Sprite A_Img, B_Img;
private List<string> strings = new List<string>();
private void Awake()
{
DialogueS(textAsset);
}
private void OnEnable()
{
text.text = strings[index];
index++;
isOnClickR = true;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R) && strings.Count == index)
{
gameObject.SetActive(false);
index = 0;
return;
}
//if(Input.GetKeyDown(KeyCode.R) && isOnClickR)
//{
// //text.text = strings[index];
// //index++;
// StartCoroutine(GraduallyRevealed());
//}
if (Input.GetKeyDown(KeyCode.R))
{
if (isOnClickR && !isWhetherFast)
{
StartCoroutine(GraduallyRevealed());
}
else if (!isOnClickR)
{
isWhetherFast = !isWhetherFast;
}
}
}
private void DialogueS(TextAsset textAsset)
{
strings.Clear();
index = 0;
var textList=textAsset.text.Split('\n');
for (int i = 0; i < textList.Length; i++)
{
strings.Add(textList[i]);
}
}
IEnumerator GraduallyRevealed()
{
text.text = "";
isOnClickR = false;
switch (strings[index])
{
case "A":
image.sprite = A_Img;
break;
case "B":
image.sprite = B_Img;
break;
}
//for (int i = 0; i < strings[index].Length; i++)
//{
// text.text += strings[index][i];
// yield return new WaitForSeconds(showText);
//}
int letter = 0;
while (!isWhetherFast && letter < strings[index].Length-1)
{
text.text += strings[index][letter];
letter++;
yield return new WaitForSeconds(showTextTimer);
}
text.text = strings[index];
isOnClickR = true ;
isWhetherFast = false;
index++;
}
public void SkipDialogue()
{
text.text = "";
index = 0;
isWhetherFast = false;
isOnClickR = false;
StopAllCoroutines();
DialogueObj.SetActive(false);
}
}
效果如下:
边栏推荐
- Stm32f407 ------- FPU learning
- Pytoch correlation gradient echo
- Shell writing specifications and variables
- Practice of online problem feedback module (16): realize the function of checking details
- MySQL的碎片有哪些
- 关于湖北文理学院平衡信标组的疑问回应
- Data Lake (XVII): Flink and iceberg integrate datastream API operations
- ThreadLocal usage scenario
- Animation
- The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,
猜你喜欢
随机推荐
Win11怎么显示固定应用?
【R语言】环境指定删除 rm函数
【Codeforces Round #806 (Div. 4)(A~F)】
Shell writing specifications and variables
Original title of Blue Bridge Cup
C # set TextBox control not editable
20220726汇承科技的蓝牙模块HC-05的AT命令测试
Defect detection of BP SVM system design of leaf defect detection
图文并茂:JVM 内存布局详解
Embedded database -- SQLite
"Introduction to engineering electromagnetic field" after class exercises with answers
Analysis of redis network model
如何解决mysql深分页问题
bp svm的缺陷检测 树叶缺陷 叶片缺陷检测的系统设计
C -- switch case statement
鼠标操作和响应
xctf攻防世界 Web高手进阶区 unserialize3
Version compatibility issues
Redis经典面试题总结
Leetcode 29th day








