先上最开始我犯错的版本
IEnumerator DownSprites(string url, Sprite sp)
{
using (UnityWebRequest request = new UnityWebRequest(url))
{
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
request.downloadHandler = texDl;
yield return request.SendWebRequest();
if (request.isNetworkError)
{
Debug.LogError(request.error);
}
else
{
Texture2D tex = texDl.texture;
tex.width = 100;
tex.height = 100;
Sprite sprite=Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width,tex.height), new Vector2(0.5f, 0.5f));
}
}
}
上面这个是我直接把接收的图片的sprite传给需要改变图片的sprite,这样做运行的时候图片会没有任何变化。因为sprite是一个组件下面的变量,只有直接传递需要改变的组件,然后给该组件赋值才能传递成功。如下:
IEnumerator DownSprite(string url, RawImage rawImage)
{
using (UnityWebRequest request = new UnityWebRequest(url))
{
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
request.downloadHandler = texDl;
yield return request.SendWebRequest();
if (request.isNetworkError)
{
Debug.LogError(request.error);
}
else
{
rawImage.texture = texDl.texture;
}
}
}
|