UNITYTextAssest去创建一个对话系统
UNITYTextAssest去创建一个对话系统
TextAsses:
Inherits from Object
Text file assets.
文本文件资源。
You can use raw .txt files in your project as assets and get their contents through this class.
在你的工程里,你可以使用原始的.txt文件作为资源并且通过该类获取它们的内容。
核心代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class TextTest : MonoBehaviour
{
//public Text Text;
Start is called before the first frame update
//void Start()
//{
//}
//string ReadLine(string name,int line)
//{
// string[] file=File.ReadAllLines(name);
// return file[line];
//}
//public void Read(int line)
//{
// Text.text = ReadLine("TxtInfo.txt", line);
//}
Update is called once per frame
//void Update()
//{
//}
private void Start()
{
GetTextFromFile(textfile);
index = 0;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
TextLabel.text = textList[index];
index++;
}
}
[Header("UI组件")]
public Text TextLabel;
public Image faceImage;
[Header("文本文件")]
public TextAsset textfile;
public int index;
List<string> textList = new List<string>();
void GetTextFromFile(TextAsset file)
{
textList.Clear();
index = 0;
var LineData= file.text.Split('\n');
foreach (var line in LineData)
{
textList.Add(line);
}
}
}
图中注释的为另一种方法,目前遇到了点小bug.
以下为文本文档
你好啊!
你好啊!有事吗!
有兴趣和我聊聊吗?
没有兴趣!你走吧
我不。