Ok, I know the ‘' symbol is used by the editor to separate the differents folders’ names when accessing to a specific file. I my case, what I want to do is to go to the Resources/Maps folder in my Assets folder and load all .txt files in my Maps folder.
And yes, I have already tried Resources.LoadAll(), but I want Unity to return a fileInfo and resources.LoadAll doesn’t allow this.
Here is my script :
private void Add_Options_To_Dropdown()
{
DirectoryInfo levelDirectoryPath = new DirectoryInfo(Application.dataPath);
FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources\Maps", SearchOption.TopDirectoryOnly);
foreach (FileInfo file in fileInfo)
{
List<string> dropOptions = new List<string>(fileInfo.Length);
dropOptions.Add(file.Name);
if (file.Extension == ".txt")
{
if(dropdownLoad.options.Count > 0)
dropdownLoad.ClearOptions();
dropdownLoad.AddOptions(dropOptions);
dropdownLoad.RefreshShownValue();
}
}
}
When I’m trying to load the directory (2nd line), Unity doesn’t recognize the '' symbol. I tried a ‘/’ but I get an error that explicitely says that this function uses '' instead of ‘/’ to load the desired folder. I have tried this :
Because I have read somewhere that putting ' before a \ allowed in C# to recognize \ as a normal character that can be put in a string. I also know that this trick is used to implement other special characters, like “\n”.
But this time, I’m quite lost. I’ve searched a lot of Unity forums speaking about the same issue, and I found nothing. I know where my mistake is, but I don’t know how to solve it.
hello Brathnann, using @ doesn’t work. And, just like I said earlier, using / doesn’t work either. The problem remains the same : The script cannot go further that the Resources folder. It completely ignores the Maps folder. I also tried using Resources.LoadAll(), but either I’m not using it properly or it cannot be used in this specific case. I wrote :
List<TextAsset> fileInfo = Resources.LoadAll("Resources/Maps", typeof(TextAsset)) as TextAsset;
List<string> dropOptions = new List<string>(fileInfo.Length);
foreach (FileInfo file in fileInfo)
{
dropOptions.Add(file.Name);
}
foreach (FileInfo file in fileInfo)
{
if (file.Extension == ".txt")
{
if(dropdownLoad.options.Count > 0)
dropdownLoad.ClearOptions();
dropdownLoad.AddOptions(dropOptions);
dropdownLoad.RefreshShownValue();
}
}
And it still doesn’t work. it says that there’s a conversion issue at the line where I attempt to load all files located in my folder, but I cannot figure out why, even though I’m pretty sure the solution is really simple.
It’s because LoadAll does not return a TextAsset, or anything convertible to a TextAsset. It returns Object[ ] (as shown in the docs), i.e., an array of type Object.
While reading the docs you might also notice this:
So, use forward slashes, and pay attention to what types you’re dealing with, and you should be able to make this work.
Ok, after one night and another day spent hittinh my head against my table, I give up. I managed to get rid of my errors, but I still can’t figure out why my script doesn’t add my textAssets to my Object[ ] array. Here is my code without errors :
private void Add_Options_To_Dropdown()
{
//DirectoryInfo levelDirectoryPath = new DirectoryInfo(Application.dataPath);
//FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources");
Object[] fileInfo = Resources.LoadAll("Resources/Maps", typeof(TextAsset));
print(fileInfo.Length);
List<string> dropOptions = new List<string>(fileInfo.Length);
foreach (TextAsset file in fileInfo)
{
dropOptions.Add(file.name);
}
foreach (TextAsset file in fileInfo)
{
if (file.name == "*.txt")
{
if (dropdownLoad.options.Count > 0)
{
dropdownLoad.ClearOptions();
}
dropdownLoad.AddOptions(dropOptions);
dropdownLoad.RefreshShownValue();
}
}
}
When I execute the function, it is supposed to print the length of my array after loading all my text files, but it remains at 0. It doesn’t add them to my array. Judging by your answer, it might be a pretty dumb solution, but I can’t find it.
This is when I see anwsers like this that I realize I’m a stupid developer.
Thanks, Brathnann. It was exactly my problem. What’s more shameful is, I’ve already used Resources.Load() in another script, and I haven’t even noticed that I simply removed “Resources” in the path. But now it works. Thanks you all, and sorry for bothering you for a stupid issue like this one.
There are probably very few, if any, developers that have never run into a situation where they just overlook something. Sometimes another pair of eyes helps. Glad it’s working now.