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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to print the "\" symbol ?

Discussion in 'Scripting' started by Quasar47, Feb 9, 2018.

  1. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    117
    Error : Unrecognized escape sequence '\M'...

    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 :

    Code (CSharp):
    1.     private void Add_Options_To_Dropdown()
    2.     {
    3.  
    4.         DirectoryInfo levelDirectoryPath = new DirectoryInfo(Application.dataPath);
    5.         FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources\Maps", SearchOption.TopDirectoryOnly);
    6.  
    7.         foreach (FileInfo file in fileInfo)
    8.         {
    9.             List<string> dropOptions = new List<string>(fileInfo.Length);
    10.             dropOptions.Add(file.Name);
    11.  
    12.             if (file.Extension == ".txt")
    13.             {
    14.                 if(dropdownLoad.options.Count > 0)
    15.                 dropdownLoad.ClearOptions();
    16.                 dropdownLoad.AddOptions(dropOptions);
    17.                 dropdownLoad.RefreshShownValue();
    18.             }
    19.  
    20.         }
    21.     }

    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 :

    Code (CSharp):
    1.  FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources\'\\'Maps", SearchOption.TopDirectoryOnly);

    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.

    Thanks for your answer.
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    Simply use "\\"
    Code (CSharp):
    1. FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources\\Maps", SearchOption.TopDirectoryOnly);
    2.  
     
  3. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    117
    Already tried it. And it doesn't work :/

    When the editor prints the error, the path stops at the Resources folder and don't go further into the hierarchy.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
  5. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    117
    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 :

    Code (CSharp):
    1. List<TextAsset> fileInfo = Resources.LoadAll("Resources/Maps", typeof(TextAsset)) as TextAsset;
    2.         List<string> dropOptions = new List<string>(fileInfo.Length);
    3.  
    4.         foreach (FileInfo file in fileInfo)
    5.         {
    6.            dropOptions.Add(file.Name);
    7.         }
    8.         foreach (FileInfo file in fileInfo)
    9.         {
    10.             if (file.Extension == ".txt")
    11.             {
    12.                 if(dropdownLoad.options.Count > 0)
    13.                 dropdownLoad.ClearOptions();
    14.                 dropdownLoad.AddOptions(dropOptions);
    15.                 dropdownLoad.RefreshShownValue();
    16.             }
    17.  
    18.         }


    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.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    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.
     
  7. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    117
    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 :

    Code (CSharp):
    1.     private void Add_Options_To_Dropdown()
    2.     {
    3.  
    4.         //DirectoryInfo levelDirectoryPath = new DirectoryInfo(Application.dataPath);
    5.         //FileInfo[] fileInfo = levelDirectoryPath.GetFiles("Resources");
    6.         Object[] fileInfo = Resources.LoadAll("Resources/Maps", typeof(TextAsset));
    7.         print(fileInfo.Length);
    8.         List<string> dropOptions = new List<string>(fileInfo.Length);
    9.  
    10.         foreach (TextAsset file in fileInfo)
    11.         {
    12.            dropOptions.Add(file.name);
    13.         }
    14.         foreach (TextAsset file in fileInfo)
    15.         {
    16.             if (file.name == "*.txt")
    17.             {
    18.                 if (dropdownLoad.options.Count > 0)
    19.                 {
    20.                     dropdownLoad.ClearOptions();
    21.                 }
    22.  
    23.                 dropdownLoad.AddOptions(dropOptions);
    24.                 dropdownLoad.RefreshShownValue();
    25.             }
    26.  
    27.         }
    28.     }
    29.  

    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.
     
    Last edited: Feb 10, 2018
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    JoeStrout and Kiwasi like this.
  9. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    117


    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.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    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. :)
     
    Quasar47 likes this.