Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to File access UWP

Discussion in 'Windows' started by liou28335, Apr 11, 2021.

  1. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    Could someone please tell me how to access files in UWP? no one seems to have the answer. I cannot even access windows.storage in unity
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    What files are you trying to access? Windows.Storage should work.
     
  3. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11

    windows.storage does shows the red underline indicating an error, even when i surround it with the #if block. I am simple trying to access pictures saved on my desktop. Could you show me the code for that? thanks
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
  5. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11

    Hello, I just want to be able to list the names of the files in desktop. I am looking for an alternative to System.Environment.specialfolder in the regular c#. UWP seems like hell to develop for. When I use the file picker it builds without error but doesnt work.
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    You are not allowed to access random places like the desktop programmatically from UWP apps. You could, however, use a folder picker and ask the user pick a folder, and then you should be able to freely operate on all the files inside.

    When you say it doesn't work, what exactly happens? Did you try using a debugger?
     
  7. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    I know this might sound embarrassing, but i am struggling to call the async function.
    These lines seem to be cool.

    var picker = new Windows.Storage.Pickers.FileOpenPicker(); picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail; picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;

    However when i get to this line

    Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();

    The compiler requests that i wrap it in an async function. when i do that, i do not know how to call that async function in an onclick function. I keep getting errors.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    Do this:

    Code (csharp):
    1. void OnClick()
    2. {
    3.     UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
    4.     {
    5.         var picker = new Windows.Storage.Pickers.FileOpenPicker();
    6.         picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    7.         picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
    8.  
    9.         file = await picker.PickSingleFileAsync();
    10.  
    11.         // Do stuff with file here
    12.         ....
    13.  
    14.         // Come back to Unity's/Game's main thread
    15.         UnityEngine.WSA.Application.InvokeOnAppThread(() =>
    16.         {
    17.              // Process the computed result, it's safe to interact with all scene objects and Unity API here.
    18.         }, false);
    19.     }, false);
    20. }
     
  9. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    Hello, I am no more getting any errors and it builds fine, however the file dialog does not pop up, I cannot see any file dialog show up.
     
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    Does the code even get called? Did you try debugging it? Any exceptions happening?
     
  11. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    Yes the code gets called by a button click. button.OnClick.addListener(()=> OnClick()); I even added a little text display to ensure that it executes that piece of code. No error is given so far.

    The problem seems to be with this line. file = await picker.PickSingleFileAsync();

    When i include that line, the text that is supposed to show up to indicate that everything went fine didnt show up.
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    Try wrapping it in try/catch to see if any exceptions are thrown.
     
  13. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    I get a COMException Unspecified error
     
  14. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11
    This line seems to be the devil.

    file = await picker.PickSingleFileAsync();

    When I remove it i do not get errors
     
  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
  16. liou28335

    liou28335

    Joined:
    Feb 22, 2021
    Posts:
    11