I am trying to discover basic file in/out on the HoloLens. I am using the following Code:
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
}
}
The Code written in the Start() function seems to work, I can access the text file through the Device Portal. But the text of my 3D Text object does not change into "Test Start" then (though it is in the text file).
Any idea why writing works but reading doesn't?
Two things:
Application.persistentDataPath
. You should be prefixing your paths: var path = Path.Combine(Application.persistentDataPath, "MyPath.txt");
File
operations instead. To write:
File.WriteAllBytes(Encoding.UTF8.GetBytes(text));
To read:
Encoding.UTF8.GetString(File.ReadAllBytes(path));
This API is supported on UWP and in .NET Framework so you also don't need your conditional compilation directives.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.