【发布时间】:2018-03-22 11:51:11
【问题描述】:
我正在尝试在 HoloLens 上发现基本文件输入/输出。我正在使用以下代码:
public class FileInOut : MonoBehaviour {
string plainText = "";
TextMesh textmesh;
// Use this for initialization
void Start () {
textmesh = GameObject.Find ("Text").GetComponent<TextMesh>();
//test if Text changes normally
textmesh.text = "Hallo";
//create the text file and put text into it
#if WINDOWS_UWP
Task task = new Task(async () =>
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile textFileForWrite = await storageFolder.CreateFileAsync("LocalText.txt");
await FileIO.WriteTextAsync(textFileForWrite, "Test Start");
});
task.Start();
task.Wait();
#endif
}
// Update is called once per frame
void Update () {
//Read the text file and change the text of 3D text to it (not working)
#if WINDOWS_UWP
Task task = new Task(async () =>
{
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile textFileforRead = await storageFolder.GetFileAsync("LocalText.txt");
plainText = await FileIO.ReadTextAsync(textFileforRead);
textmesh.text = plainText;
});
task.Start();
task.Wait();
#endif
}
}
Start() 函数中编写的代码似乎可以工作,我可以通过设备门户访问文本文件。但是我的 3D 文本对象的文本不会变成“测试开始”(尽管它在文本文件中)。
知道为什么写作有效而阅读无效吗?
【问题讨论】:
-
有没有试过将plainText设置为public,看看赋值是否正确?
-
HoloLens 在读取和写入文件时有一些奇怪的地方。我发现除非你使用
Windows.Storage.KnownFolders或Application.persistentDataPath作为文件路径,否则它不会太喜欢它。
标签: c# unity3d file-io hololens