
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ImageDownloader : MonoBehaviour
{
public string imageUrl = "http://192.168.1.360:8083/profile/1702881948459.png";
// Start is called before the first frame update
void Start()
{
StartCoroutine(DownloadImage());
}
IEnumerator DownloadImage()
{
UnityWebRequest request = UnityWebRequestTexture.GetTexture(imageUrl);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.LogError("下载图片失败: " + request.error);
}
else
{
Texture2D texture = DownloadHandlerTexture.GetContent(request);
if (texture != null)
{
// 操作下载得到的Texture2D
// 例如,将其赋给一个RawImage组件的texture属性
RawImage rawImage = GetComponent<RawImage>();
rawImage.texture = texture;
}
else
{
Debug.LogError("下载的内容不是图片!");
}
}
}
}