c# - 在 Unity 中实现对话系统的问题
问题描述
我正在尝试在 Unity 中制作自己的视觉小说。我制作了一个文本管理器脚本来加载文本文件并正确阅读它们,并制作了一个文本框脚本来将文本加载到文本框中。在我的层次结构中,我制作了两个对象,一个用于文本框脚本,另一个用于对话管理器。(DialogueParserObj) 我的目标是将文本文件放入文本框中,该文本框被视为 TextBox 脚本中的对话字符串变量,因此您可以在我标记的对话框中看到实际的文本文件。但它仍然是空的,我不知道为什么,也没有收到任何错误。
我非常感谢您的帮助或其他建议,以建立一个结构良好的对话系统!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueBox : MonoBehaviour
{
TextManager parser;
public string dialogue;
int lineNum;
public GUIStyle customStyle;
void Start()
{
dialogue = "";
parser = GameObject.Find("DialogueParserObj").GetComponent<TextManager>();
lineNum = 0;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Return))
{
dialogue = parser.GetContent(lineNum);
lineNum++;
}
}
void OnGUI()
{
dialogue = GUI.TextField(new Rect(100, 400, 600, 200), dialogue, customStyle);
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class TextManager : MonoBehaviour
{
string readPath;
List<string> stringList = new List<string>();
List<DialogueLine> lines = new List<DialogueLine>();
struct DialogueLine
{
public string name;
public string content;
public int pose;
public DialogueLine(string n, string c, int p)
{
name = n;
content = c;
pose = p;
}
}
void Start()
{
readPath = Application.dataPath + "/text.txt";
ReadFile(readPath);
}
public string GetName(int lineNumber)
{
if (lineNumber < lines.Count)
{
return lines[lineNumber].name;
}
return "";
}
public string GetContent(int lineNumber)
{
if (lineNumber < lines.Count)
{
return lines[lineNumber].content;
}
return "";
}
public int GePose(int lineNumber)
{
if (lineNumber < lines.Count)
{
return lines[lineNumber].pose;
}
return 0;
}
void ReadFile(string filePath)
{
StreamReader r = new StreamReader(filePath);
while (!r.EndOfStream)
{
string line = r.ReadLine();
if (line != null)
{
string[] lineValues = line.Split('|');
DialogueLine lineEntry = new DialogueLine(lineValues[0], lineValues[1], int.Parse (lineValues [2]));
stringList.Add(line);
}
}
r.Close();
}
解决方案
推荐阅读
- java - 在多个内核中处理一个 HTTP 请求
- python - 不要在 Python3 中自动将十六进制转换为十进制
- evolutionary-algorithm - jDE(自适应差分进化)
- java - 如何在休眠中重新生成已删除的序列号?
- javascript - 如何计算Django模板标签中for循环中链接的点击次数?
- html - 包裹在其他父母中的元素的最后一个孩子?
- c++ - 为什么 UINT32_MAX + 1 = 0?
- java - 将字符串从 onLoadFinished 方法传递到另一个活动
- mysql - 获取返回行值的百分比
- c++ - Qt 和 C++:将 QLineEdit 添加到 QTabWidget