原来有个WWW类的,不过显示已被弃用,最新用的是UnityWebRequest ,代码如下:
public void SetSprite(string url, Transform transform)
{
StartCoroutine(DownSprite(url, transform));
}
IEnumerator DownSprite(string url, Transform transform)
{
using (UnityWebRequest request = new UnityWebRequest(url))
{
//下载图像作为纹理使用
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
request.downloadHandler = texDl;
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.ConnectionError)
{
Debug.LogError(request.error);
}
else
{
Texture2D tex = new Texture2D(300, 300);
tex = texDl.texture;
Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
transform.GetComponent<Image>().sprite = sprite;
}
}
}