Search code examples
character-encodingunityscriptunity-game-engineextended-ascii

How to load TextAsset from txt file containing extended ASCII characters in Unity?


In my case specifically, I have bullet-points (• or #149) in my text file.
If I copy paste "•" into my Unity text field in the editor, it shows up, so I am pretty sure the bullet-point is lost in the reading process. (I checked in debug mode, and indeed the bullet-point is lost at reading).

This is how I read in my text file as a TextAsset:

TextAsset content = Resources.Load(SlideManager.slideLanguage+"\\"+fileName+" ("+SlideManager.slideNumber+")") as TextAsset; 

Solution

  • It turns out, that the way I read is completely fine. It reads the file correctly, but the encoding of the file is ASCII, therefore the resource loader cannot interpret none ASCII characters, and drops them.

    Thus, since the bullet-point is not standard ASCII, but extended ASCII character, you have to specify the encoding of your text files.
    For example, set encoding to UTF-8, and then it will work.

    I used notepad++ to set encoding, but I am sure there are many other ways you can do it.

    To set encoding in Notepad++

    • Click on the tab named Encoding (fifth tab from the left on the top by default), and select Convert to UTF-8.