用WWW加载网络的jpg png等图片是可以正常显示的;但是如果加载的是gif格式的图片就会出错。 PC端下,可以直接调用System.Drawing.这个就不用多说了,网上直接有代码。
如果在Android,是不支持引入System.Drawing的,这样会直接报错。解决办法是用在Android下写gif转png的方法,然后对unity加载的图片进行格式判断,如果是gif的,再用unity调用Android方法,进行图片的转化。
碰巧遇到一样的问题,现在的解决方案是,如果判断是gif图片,则先保存成文件,再按资源加载。
IEnumerator downloadRoutine()
{
WWW www = new WWW(url);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
string contentType = www.responseHeaders["CONTENT-TYPE"];
var contents = contentType.Split('/');
if (contents[0] == "image")
{
if (contents[1].ToLower() == "png" || contents[1].ToLower() == "jpeg")
{
Texture2D t = www.texture;
Sprite s = null;
if(t!=null)
s = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
if(s!=null)
image.sprite = s;
}
else
{
//写到文件中
string fileName = ApolloInterface.Instance.account.OpenId.ToString();
string dict = Application.persistentDataPath + "/Resources/";
if (!Directory.Exists(dict))
{
Directory.CreateDirectory(dict);
}
string filePath = dict + fileName + "." + contents[1];
File.WriteAllBytes(filePath, www.bytes);
//从文件加载
Texture2D t = Resources.Load(fileName);
Sprite s = null;
if(t != null)
s = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(0.5f, 0.5f));
if(s != null)
image.sprite = s;
}
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。