Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

How to deploy and read data file with app.

Someone reached out and asked me how to package a simple xml data file with their 3D Unity UWP project and how to programmatically find and read the file from within the HoloLens app on the device.

They seemed happy with the answer so I thought I would share it in case others are looking to do something similar.

Windows Holographic User Group Redmond

WinHUGR.org - - - - - - - - - - - - - - - - - - @WinHUGR
WinHUGR YouTube Channel -- live streamed meetings

Tagged:

Best Answers

Answers

  • What would be the method for logic to write the text file using FileIO?

  • edited December 2016

    Great Information! Thank you!

    I'm stuck on load a saveData StorageFile if it exists or create a new StorageFile if it doesn't. I've tried StoreFile.GetFileAsync, & CreateFileAsync("saveData", CreationCollisionOption.OpenIfExists) with no luck. When loading Save.txt, the Json string always returns "". Any Advice?

    var saveData = ScriptableObject.CreateInstance();
    saveData.PlayerExperience = 0;

    var storageFolder = ApplicationData.Current.LocalFolder;
    StorageFileQueryResult results = storageFolder.CreateFileQuery();

    IReadOnlyList filesInFolder = await results.GetFilesAsync();

    foreach (StorageFile item in filesInFolder)
    {
    if (item.Name == "Save.txt")
    {
    StorageFile tempFile = await storageFolder.GetFileAsync("Save.txt");
    saveTextFile = tempFile;
    _jsonString = await FileIO.ReadTextAsync(saveTextFile);
    }
    }

    if (_jsonString != "")
    {
    saveData = JsonUtility.FromJson(_jsonString);
    }
    else
    {
    // For Testing
    saveData.PlayerExperience = 7;
    }

    _jsonString = JsonUtility.ToJson(saveData);
    saveTextFile = await storageFolder.CreateFileAsync("Save.txt",CreationCollisionOption.ReplaceExisting);
    await FileIO.WriteTextAsync(saveTextFile, _jsonString);

    Happy Holidays!!

  • edited December 2016

    It works!! Thank you so much!!

    try
    {
    if (await storageFolder.TryGetItemAsync("Save.txt") != null)
    {
    saveTextFile = await storageFolder.GetFileAsync("Save.txt");
    _jsonString = await FileIO.ReadTextAsync(saveTextFile);
    var serializedData = CreateFromJson(_jsonString);
    saveData = serializedData;
    }
    }
    catch (Exception e)
    {
    Debug.Log(e);
    throw;
    }

  • If I do not use Unity (use DirectX and c++/cx) and want to load a big binary data, can I load it from network at run time?

    Thanks.

  • Hello,

    I also tried in the unity Forums, if anybody wants to have a look:
    https://forum.unity3d.com/threads/still-huge-problems-saving-data-permanent-folders-inside.487530/

    I have my app on the hololens. It should save an XML file containing Texts and data (it is a new file, not deployed or something) to the hololens itself (not One Drive).
    It has links to Pictures and Videos which I can record with the app myself or maybe upload additional files. These must also be stored on the hololens, I would prefer the same Folder like the XML file. If possible, it should be stored under a user given "assistant Name".

    Like suggested here I tried the LocalFolder and others:

    if !UNITY_EDITOR && UNITY_METRO

        //Globals.DataPath = Windows.Storage.ApplicationData.Current.RoamingFolder.Path;
        //Globals.DataPath = Application.streamingAssetsPath;
        //Globals.DataPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
        //Globals.DataPath = Application.dataPath;
        //Globals.DataPath = Application.persistentDataPath;
        //Globals.DataPath = Windows.Storage.KnownFolders.CameraRoll.Path; 
        //Globals.DataPath = Windows.Storage.KnownFolders.DocumentsLibrary.Path;
    
        Globals.DataPath = Application.persistentDataPath;
    

    else

        Globals.DataPath = Application.persistentDataPath;
    

    endif

    But if I install an update of the app, or completly shut down the hololens all files are gone! Or cannot be seen anymore. I could see them with the filebrowser on my PC linked to the hololens, I cannot find them anymore after shutdown or app update.

    Any help please?

  • PS:
    When I use
    Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path;
    it seems to survive the shutdown and restart of the hololens. But when I deploy the app again, all files are gone...

  • Hello,

    This is exactly what I need! I just have two quick follow-up questions:

    @HoloSheep said:
    In the Visual Studio (Universal Windows) project that gets generated from the Unity build process you can add your XML or text file either to the root of the project or you can create a subdirectory in the project and put the file there, you will just need to adjust the Uri path accordingly.

    Is the correct location at AppName/GeneratedProjects/UWP/
    or somewhere further down the line in the Assembly-CSharp Folders?

    And will I be able to see the file somewhere on the file explorer?

    Best Wishes :)

  • @HoloSheep said:
    In the Visual Studio (Universal Windows) project that gets generated from the Unity build process you can add your XML or text file either to the root of the project or you can create a subdirectory in the project and put the file there, you will just need to adjust the Uri path accordingly.

    You will want to include the following conditional using statements at the top of the script that you plan to insert the file read logic into:

    #if WINDOWS_UWP
    using Windows.Storage;
    using System.Threading.Tasks;
    using Windows.Data.Xml.Dom;
    using System;
    #endif

    And then inside the body of that script where you wish to read the file:

    string xmlText;

    #if WINDOWS_UWP

    Task task = new Task(

    async () =>
    {
    var xmlFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///xmlData/Test.xml"));
    XmlDocument xdoc = await XmlDocument.LoadFromFileAsync(xmlFile);
    xmlText = xdoc.GetXml();

    });
    task.Start();
    task.Wait();

    #endif

    The above sample is expecting your xml file to be in a subdirectory in your project called:
    xmlData
    and the file in this case is named
    Text.xml

    In Visual Studio, if you right click on the file and bring up the Properties window for the file make sure it is marked as:

    Build Action – Content
    Copy to Output Directory – Copy if newer

    alternatively, if you want to do something similar with a plain text file you could instead use:

    string plainText;
    
    #if WINDOWS_UWP        
    
            Task task = new Task(
    
                async () =>
                {                              
                    StorageFile textFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Text.txt"));
                    plainText = await FileIO.ReadTextAsync(textFile);
    
                    txtTextMesh.text = plainText;
    
                });
            task.Start();
            task.Wait();
    
    #endif
    

    and something similar if it contained JSON that you wanted to parse.

    The person who originally asked wanted a simple way to store some meta data in a file in the project that they could read at runtime.

    HTH.

    What if we wand to read an asset bundle(.unity3d) file that is stored in hololens known folders?

Sign In or Register to comment.