220 lines
7.2 KiB
C#
220 lines
7.2 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Collections;
|
||
using UnityEngine.Networking;
|
||
using System;
|
||
|
||
public class WebImageLoader : MonoBehaviour
|
||
{
|
||
// 用于显示图片的UI RawImage组件
|
||
public RawImage imageDisplay;
|
||
|
||
// 成功加载回调
|
||
public Action<Texture2D> OnLoadSuccess;
|
||
|
||
// 加载失败回调
|
||
public Action<string> OnLoadFailed;
|
||
|
||
// 进度更新回调
|
||
public Action<float> OnProgressChanged;
|
||
|
||
void Start()
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步加载网络图片
|
||
/// </summary>
|
||
/// <param name="url">图片URL地址</param>
|
||
public void LoadImageFromUrl(string url, RawImage rawImage, Action action = null)
|
||
{
|
||
StartCoroutine(LoadImageCoroutine(url, rawImage, action));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步加载网络图片,带超时设置
|
||
/// </summary>
|
||
/// <param name="url">图片URL地址</param>
|
||
/// <param name="timeoutSeconds">超时时间(秒)</param>
|
||
public void LoadImageFromUrl(string url, int timeoutSeconds)
|
||
{
|
||
StartCoroutine(LoadImageWithTimeoutCoroutine(url, timeoutSeconds));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 协程方式加载图片
|
||
/// </summary>
|
||
/// <param name="url">图片URL地址</param>
|
||
private IEnumerator LoadImageCoroutine(string url, RawImage _imageDisplay, Action action = null)
|
||
{
|
||
if (string.IsNullOrEmpty(url))
|
||
{
|
||
Debug.LogError("图片URL不能为空");
|
||
OnLoadFailed?.Invoke("图片URL不能为空");
|
||
yield break;
|
||
}
|
||
Debug.Log("url:" + url);
|
||
|
||
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
|
||
{
|
||
// 添加请求头,模拟浏览器访问
|
||
request.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
||
|
||
var operation = request.SendWebRequest();
|
||
|
||
// 监听下载进度
|
||
while (!operation.isDone)
|
||
{
|
||
float progress = operation.progress;
|
||
OnProgressChanged?.Invoke(progress);
|
||
yield return null;
|
||
}
|
||
|
||
OnProgressChanged?.Invoke(1f); // 下载完成
|
||
|
||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||
request.result == UnityWebRequest.Result.ProtocolError)
|
||
{
|
||
Debug.LogError($"加载图片失败: {request.error}");
|
||
OnLoadFailed?.Invoke(request.error);
|
||
StopCoroutine(LoadImageCoroutine(url, _imageDisplay, action));
|
||
StartCoroutine(LoadImageCoroutine(url, _imageDisplay, action));
|
||
}
|
||
else
|
||
{
|
||
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
|
||
|
||
if (texture != null)
|
||
{
|
||
if (_imageDisplay != null)
|
||
{
|
||
_imageDisplay.texture = texture;
|
||
}
|
||
|
||
OnLoadSuccess?.Invoke(texture);
|
||
_imageDisplay.gameObject.SetActive(true);
|
||
action?.Invoke();
|
||
Debug.Log("图片加载成功");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("获取到的纹理为空");
|
||
OnLoadFailed?.Invoke("获取到的纹理为空");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 带超时控制的协程加载
|
||
/// </summary>
|
||
/// <param name="url">图片URL地址</param>
|
||
/// <param name="timeoutSeconds">超时时间(秒)</param>
|
||
private IEnumerator LoadImageWithTimeoutCoroutine(string url, int timeoutSeconds)
|
||
{
|
||
if (string.IsNullOrEmpty(url))
|
||
{
|
||
Debug.LogError("图片URL不能为空");
|
||
OnLoadFailed?.Invoke("图片URL不能为空");
|
||
yield break;
|
||
}
|
||
|
||
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
|
||
{
|
||
// 设置超时
|
||
request.timeout = timeoutSeconds;
|
||
|
||
// 添加请求头
|
||
request.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
||
|
||
var operation = request.SendWebRequest();
|
||
|
||
float startTime = Time.time;
|
||
while (!operation.isDone)
|
||
{
|
||
if (Time.time - startTime > timeoutSeconds)
|
||
{
|
||
Debug.LogError($"加载超时 ({timeoutSeconds}秒)");
|
||
OnLoadFailed?.Invoke($"加载超时 ({timeoutSeconds}秒)");
|
||
yield break;
|
||
}
|
||
|
||
float progress = operation.progress;
|
||
OnProgressChanged?.Invoke(progress);
|
||
yield return null;
|
||
}
|
||
|
||
OnProgressChanged?.Invoke(1f); // 下载完成
|
||
|
||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||
request.result == UnityWebRequest.Result.ProtocolError)
|
||
{
|
||
Debug.LogError($"加载图片失败: {request.error}");
|
||
OnLoadFailed?.Invoke(request.error);
|
||
}
|
||
else
|
||
{
|
||
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
|
||
|
||
if (texture != null)
|
||
{
|
||
if (imageDisplay != null)
|
||
{
|
||
imageDisplay.texture = texture;
|
||
}
|
||
|
||
OnLoadSuccess?.Invoke(texture);
|
||
Debug.Log("图片加载成功");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("获取到的纹理为空");
|
||
OnLoadFailed?.Invoke("获取到的纹理为空");
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 直接加载图片并返回Texture2D(静态方法)
|
||
/// </summary>
|
||
/// <param name="url">图片URL地址</param>
|
||
/// <param name="callback">回调函数,参数为Texture2D和错误信息</param>
|
||
public static IEnumerator LoadTextureFromUrl(string url, Action<Texture2D, string> callback)
|
||
{
|
||
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
|
||
{
|
||
// 添加请求头
|
||
request.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
|
||
|
||
yield return request.SendWebRequest();
|
||
|
||
if (request.result == UnityWebRequest.Result.ConnectionError ||
|
||
request.result == UnityWebRequest.Result.ProtocolError)
|
||
{
|
||
callback(null, request.error);
|
||
}
|
||
else
|
||
{
|
||
Texture2D texture = ((DownloadHandlerTexture)request.downloadHandler).texture;
|
||
callback(texture, null);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将Texture2D转换为Sprite
|
||
/// </summary>
|
||
/// <param name="texture">输入纹理</param>
|
||
/// <returns>生成的Sprite</returns>
|
||
public static Sprite TextureToSprite(Texture2D texture)
|
||
{
|
||
if (texture == null) return null;
|
||
|
||
Rect rect = new Rect(0, 0, texture.width, texture.height);
|
||
Vector2 pivot = new Vector2(0.5f, 0.5f); // 中心点
|
||
|
||
return Sprite.Create(texture, rect, pivot);
|
||
}
|
||
}
|