我正在尝试在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);
}
void Update()
{
}
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();
}
}
在Unity中使用Ink进行对话。