Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question HoloLens2- Access file names

Discussion in 'Windows' started by cahm29, Mar 24, 2023.

  1. cahm29

    cahm29

    Joined:
    Feb 7, 2020
    Posts:
    7
    Hi,

    I am working on a HoloLens2 project where I have to access a file. In my unity project, I usually keep the file in StreamingAssets folder and can easily access it from the editor. But when I try to access the file for UWP, I don't get any output in HoloLens.

    Code (CSharp):
    1.  string folderPath = Application.streamingAssetsPath;
    2. #if UNITY_EDITOR
    3.         var files = Directory.GetFiles(folderPath);
    4.         if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
    5.             voxel = File.OpenRead(files[0]);
    6.  
    7. #elif UNITY_WSA
    8.  
    9.                 var files = new[]{folderPath + "/file_name.bin"};
    10.  
    11.                 if (UnityEngine.Windows.File.Exists(files[0]) && (files[0].Contains(".bin")))
    12.                     voxel = File.OpenRead(files[0]);
    I can see the output in HoloLens when I specify the file name. That is what I am trying to avoid.

    It would be helpful if you could suggest how to read files in UWP
     
  2. CarePackage17

    CarePackage17

    Joined:
    Dec 6, 2014
    Posts:
    22
    No need for special casing; the System.IO namespace works in Editor and in UWP builds (at least after a regression was fixed in 2021.3.19).

    Code (CSharp):
    1.  
    2. string folderPath = Application.streamingAssetsPath;
    3. var files = Directory.GetFiles(folderPath, "*.bin");
    4.  
    5. if (files.Length > 0)
    6. {
    7.     using (var stream = File.OpenRead(files[0]))
    8.     {
    9.         //read from stream here
    10.     }
    11. }
    12.  
     
  3. cahm29

    cahm29

    Joined:
    Feb 7, 2020
    Posts:
    7
    Thanks a lot!
    It is working perfectly fine.
     
    CarePackage17 likes this.