首页 > 解决方案 > 在 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();
   } 

对话框问题

标签: c#unity3d

解决方案


推荐阅读