简体   繁体   English

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

[英]How can I do reading and writing TextAssets at runtime with Unity 3D?

I have my desired txt files which I want to use as TextAssets.我有我想要用作 TextAssets 的 txt 文件。 I need these files to be usable at runtime by my other scripts.我需要这些文件在运行时可供我的其他脚本使用。 Now the issue is that I can not figure out a way to make these things work.现在的问题是我想不出让这些东西起作用的方法。

I know that I should be using the Assets/Resources or the Streaming Assets folder but for some reason things are not working properly.我知道我应该使用 Assets/Resources 或 Streaming Assets 文件夹,但由于某种原因,事情不能正常工作。 Is there a way to incorporate it all with StreamWriters and Filestreams?有没有办法将它与 StreamWriters 和 Filestreams 结合起来? What about TextAssets assigned in Unity Editor, can those also be setup as Streaming?在 Unity Editor 中分​​配的 TextAssets 怎么样,那些也可以设置为 Streaming? Some examples of code that uses my assets:一些使用我的资产的代码示例:

    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());     
        }


All help greatly appreciated, I've basically messed up big time since I only found out about this now when I built everything as is...非常感谢所有帮助,我基本上搞砸了很长时间,因为我现在在按原样构建所有东西时才发现这一点......

Edit: got the streamwriters to do everything nicely with Application.persistentDataPath!编辑:让流编写器使用 Application.persistentDataPath 很好地完成所有事情! Now stuck with a problem that I already struggled with - how to assign a TextAsset to get the file from a fixed path...现在遇到一个我已经在努力解决的问题 - 如何分配 TextAsset 以从固定路径获取文件...

public TextAsset textFile; 

Wondering how to set this to get it's .txt from Application.persistentDataPath想知道如何设置它以从 Application.persistentDataPath 获取它的 .txt

Application.persistentDataPath is what was needed all along. Application.persistentDataPath一直是需要的。

Something nobody ever mentioned wherever I looked around.无论我环顾四周,都没有人提到过。 Hope somebody will be able to find the correct way using this mess of a question and lackluster answer.希望有人能够使用这个混乱的问题和乏味的答案找到正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM