C# 如何使用Unity 3D在运行时读取和写入TextAssets?

C# 如何使用Unity 3D在运行时读取和写入TextAssets?,c#,unity3d,runtime,streamwriter,C#,Unity3d,Runtime,Streamwriter,我有我想要的txt文件,我想用作文本资源。我需要这些文件在运行时可以被我的其他脚本使用。现在的问题是,我想不出一个办法来让这些东西发挥作用 我知道我应该使用Assets/Resources或streamingassets文件夹,但由于某些原因,事情不能正常工作。有没有一种方法可以将其与StreamWriter和Filestreams结合起来?在Unity Editor中分配的TextAssets如何,它们也可以设置为流媒体吗? 使用我的资产的一些代码示例: public void Tas

我有我想要的txt文件,我想用作文本资源。我需要这些文件在运行时可以被我的其他脚本使用。现在的问题是,我想不出一个办法来让这些东西发挥作用

我知道我应该使用Assets/Resources或streamingassets文件夹,但由于某些原因,事情不能正常工作。有没有一种方法可以将其与StreamWriter和Filestreams结合起来?在Unity Editor中分配的TextAssets如何,它们也可以设置为流媒体吗? 使用我的资产的一些代码示例:

    public void TaskOnClick() //getting multi-values
    {
        string filename = "Assets/Resources/TempoText/multi-export.txt";
        using (StreamWriter writeFile = new StreamWriter(filename, false))
        {
            foreach (string inputJson in File.ReadLines("Assets/Resources/TempoText/multi-import.txt"))
            {
                string temperature = GetTemperatureByRegex(inputJson);
                Debug.Log(temperature);
                writeFile.AutoFlush = true;
                Console.SetOut(writeFile);
                writeFile.WriteLine(temperature.ToString());
            }

        }
        File.Copy("Assets/Resources/TempoText/multi-export.txt", "Assets/Resources/multi-export.txt", true);
    }
//or

        FileStream filestream = new FileStream("Assets/Resources/TempoText/multi-import.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        var writeFile = new StreamWriter(filestream);

        {
            var document = collection.Find(new BsonDocument()).Sort(sort).Limit(limit: limit).ForEachAsync(d => Console.WriteLine(d)); //displays last 10 entries
            Debug.Log(document.ToString());
            writeFile.AutoFlush = true;
            Console.SetOut(writeFile);
            writeFile.Write(document.ToString());     
        }


非常感谢所有的帮助,我基本上把事情搞砸了,因为我现在才发现这一点,当我按原样建造一切的时候

编辑:让StreamWriter可以很好地使用Application.persistentDataPath完成所有工作!现在我遇到了一个我一直在努力解决的问题-如何分配TextAsset以从固定路径获取文件

public TextAsset textFile; 

想知道如何设置它以从Application.persistentDataPath.Application.persistentDataPath.persistentDataPath


无论我在哪里看,都没有人提到过。希望有人能够找到正确的方法来使用这些杂乱无章的问题和平淡无奇的答案。

“资产”作为一个文件夹只存在于编辑器中,这些路径在构建项目时不存在。由于您试图读取的文件位于参考资料中,因此可以使用
Resources.Load
来加载它们。如果希望将它们作为外部文件,则需要将它们放置在其他位置,并使用
Application.persistentDataPath
(或类似)立即保存到C:/Users/Public/Documents/Looking into Application.persistentDataPath来解决问题。似乎是我需要的!当我得到工作字符串path=path.Combine(Application.persistentDataPath,“testext”)时保持更新;path=path.Combine(路径,“多重导入”+“.txt”);如果(!Directory.Exists(Path.GetDirectoryName(Path)){Directory.CreateDirectory(Path.GetDirectoryName(Path));}对于
Resources
文件夹,Unity明确声明
不要使用它
,所以我不会说
我知道我应该使用资产/资源
;)