1

I want to make an image existing in Streaming Assets a texture for a 3D object. However, the texture of _Material becomes a red "Interrogation mark".

Interrogation mark This is incorrect.

How can I get the correct image?

Material _Material;
IEnumerator LoadPlayerTexture()
{
    string url = Path.Combine(Application.streamingAssetsPath, "front.png");

#if UNITY_EDITOR
    url = "file://" + url;
#endif

    byte[] imgData;
    Texture2D tex = new Texture2D(2, 2);

    //Check if we should use UnityWebRequest or File.ReadAllBytes
    if (url.Contains("://") || url.Contains(":///"))
    {
        UnityWebRequest www = UnityWebRequest.Get(url);
        yield return www.SendWebRequest();
        imgData = www.downloadHandler.data;
    }
    else
    {
        imgData = File.ReadAllBytes(url);
    }

    //Load raw Data into Texture2D 
    tex.LoadImage(imgData);

    _Material.SetTexture("_MainTex", tex);
}
5
  • Why don't you set texture directly? I think converting to sprite is unnecessary.
    – Raguel
    Apr 17, 2020 at 7:46
  • I got this code on a website. Certainly, Sprite conversion is not necessary. I erased it. thanks
    – user13337231
    Apr 17, 2020 at 7:57
  • also I don't really understand what is _Material.Front. if your material is type of Material then you should set texture without this Front. Should be something like _Material.SetTexture("_MainTex", tex)
    – Raguel
    Apr 17, 2020 at 8:24
  • sorry. Originally _Material is a struct and due to my editing mistake I wrote it as if it were Materital. Correct the question code.
    – user13337231
    Apr 17, 2020 at 8:35
  • Corrected* correctX
    – user13337231
    Apr 17, 2020 at 8:51

2 Answers 2

3

Why don't you use UnityWebRequestTexture.GetTexture instead?

Note that UnityWebRequest can also be used for local files and is even the recommended way.

Little limit (that doesn't affect your use case)

Note: Only JPG and PNG formats are supported.

Material _Material;
IEnumerator LoadPlayerTexture()
{
    var url = Path.Combine(Application.streamingAssetsPath, "front.png");

    // UnityWebRequest can also be used for reading local files
    // also from streaming assets
    using(var uwr = UnityWebRequestTexture.GetTexture(url))
    {
        yield return uwr.SendWebRequest();

        if (uwr.isNetworkError || uwr.isHttpError)
        {
            Debug.Log(uwr.error);
        }
        else
        {
            // Get downloaded texture
            var texture = DownloadHandlerTexture.GetContent(uwr);
            _Material.SetTexture("_MainTex", texture);
        }
    }
}
3
  • Thank you for your answer.but I've been wondering for a long time, why are you using the network to find files that exist locally? sorry. I am really new to reading and writing data.
    – user13337231
    Apr 17, 2020 at 13:18
  • It is not using the network actually .. it is the same as opening a local html file in your web browser .. it also works with local files without using any network actually. For Android it is even the only way to access anything from streamingAssets since on Android this folder is packet into the apppackage and has to be unpacked on runtime. You could try to again add the url = "file//:" + url;
    – derHugo
    Apr 17, 2020 at 13:22
  • Oh, I understood it's not using the network. But, if I try the code you taught me, the texture is still "?" be By the way I try these processes in Editor then build for Windows.
    – user13337231
    Apr 17, 2020 at 13:26
0

I created the texture with standard Windows software, "Paint". This was the problem. The problem was solved by exporting the texture created by "paint" with "GIMP".

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.