- Home /
DirectoryInfo and FileInfo not working in standalone build
I've read in another question that to parse a collection of files from a folder to use DirectoryInfo
and FileInfo
from System.IO
which both work fine within the confine of the editor.
However, once I build a stand-alone version (even PC), those classes and methods fail to work properly.
Is there anyway to retrieve a list of assets contained in a folder once the assets are properly built in a unity resources files?
Example of what I'm doing:
DirectoryInfo root = new DirectoryInfo("Assets/Resources/Sound/Music");
FileInfo[] infos = root.GetFiles();
musics = new Music[infos.Length];
for (int i = 0; i < infos.Length; i++)
musics[i] = new Music(infos[i].Name.Split('.')[0]);
return musics;
While being there, is there other .NET assembly that won't work properly in other environment such as iOS/Android? If I use System.Reflection
, will I get some nasty surprises down the line?
Answer by Graham-Dunnett · Aug 04, 2013 at 08:48 AM
When you make a standalone the contents of Assets\Resources
are compressed into a data file. You access the contents of this file at runtime using Resources.Load()
.
On iOS all your script code and .Net bytecode is converted into Arm assembler. There is no reflection or runtime code generation possible on this platform.
Good grief, this is terrible. Nobody should tell people to use System.IO
or System.Reflection
, it's just seeking troubles! And nobody is going to run a game in the editor. Do you mean Activator
is also out of question? Would typeof()
or GetType()
even works since they access the class definition, which is reflection?
A lot of system.reflection should work, I'd think, things like finding members and attributes, I'm not sure how JScript or Boo would work without those pieces.
Things like dynamically loading assemblies and emitting bytecode cannot work on iOS.
It is, but there are some caveats. The big issue is that iOS compiles at link time ins$$anonymous$$d of at run-time so things like Emit aren't supported and never will be.
http://docs.xamarin.com/guides/ios/advanced_topics/limitations
The first links talk about Xamarin, and I guess the attributes specified in it is Xamarin's specific? Is a workaround to prevent class from being flush to explicitly reference them somewhere in your code paths or does Unity provide attributes to prevent code removal?
Answer by chin13577 · Jan 15, 2017 at 05:31 PM
This is my code when I try to read all of .txt file in folder "Resource".
path = "FolderInResource/SubFolder/";
UnityEngine.Object[] fileInfo = Resources.LoadAll(path, typeof(TextAsset));
foreach (var file in fileInfo)
{
TextAsset mytext = (TextAsset)file;
string str = mytext.text;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Namespaces that correspond to file location 1 Answer
Debug.Log(files in a directory) problem 0 Answers
Mac/PC differences in directory symbols '\' and '/' 2 Answers