提示:以下是本篇文章正文内容
主要使用TextAsset组件
说明:可以使用的格式

| 变量 | 作用 |
|---|---|
| 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);
注意:可以使用 MonoBehaviour.StopCoroutine 和 MonoBehaviour.StopAllCoroutines 停止协程。销毁 MonoBehaviour 时,或是如果 MonoBehaviour 所附加到的 GameObject 已禁用,也会停止协程。 禁用 MonoBehaviour 时,不会停止协程。
因为相关素材需要9.99$,这里就直接分享Github链接了
GitHub链接:点击跳转到GitHub下载素材


如:

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 strings = new List();
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 strings = new List();
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);
}
}
效果如下: