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
What files are you trying to access? Windows.Storage should work.
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
You’ll have to wrap the code if #if ENABLE_WINMD_SUPPORT && UNITY_WSA. For accessing files on your desktop, I suggest using the file picker. There’s some example code here: Open files and folders with a picker - UWP applications | Microsoft Learn
Make sure to invoke all UI functions on the UI thread, which you can switch to using this API: Unity - Scripting API: WSA.Application.InvokeOnUIThread
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.
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?
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.
Do this:
void OnClick()
{
UnityEngine.WSA.Application.InvokeOnUIThread(async () =>
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
file = await picker.PickSingleFileAsync();
// Do stuff with file here
....
// Come back to Unity's/Game's main thread
UnityEngine.WSA.Application.InvokeOnAppThread(() =>
{
// Process the computed result, it's safe to interact with all scene objects and Unity API here.
}, false);
}, false);
}
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.
Does the code even get called? Did you try debugging it? Any exceptions happening?
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.
Try wrapping it in try/catch to see if any exceptions are thrown.
I get a COMException Unspecified error
This line seems to be the devil.
file = await picker.PickSingleFileAsync();
When I remove it i do not get errors
Looking at Microsoft samples, looks like you have to add one or more FileTypeFilters before calling PickSingleFileAsync. See this: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-using-file-and-folder-pickers
It now works, you are my knight in shining armour. cheers