Search Unity

resources.Load() with special characters in the file name (iOS and Mac)

Discussion in 'Scripting' started by ChristofS, Dec 9, 2015.

  1. ChristofS

    ChristofS

    Joined:
    Jan 22, 2013
    Posts:
    9
    On Windows: Unity 5.2.2 , on Mac Unity 5.2.3

    I try to use resources.Load() with special characters in the file name i.e. "Set03-Löwe-02-L1".
    Works perfectly on Windows and Android. File is not found on Mac and iOs.
    I only use simple file names, no complete paths.

    This happens when I hardcode the file name in the C# and when I load the filename from an UTF8-coded TextAsset. File names without special characters are loaded perfectly. I tried different encodings for the file name (i.e. UTF16) without success.

    Do you have any idea?

    I need to use the special characters, because the names are displayed in the UI.
     
    Last edited: Dec 9, 2015
    hitarthdoc1994 likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you're unable to find a proper solution, you could work around it by writing a special-to-nonspecial "translator" and using the non-special-character names as the filenames. If needed, you can simultaneously build a dictionary (with the special as the value and the non-special as the key) so that you can use the nonspecial name to look up the special one.

    FWIW, situations like this are one of a number of good reasons to separate text that's displayed in the UI (e.g. "name") from text you use internally (e.g. "ID").
     
    hitarthdoc1994 likes this.
  3. ChristofS

    ChristofS

    Joined:
    Jan 22, 2013
    Posts:
    9
    @StarManta:Thanks for your reply and for your suggested workarounds. I use the file name to define all meta data that I need for this sprite resources. So there is only one place to update when something changes, which is less error-prone. This is a pool of resources, which changed quite often, while the project evolved.
    Yes, I will build a character translator, when there is no other solution, but I hope this is not necessary.


    Does anybody know, whether Unity is basically able to handle special characters in resource file names? I don't see a reason why this works in Windows and Android and not in iOS. The resource file names are handled internally in unity so this should not depend on a operating system.
     
    hitarthdoc1994 likes this.
  4. hitarthdoc1994

    hitarthdoc1994

    Joined:
    Dec 17, 2015
    Posts:
    15
    Thanks for the answer.

    I Tried it but to my hearts contempt and it still gives me a Null Reference Exception.

    My Code:

    ```
    public void GetFileName(string sentFileName){

    _XLetterWords = Resources.Load (sentFileName) as TextAsset;

    Debug.Log(_XLetterWords); // Returns Null in iOS but works in Windows and Android.
    //example file name: 3LetterWords.txt
    .
    .
    .
    // Code to read File.
    }​
    ```

    Thanks for the help.
     
  5. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    As @StarManta mentioned, it's probably best to split out your filenames (implementation detail) from your GUI. But, Unix seems to have no issue itself with the filename you pasted - can you post the code snippet that you're using to load the text asset?

     
  6. hitarthdoc1994

    hitarthdoc1994

    Joined:
    Dec 17, 2015
    Posts:
    15
    I apologise for the inconvenience caused but I had also passed the file extension as the argument.
    When you remove it the Code works.

    Thanking you again
     
  7. DevMan5000

    DevMan5000

    Joined:
    Jun 13, 2014
    Posts:
    14
    Hello people of the internet,

    I struggled with this for two straight days, it has been maddening, but I found the answer and I wish to share it with you all in hopes of saving others from this madness.

    /// NOTE: This next line is the savior to what has been the bane of my existance for the past couple days.
    /// Mac uses whats called the decomposed format for accented characters when dealing with filenames.
    /// This means that "sofá " (copied directly from filename in Finder) is different than "sofá"
    /// (copied from my Localization.csv). The difference is the one coped from the filename is "decomposed" and
    /// uses two characters to express the accent while the other just uses one character.
    /// If you use TextWrangler to view the pasted word, then the difference is clear. The one from the filename looks
    /// like "sofa`" with the accent in the space AFTER the a, not directly above it.
    /// This link talks about the composed / decomposed issue and its where I grabbed the string.Normalize() example from.
    /// http://stackoverflow.com/questions/...ilename-character-is-considered-international
    ///
    /// NOTE: Its important NOT to normalize to the decomposed form on Windows/Android since those file systems do NOT used decomposed
    /// filenames. That is why we do the platform checking here.
    ////////////////////////////////////
    #if UNITY_IOS || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
    filePath = filePath.Normalize(NormalizationForm.FormD);
    #endif

    Hope this helps someone!
     
    Vetaut, SkippyV, Shizola and 3 others like this.
  8. kapaakea2

    kapaakea2

    Joined:
    Sep 4, 2013
    Posts:
    7

    THANK YOU!!!! IT WORKS!!! (AND IT DID HELP SOMEONE)
     
    Shizola likes this.
  9. Shizola

    Shizola

    Joined:
    Jun 29, 2014
    Posts:
    476
    NovoTempo likes this.
  10. RaffaeleT

    RaffaeleT

    Joined:
    May 12, 2017
    Posts:
    2
    Thank you DevMan5000 for sharing this information!!!
     
  11. SkippyV

    SkippyV

    Joined:
    Dec 13, 2012
    Posts:
    34
    This saved me a ton of time. Many thanks ... hopefully reigniting the thread saves some others as well.