- Home /
GetDirectories() does not work on Android
Hi, I've been trying to retrieve directories within Resources folder (Assets -> Resources -> DiscoveryPage -> Bloggers -> various folders), and it only works on Unity Editor on run time, but not on my Android device.
Here's what I have:
DirectoryInfo discoveryDir;
DirectoryInfo[] bloggerDirs;
int numBloggers;
void Start () {
if (Application.platform == RuntimePlatform.Android)
{
//discoveryDir = new DirectoryInfo(Application.dataPath + "/Resources/DiscoveryPage/Bloggers");
//discoveryDir = new DirectoryInfo(Application.persistentDataPath + "/Resources/DiscoveryPage/Bloggers");
discoveryDir = new DirectoryInfo("jar:file://" + Application.dataPath + "!/assets" + "/Resources/DiscoveryPage/Bloggers");
MainUI.ShowDebug("Running on Android. @" + discoveryDir.Name);
}
else
{
discoveryDir = new DirectoryInfo(Application.dataPath + "/Resources/DiscoveryPage/Bloggers");
}
bloggerDirs = discoveryDir.GetDirectories();
if(bloggerDirs == null)
MainUI.ShowDebug("bloggerDirs is null");
else
MainUI.ShowDebug("bloggerDirs is NOT null");
The line "MainUI.ShowDebug("Running on Android. @" + discoveryDir.Name);" did show up with proper information, but
if (bloggerDirs == null)
MainUI.ShowDebug("bloggerDirs is null");
else
MainUI.ShowDebug("bloggerDirs is NOT null");
doesn't return anything (not even null). No debug text was printed out.
Seems like discoveryDir.GetDirectories(); does not work on Android. Anyone knows the problem and how to get those directories on Android run time?
Answer by Bunny83 · Jun 01, 2016 at 11:42 AM
The Assets folder only exists in the Unity project. It's not available in any build as all used assets are packed into an internal asset database when you built your game. Assets in a Resources folder can only be accessed with Resources.Load.
There's no built-in way to find out what assets are available. I've made a fairly simple system to store the information about assets in a resources folder in an asset so you can get that information at runtime.
Apart from that you can use the StreamingAssets folder instead. Files in that folder are ignored by Unity's asset system and just copied into the build in some way. Just read the documentation page.