string url = “http://1drv.ms/1bCdLy1 ”;
www = new WWW(url);
StartCoroutine(WaitForWWW());
portraitSprite.sprite = (Sprite)www.texture;
private IEnumerator WaitForWWW() {
yield return www;
}
versus
Cannot convert type ‘UnityEngine.Texture2D’ to 'UnityEngine.Sprite
Any ideas? new to Unity…
Eric5h5
February 16, 2014, 9:12pm
2
A sprite isn’t a texture. You can create a sprite from a texture; see the docs for Sprite , specifically the Create function.
Cenda
May 30, 2015, 11:34am
3
I just solved exactly same problem with Sprite.Create. It is working in editor, but not on device. It looks that device can not use Sprite.Create, so I have to use texture and RawImage.
IEnumerator Start()
{
WWW www = new WWW(url);
yield return (www);
gameObject.GetComponent().overrideSprite = Sprite.Create(www.texture, new Rect(0, 0, 900, 360), new Vector2(0.5f, 0.5f));
}
Hi, this can help
// reading texture from images in system local
Blockquote
public static Texture2D LoadPNG(string filePath)
{
Texture2D tex = null;
byte fileData;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
return tex;
}
Blockquote
then save the texture from LoadPNG function
Blockquote
mytexture = LoadPNG(thumbnailpath);
obj.GetComponentInChildren().sprite = Sprite.Create(mytexture, new Rect(0.0f, 0.0f, mytexture.width, mytexture.height), new Vector2(0.5f, 0.5f), 100.0f);
Blockquote