Reputation:
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);
}
Upvotes: 1
Views: 753
Reputation:
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".
Upvotes: 0
Reputation: 90683
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);
}
}
}
Upvotes: 3