Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

File and Directory limitations

Discussion in 'Windows' started by bakno, Apr 17, 2014.

  1. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    595
    In order to use the File and Directory functions, we put this at the beginning of our C# script:

    #if UNITY_WINRT
    using UnityEngine.Windows;
    using File = UnityEngine.Windows.File;
    using Directory = UnityEngine.Windows.Directory;
    #else
    using System.IO;
    using File = System.IO.File;
    using Directory = System.IO.Directory;
    #endif

    However, there are a few functions that are not supported by UnityEngine.Windows. For example: Directory.GetFiles or File.Move.

    Is there a way to workaround these limitations?

    Specifically asking about our most important problem to solve: Is there a way to get the list of files in a folder / directory?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    You can use StorageFile and StorageFolder classes from Windows.Storage namespace.
    In order to use them, the script must be compiled using .NET Core and the code with these classes wrapped with #if NETFX_CORE.
    Or you can write a plugin.
     
  3. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    595
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    There would be another solution. When you select "Use .NET Core partially", scripts in Assets/Standard Assets, Assets/Pro Standard Assets and Assets/Plugins are compiled using Mono C# compiler, while all the others are compiled using Microsoft C# compiler. In result, JS can see classes from those three folders and you can use WinRT APIs in all other C# scripts.
     
  5. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    595
    Thank you for the explanation Tautvydas. This might help on future projects.

    The problem is that the C# functions being called from JS are precisely those with the WinRT APIs
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,507
    Another solution without changing your program architecture would be setting up callbacks to scripts that are allowed to call WinRT APIs.
     
  7. bakno

    bakno

    Joined:
    Mar 18, 2007
    Posts:
    595
    Hi Tautvydas

    Would it be possible to use Unity Networking by selecting "Use .NET Core partially", and placing the Networking related scripts in Standard Assets?
     
  8. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,644
    No. That option just switches the compiler, which allows accessing C# classes from UnityScript and Boo. The cost of that is that these scripts can not use WinRT API.
    This compilation option does not make any part of Unity API work. It might compile scripts in some cases, but they will still fail at runtime. Exception is restricted .NET APIs, which might compile and work, but they will fail certification later, so you won't be able to submit your game to store.
     
  9. trunkz

    trunkz

    Joined:
    Jul 18, 2014
    Posts:
    29
    Hi there, i got the same problem with Directiory.GetFile() with Win Store, so can u help me ? I'm newbee.
    Thanks !