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.

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_, ekezetnelkul*);*_

}

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_, ekezetnelkul*);*_

}

return _exit;
}

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