- Home /
Solved - Handling special characters (áéőúűöüó) in Unity
Hello Unity Community,
PLease help me out with the following: I need to load textures from hard drive, but I am struggling with special characters in path (áéőúűöüó). In case there are none of these characters in path, the below code works fine:
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT5, false);
var www = new WWW("file://"+pathToFile);
yield www;
www.LoadImageIntoTexture(renderer.material.mainTexture);
renderer.material.shader = Shader.Find("Mobile/Unlit (Supports Lightmap)");
But fails in case there is a special character. I tried WWW.EscapeURL it like this:
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT5, false);
var URLFriendlyPath = WWW.EscapeURL("file://"+pathToFile);
var www = new WWW(URLFriendlyPath);
yield www;
www.LoadImageIntoTexture(renderer.material.mainTexture);
renderer.material.shader = Shader.Find("Mobile/Unlit (Supports Lightmap)");
In this case nothing works properly.
What I would like to have is a bulletproof solution, that no matter if a Hungarian, French, German user (having such special characters in their language) chooses the path, none will face such problem. How can it be done. Thanks for any hints in advance.
I am not sure if this will help, but give it a try.
You have to import these packages first
import System.Text;
import System.Text.RegularExpressions;
and then convert the pathToFile string into utf8 encoding, and store it in another like this, now use the new path string.
var path : String = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(pathToFile));
Answer by srancsik · Oct 02, 2013 at 01:02 PM
My thanks goes to Graham Dunnett, calling my attention to the fact that URL should be ASCII.
if (pathToFile.Length != 0) {
var ekezetes = ['Ž','ž','Ÿ','¡','¢','£','¤','¥','¦','§','¨','©','ª','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','ù','ú','û','ü','ý','þ','ÿ'
];
var ekezetnelkul = ['%8E','%9E','%9F','%A1','%A2','%A3','%A4','%A5','%A6','%A7','%A8','%A9','%AA','%C0','%C1','%C2','%C3','%C4','%C5','%C6','%C7','%C8','%C9','%CA','%CB','%CC','%CD','%CE','%CF','%D0','%D1','%D2','%D3','%D4','%D5','%D6','%D9','%DA','%DB','%DC','%DD','%DE','%DF','%E0','%E1','%E2','%E3','%E4','%E5','%E6','%E7','%E8','%E9','%EA','%EB','%EC','%ED','%EE','%EF','%F0','%F1','%F2','%F3','%F4','%F5','%F6','%F9','%FA','%FB','%FC','%FD','%FE','%FF'
];
for(var i=0;i<ekezetes.length;i++)
{
pathToFile = pathToFile.Replace(ekezetes[i], ekezetnelkul[i]);
}
Answer by EdwardLM · Jan 23, 2014 at 12:00 PM
Thank you very much! With a few little changes I could make this work in C#. It is also useful when trying to use WWW class from file paths with special characters.
This is the code I used. Thanks again!
string SafeFilePath(string _enter)
{
string _exit = _enter;
string[] ekezetes = new string[] { "Ž", "ž", "Ÿ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "ù", "ú", "û", "ü", "ý", "þ", "ÿ" };
string[] ekezetnelkul = new string[] { "%8E", "%9E", "%9F", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7", "%A8", "%A9", "%AA", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" };
for (var i = 0; i < ekezetes.Length; i++)
{
_exit = _exit.Replace(ekezetes[i], ekezetnelkul[i]);
}
return _exit;
}
Thank you very much to both for the tip.
I was trying to use WWW class to access file paths with special characters (spaces or accents) in iOS/Android environments, without success. Worked in Standalone environments or if the path contains only letters or numbers.
The correspondence is from w3schools: HT$$anonymous$$L URL Encoding Reference and I need to select the UTF-8 charset. I'am using Unity 5.6.4f1 and this is the code (with my specific special chars):
public string convertPathTo$$anonymous$$obiles(string url2mobiles)
{
string[] symbol = new string[] {" ","À","Á","Â","Ã","Ç","È","É","Ê","Ì","Í","Î","Ñ","Ò","Ó","Ô","Õ","Ù","Ú","Û","à","á","â","ã","ç","è","é","ê","ì","í","î","ñ","ò","ó","ô","õ","ù","ú","û" };
string[] symbolHT$$anonymous$$L = new string[] {"%20","%C3%80","%C3%81","%C3%82","%C3%83","%C3%87","%C3%88","%C3%89","%C3%8A","%C3%8C","%C3%8D","%C3%8E","%C3%91","%C3%92","%C3%93","%C3%94","%C3%95","%C3%99","%C3%9A","%C3%9B","%C3%A0","%C3%A1","%C3%A2","%C3%A3","%C3%A7","%C3%A8","%C3%A9","%C3%AA","%C3%AC","%C3%AD","%C3%AE","%C3%B1","%C3%B2","%C3%B3","%C3%B4","%C3%B5","%C3%B9","%C3%BA","%C3%BB" };
for (var i = 0; i < symbol.Length; i++) {
url2mobiles = url2mobiles.Replace (symbol [i], symbolHT$$anonymous$$L [i]);
}
return url2mobiles;
}
Answer by fibriZo raZiel · Sep 19, 2015 at 03:32 AM
This worked out:
string path = "c:/a veces veo muertos/ñ qué ñ.png";
string escapedPath = "file:///" + WWW.EscapeURL(path, Encoding.Default).Replace("+", "%20");
var www = new WWW(escapedPath);
// Use www here...
Your answer
Follow this Question
Related Questions
My apk not working properly in runtime 0 Answers
Is it possible to load a script from a url? 1 Answer
Load texture on disabled GameObject 1 Answer