Missing special characters after load a Text Asset

I have a C# code that reads a text file (.txt) file via TextAsset and I’m having trouble to maintain special characters. The word Leão (lion in portuguese) turns to Leo, for example.

Here’s the basic code:

public TextAsset references; //here I load the txt file, something like a csv
string[] valores;

valores = references.text.Split(' ; ');

All code are working fine. The problem is just these especial characters. Heres some other examples:

Búfalo = Bfalo
Cão = Co
etc…

Any tips? I’ve already tried saving the scripts in UTF-8…

I’ve tried several things and still nothing. Heres’s what I tried:

  • Saving the txt in UTF-8 Unicode
  • Changing the font (from Arial to Arial Unicode)
  • Typing the text directly in the script (also saved in UTF-8

The strange thing is as I typed directly on the script, the results chages. Ex.:

In txt:
Pássaro becomes Pssaro ( the “á” is lost)

In the script:
Pássaro becomes P??ssaro (the "á"changes to ??)

The last thing I’ve noticed is if I type the same words in the inspector its looks fine.

Thanks

Fixed that! I’d to save 2 scripts that uses parts of the text asset in UTF-16 and open the asset in MonoDevelop (mac) to fix the words with special chars!

Desculpa reviver o tópico mas estou tendo o mesmo problema no Windows. Como faço para resolver?

Just hit this issue myself, and I’ve found a reasonable fix. Normally, TextAssets will strip non-ASCII characters (as specified here), unless you save your asset with a .bytes extension.

So, you can change/copy your TextAsset file to have a .bytes extension (e.g. blah.txt becomes blah.bytes). Then, if you import the byte array and convert back to a string with the correct encoding, special characters appear just fine.

// You might also get this file from an AssetDatabase or elsewhere.
TextAsset byteAsset = Resources.Load<TextAsset>("resources/path/to/my/bytes/file");
// There are other options for Encoding.*.GetString, in case Default fails.
string contents = System.Text.Encoding.Default.GetString(byteAsset.bytes);

Note that this avoids using System.IO file operations, which don’t play nicely with builds since Resources won’t be accessible through normal file I/O.

(I’m still using Unity 5.5.4 so it’s possible that there’s some newer solution.)

I tried your solution but still not see the special characters…

2 years and some months later, I also encounter this problem with no solution to be seen

This is what the original text looks like

versus in the editor
9241305--1291593--upload_2023-8-22_16-5-38.png

Actually it is 11 years later. Please don’t necro-post. Start a new thread as per forum rules.

Make your thread meaningful. For instance, since you can see the characters (those goofy inverse tick marks), do a check YOURSELF and say “I checked my font and it is capable of displaying these characters,” because then we know you actually are bringing something to the table.

So please, start your own fresh post, and when you post, post complete troubleshooting steps. Here’s how:

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • ONLY post the relevant code, and then refer to it in your discussion.