2

I'm using Unity 2019 and I made a script that load a texture from the pc with "UnityWebRequest".

using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(finalPath))
    {
        yield return uwr.SendWebRequest();

        if (uwr.isNetworkError || uwr.isHttpError)
        {
            Debug.Log(uwr.error);
        }
        else
        {
            // Get downloaded asset bundle
            horizImg.gameObject.SetActive(true);
            horizImg.texture =  DownloadHandlerTexture.GetContent(uwr);
        }
    }

If finalpath has a space or a "+" in the name it works well, but if "finalPath" has both ("+" and a space") it returns this error:

HTTP/1.1 404 Not Found

G. Threepwood
  • 454
  • 8
  • 23
  • 1
    it can not have a space .. that's probably the main issue. You could try using `System.Web.HttpUtility.UrlEncode(finalPath)` though I remember that also `UnityWebRequest` should do some url encoding but not sure .. Could you show how exactly you build your path? – derHugo Dec 13 '19 at 15:20
  • Also have a look at [What's valid and what's not in a URI query?](https://stackoverflow.com/questions/2366260/whats-valid-and-whats-not-in-a-uri-query) – derHugo Dec 13 '19 at 15:23
  • Thank you for the answer! I understand but if I use it for loading a local file it could be that there is a file with a "reserved character" in the name. Is there a way to escape that characters? my path (for a local file) is built like this: finalPath = "file://" + Application.streamingAssetsPath + "/" + name; – G. Threepwood Dec 13 '19 at 15:40
  • 1
    never use string concatenation (`+ "/"`). Try using `Path.Combine(Application.streamingAssetsPath, name)` instead. Also not sure but if this is Android you might need a prefix like `jar:file//` – derHugo Dec 13 '19 at 15:46
  • I tried using path.combine but it's the same. For example "a+.jpg" and "a a.jpg" both work but if I use as a filename "a a+.jpg" it doesn't work. Also I can't use "System.Web.HttpUtility.UrlEncode()" even if I put using System.Web on top. It says "The type name 'HttpUtility' could not be found in the namespace 'System.Web'" – G. Threepwood Dec 13 '19 at 16:18

0 Answers0