- Home /
How to get the contents of a directory?
I want to get a list of files in a sub directory under the Resources folder from a c# script in Unity.
I am looking at the File and Directory functions but what they do is minimal.
How would I do this?
The Resources folder does not exist in a build. Unless you're doing this exclusively in the editor, it will not work.
Third time I am posting this...
I want to load a set of jpg files in several directories into and array of Texture[].
I was expecting that the Resources dir to be available as a root folder and I would create folders under Resources and place jpg texture files in the folders I create.
The number of jpg files in the directories will vary.
Thanks for the 'using System.IO' tidbit. This may be what I was missing (I'm new to c#) because Directory was undefined.
But I guess I have a new issue in that the Resources folder only exists within Unity editor.
How can I have a directory that holds files that is part of the Unity game once I deploy it to run on a device (like an iPhone)?
Answer by jgodfrey · Dec 27, 2014 at 05:24 AM
You probably want Directory.GetFiles()
There are lots of folder / file methods available in .NET/Mono...
Can I call Directory.GetFiles() from a C# script in Unity? Do you perhaps have some sample code?
Hmmm... I can't seem to add a new answer, but...
I'm not sure what's wrong with my original answer. The following code will retrieve all *.TXT files from the C:\ folder. Though, you'll need to add a 'using System.IO' to have access to the Directory class.
void Start()
{
foreach (string file in Directory.GetFiles(@"c:\", "*.txt"))
{
Debug.Log(file);
}
}
Nothing was wrong with your answer, although the OP should state what the actual intent of the question is, since as I mentioned there is no Resources folder as such in a build.
@Eric5h5 - Agreed on all counts. Though, I thought there was more explanation here a few $$anonymous$$utes ago, but I don't see it now (maybe OP deleted).
I want to load a set of jpg files in several directories into and array of Texture[].
I was expecting that the Resources dir to be available as a root folder and I would create folders under Resources and place jpg texture files in the folders I create.
The number of jpg files in the directories will vary.
Thanks for the 'using System.IO' tidbit. This may be what I was missing (I'm new to c#) because Directory was undefined.
But I guess I have a new issue in that the Resources folder only exists within Unity editor.
How can I have a directory that holds files that is part of the Unity game once I deploy it to run on a device (like an iPhone)?
$$anonymous$$y other choice would appear to be to make all my texture holding variables 'public texture' and drop the texture into them in the editor. But this is inefficient when I will have a large number of texture files.
$$anonymous$$y use here is that I will have varied planet textures (space game) that will applied to a sphere. I expect 100's of stars with 1 to 12 planets each. I will have varied texture jpg files (maybe a 100 or so) with the planets surface.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How can i get a function to return 2D List? 0 Answers
WWW file:// protocol to list a directory 1 Answer
Variable will not change? 1 Answer
Function call is not working 0 Answers