Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我调用执行此操作的函数时,静态列表不会更新_C#_Unity3d_Parsing_Static_Static Variables - Fatal编程技术网

C# 当我调用执行此操作的函数时,静态列表不会更新

C# 当我调用执行此操作的函数时,静态列表不会更新,c#,unity3d,parsing,static,static-variables,C#,Unity3d,Parsing,Static,Static Variables,我对Unity有点陌生,我正在尝试制作一部视觉小说。 我有一个带有静态变量行的类,它是一个对话行列表。我有第二个类,它使用LoadDialogue()方法读取文本文件,并将对话行添加到静态变量行中。我有第三个类,它调用LoadDialogue(),从静态变量中获取一行对话,并在单击鼠标按钮时显示它 当我运行程序并按下鼠标按钮时,我得到ArgumentOutOfRangeException。我知道这是因为我试图从中获取一行对话的列表是空的,但我不知道为什么它是空的。我不知道还能尝试什么,因为我对语

我对Unity有点陌生,我正在尝试制作一部视觉小说。 我有一个带有静态变量行的类,它是一个对话行列表。我有第二个类,它使用LoadDialogue()方法读取文本文件,并将对话行添加到静态变量行中。我有第三个类,它调用LoadDialogue(),从静态变量中获取一行对话,并在单击鼠标按钮时显示它

当我运行程序并按下鼠标按钮时,我得到ArgumentOutOfRangeException。我知道这是因为我试图从中获取一行对话的列表是空的,但我不知道为什么它是空的。我不知道还能尝试什么,因为我对语言和引擎都非常不熟悉

下面是我的变量类的代码(MyTypes是一个单独的类,我用来创建一个结构和一些枚举):

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共第五类:单一行为{
公共静态列表行;
公共静态MyTypes.TimeOfDay时间;
公众安全日;
公共信贷;
}
下面是我的对话解析类的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ParseDialogue : MonoBehaviour {
    public void LoadDialogue() {
        string fileName = "Assets/Resources/Dialogue/";
        fileName += V.time.ToString();
        fileName += V.day.ToString();
        fileName += ".txt";

        V.lines = new List<MyTypes.DialogueLine>();
        ReadDialogue(fileName);
    }

    public void ReadDialogue(string fileName){
        string line = "New Line";
        StreamReader reader = null;

        try {
            using(reader = new StreamReader(fileName)){
                do {
                    line = reader.ReadLine();
                    string[] content = line.Split('|');
                    Enum.TryParse<MyTypes.DisplayPosition>(content[3], out MyTypes.DisplayPosition position);
                    MyTypes.DialogueLine newLine = new MyTypes.DialogueLine(content[0], content[1], position, int.Parse(content[2]));
                    V.lines.Add(newLine);
                } while (line != null);
                reader.Close();
            }
        } catch (Exception e){
            Console.WriteLine("The file could not be read.");
            Console.WriteLine(e.Message);
        }
    }
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.IO;
公共课堂对话:单一行为{
公众对话({
字符串fileName=“资产/资源/对话/”;
fileName+=V.time.ToString();
fileName+=V.day.ToString();
文件名+=“.txt”;
V.lines=新列表();
ReadDialogue(文件名);
}
公共void ReadDialogue(字符串文件名){
string line=“新行”;
StreamReader=null;
试一试{
使用(读卡器=新的StreamReader(文件名)){
做{
line=reader.ReadLine();
string[]content=line.Split(“|”);
Enum.TryParse(内容[3],out MyTypes.DisplayPosition);
MyTypes.DialogueLine newLine=新的MyTypes.DialogueLine(内容[0],内容[1],位置,int.Parse(内容[2]);
V.行。添加(换行符);
}while(line!=null);
reader.Close();
}
}捕获(例外e){
WriteLine(“无法读取文件”);
控制台写入线(e.Message);
}
}
}
下面是显示对话的类的代码(Parser是我创建的空对象,用于将ParseDialogue脚本放入其中):

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课堂对话:单一行为{
语法分析器;
公开对话;
int-lineNum;
公共风格风俗;
void Start(){
parser=GameObject.Find(“解析器”).GetComponent();
LoadDialogue()语法分析器;
对话=”;
lineNum=0;
}
无效更新(){
if(Input.GetMouseButtonDown(0)| | Input.GetKey(“空格”)){
dialogue=V.lines[lineNum].getLine();
lineNum++;
}
}
void OnGUI(){
dialogue=GUI.TextField(新的Rect(25400300200),dialogue,custom);
}
}
如果你能帮我,我会非常非常感激的


编辑:我解决了这个问题。问题是我将内容[3]切换为内容[2]。

能否在Input.GetMouseButton(0)之后添加Debug.Log(lineNum+“:“+V.lines.Count)并显示崩溃前显示的最后一个值。。ans告诉我你的文件中有多少行我刚试过,得到了0。所以你还没有加载你的列表。。您的加载器有问题..请检查您在加载行后是否有记录。我建议你展示你的txt文件,并解释你想在其中捕获什么..很抱歉回复太晚了!我做了一些研究,我想知道我的ReadDialogue()方法是否不是静态的?如果不是这样的话,我明天会给你回复,因为我晚上已经把代码放好了。你能在输入之后添加Debug.Log(lineNum+“:“+V.lines.Count)吗?GetMouseButton(0)并显示崩溃前显示的最后一个值。。ans告诉我你的文件中有多少行我刚试过,得到了0。所以你还没有加载你的列表。。您的加载器有问题..请检查您在加载行后是否有记录。我建议你展示你的txt文件,并解释你想在其中捕获什么..很抱歉回复太晚了!我做了一些研究,我想知道我的ReadDialogue()方法是否不是静态的?如果不是这样的话,我明天会给你打电话,因为我已经把我的代码留到晚上了。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ParseDialogue : MonoBehaviour {
    public void LoadDialogue() {
        string fileName = "Assets/Resources/Dialogue/";
        fileName += V.time.ToString();
        fileName += V.day.ToString();
        fileName += ".txt";

        V.lines = new List<MyTypes.DialogueLine>();
        ReadDialogue(fileName);
    }

    public void ReadDialogue(string fileName){
        string line = "New Line";
        StreamReader reader = null;

        try {
            using(reader = new StreamReader(fileName)){
                do {
                    line = reader.ReadLine();
                    string[] content = line.Split('|');
                    Enum.TryParse<MyTypes.DisplayPosition>(content[3], out MyTypes.DisplayPosition position);
                    MyTypes.DialogueLine newLine = new MyTypes.DialogueLine(content[0], content[1], position, int.Parse(content[2]));
                    V.lines.Add(newLine);
                } while (line != null);
                reader.Close();
            }
        } catch (Exception e){
            Console.WriteLine("The file could not be read.");
            Console.WriteLine(e.Message);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DisplayDialogue : MonoBehaviour {
    ParseDialogue parser;

    public string dialogue;
    int lineNum;

    public GUIStyle custom;

    void Start() {
        parser = GameObject.Find("Parser").GetComponent<ParseDialogue>();
        parser.LoadDialogue();

        dialogue = "";
        lineNum = 0;
    }

    void Update() {
        if(Input.GetMouseButtonDown(0) || Input.GetKey("space")) {
            dialogue = V.lines[lineNum].getLine();
            lineNum++;
        }
    }

    void OnGUI() {
        dialogue = GUI.TextField(new Rect(25, 400, 300, 200), dialogue, custom);
    }
}