修改网络模块
增加下载音频, 增加下载纹理的调试功能
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Get下载 Texture
|
||||
/// </summary>
|
||||
public class DataRequestTexture : DataRequest {
|
||||
public readonly string url;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<Texture2D> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override WebRequestType RequestType => WebRequestType.Texture;
|
||||
|
||||
/// <summary> Web Get请求 Texture </summary>
|
||||
public DataRequestTexture(string url, Action<Texture2D> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
DownloadHandlerTexture dht = downloadHandler as DownloadHandlerTexture;
|
||||
Texture2D texture = dht.texture;
|
||||
bool compress = (texture.width % 2) == 0 && (texture.height % 2) == 0;
|
||||
if (compress) { texture.Compress(true); }
|
||||
else { Debug.LogWarning($"无法压缩的图片:{web.url}"); }
|
||||
OnCallback?.Invoke(texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c75f34b2e714238459d0e95f16c7a7e1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+6
-1
@@ -11,7 +11,7 @@ namespace MuHua {
|
||||
/// <summary> Web请求地址 </summary>
|
||||
public abstract string Url { get; }
|
||||
/// <summary> Web请求类型 </summary>
|
||||
public abstract WebRequestType RequestType { get; }
|
||||
public abstract EnumNetworkRequestType RequestType { get; }
|
||||
/// <summary> 提交json数据 </summary>
|
||||
public virtual string Json { get; }
|
||||
/// <summary> 提交Form表单数据 </summary>
|
||||
@@ -19,5 +19,10 @@ namespace MuHua {
|
||||
|
||||
/// <summary> Web请求结果处理 </summary>
|
||||
public abstract void RequestResultHandle(bool isDone, UnityWebRequest web);
|
||||
|
||||
/// <summary> 发送请求 </summary>
|
||||
public virtual IEnumerator Send() => NetworkRequest.Execute(this);
|
||||
/// <summary> 发送请求(异步) </summary>
|
||||
public virtual void SendAsync() => NetworkRequestAsync.Execute(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
public class DataRequestAudio : DataRequest {
|
||||
public readonly string url;
|
||||
public Action<string> OnError; // 错误回调
|
||||
public Action<AudioClip> OnCallback; // 成功回调
|
||||
|
||||
public override string Url => url;
|
||||
public override EnumNetworkRequestType RequestType => EnumNetworkRequestType.Audio;
|
||||
|
||||
public DataRequestAudio(string url, Action<AudioClip> OnCallback = null, Action<string> OnError = null) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
this.OnError = OnError;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
if (!isDone) {
|
||||
OnError?.Invoke($"Audio download failed: {web.error}");
|
||||
return;
|
||||
}
|
||||
// 获取音频数据
|
||||
DownloadHandlerAudioClip downloadHandler = web.downloadHandler as DownloadHandlerAudioClip;
|
||||
if (downloadHandler == null || downloadHandler.audioClip == null) {
|
||||
OnError?.Invoke("Failed to load audio clip.");
|
||||
return;
|
||||
}
|
||||
// 获取音频长度并打印
|
||||
AudioClip audioClip = downloadHandler.audioClip;
|
||||
Debug.Log($"Audio downloaded successfully: {url}, Length: {audioClip.length} seconds");
|
||||
// 回调返回音频数据
|
||||
OnCallback?.Invoke(audioClip);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 383ac0bd35c136246aa13a5cbb60b0b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+3
-1
@@ -11,11 +11,12 @@ namespace MuHua {
|
||||
public class DataRequestGet : DataRequest {
|
||||
public readonly string url;
|
||||
|
||||
public string result;
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override WebRequestType RequestType => WebRequestType.GET;
|
||||
public override EnumNetworkRequestType RequestType => EnumNetworkRequestType.GET;
|
||||
|
||||
/// <summary> Web Get请求数据 </summary>
|
||||
public DataRequestGet(string url, Action<string> OnCallback = null) {
|
||||
@@ -25,6 +26,7 @@ namespace MuHua {
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
result = downloadHandler.text;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
OnCallback?.Invoke(downloadHandler.text);
|
||||
}
|
||||
+6
-4
@@ -12,13 +12,14 @@ namespace MuHua {
|
||||
public readonly string url;
|
||||
public readonly string json;
|
||||
public readonly WWWForm form;
|
||||
public readonly WebRequestType type;
|
||||
public readonly EnumNetworkRequestType type;
|
||||
|
||||
public string result;
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override WebRequestType RequestType => type;
|
||||
public override EnumNetworkRequestType RequestType => type;
|
||||
public override string Json => json;
|
||||
public override WWWForm Form => form;
|
||||
|
||||
@@ -27,18 +28,19 @@ namespace MuHua {
|
||||
this.url = url;
|
||||
this.json = json;
|
||||
this.OnCallback = OnCallback;
|
||||
type = WebRequestType.PostJson;
|
||||
type = EnumNetworkRequestType.PostJson;
|
||||
}
|
||||
/// <summary> Web Post请求 提交WWWForm数据 </summary>
|
||||
public DataRequestPost(string url, WWWForm form, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.form = form;
|
||||
this.OnCallback = OnCallback;
|
||||
type = WebRequestType.PostForm;
|
||||
type = EnumNetworkRequestType.PostForm;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
DownloadHandler downloadHandler = web.downloadHandler;
|
||||
result = downloadHandler.text;
|
||||
if (!isDone) { OnError?.Invoke(downloadHandler.text); return; }
|
||||
OnCallback?.Invoke(downloadHandler.text);
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Get下载 Texture
|
||||
/// </summary>
|
||||
public class DataRequestTexture : DataRequest {
|
||||
public readonly string url;
|
||||
|
||||
public Action<string> OnError;
|
||||
public Action<Texture2D> OnCallback;
|
||||
public bool isCompress;
|
||||
|
||||
public override string Url => url;
|
||||
public override EnumNetworkRequestType RequestType => EnumNetworkRequestType.Texture;
|
||||
|
||||
/// <summary> Web Get请求 Texture </summary>
|
||||
public DataRequestTexture(string url, Action<Texture2D> OnCallback = null, bool isCompress = true) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
this.isCompress = isCompress;
|
||||
}
|
||||
|
||||
public override void RequestResultHandle(bool isDone, UnityWebRequest web) {
|
||||
// 检查请求是否完成且没有错误
|
||||
if (!isDone) {
|
||||
OnError?.Invoke($"Texture download failed: {web.error}");
|
||||
return;
|
||||
}
|
||||
// 检查下载处理程序是否为 DownloadHandlerTexture
|
||||
DownloadHandlerTexture downloadHandler = web.downloadHandler as DownloadHandlerTexture;
|
||||
if (downloadHandler == null || downloadHandler.texture == null) {
|
||||
OnError?.Invoke("Failed to load Texture.");
|
||||
return;
|
||||
}
|
||||
// 获取纹理,并进行压缩
|
||||
Texture2D texture = downloadHandler.texture;
|
||||
if (isCompress) {
|
||||
bool compress = (texture.width % 2) == 0 && (texture.height % 2) == 0;
|
||||
if (compress) { texture.Compress(true); }
|
||||
else { Debug.LogWarning($"Unable to compress image: {web.url}"); }
|
||||
}
|
||||
// 获取纹理大小并打印
|
||||
string textureSize = GetTextureMemorySize(texture);
|
||||
Debug.Log($"Texture size: {textureSize}");
|
||||
OnCallback?.Invoke(texture);
|
||||
}
|
||||
|
||||
public static string GetTextureMemorySize(Texture2D texture) {
|
||||
if (texture == null) return "0 B";
|
||||
|
||||
// 获取纹理的宽度、高度和格式
|
||||
int width = texture.width;
|
||||
int height = texture.height;
|
||||
TextureFormat format = texture.format;
|
||||
|
||||
// 每像素的字节数
|
||||
int bytesPerPixel = 0;
|
||||
switch (format) {
|
||||
case TextureFormat.Alpha8: bytesPerPixel = 1; break;
|
||||
case TextureFormat.RGB24: bytesPerPixel = 3; break;
|
||||
case TextureFormat.RGBA32: bytesPerPixel = 4; break;
|
||||
case TextureFormat.ARGB32: bytesPerPixel = 4; break;
|
||||
case TextureFormat.RGBAHalf: bytesPerPixel = 8; break;
|
||||
case TextureFormat.RGBAFloat: bytesPerPixel = 16; break;
|
||||
default:
|
||||
Debug.LogWarning($"Unsupported texture format: {format}");
|
||||
return "Unknown Size";
|
||||
}
|
||||
|
||||
// 计算纹理占用的内存大小(字节)
|
||||
long memorySizeBytes = width * height * bytesPerPixel;
|
||||
|
||||
// 转换为 KB 或 MB
|
||||
if (memorySizeBytes < 1024) {
|
||||
return $"{memorySizeBytes} B";
|
||||
}
|
||||
else if (memorySizeBytes < 1024 * 1024) {
|
||||
return $"{(memorySizeBytes / 1024f):F2} KB";
|
||||
}
|
||||
else {
|
||||
return $"{(memorySizeBytes / (1024f * 1024f)):F2} MB";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9e5e26603d72f24ba9552d00b246bf3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+4
-2
@@ -6,7 +6,7 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// Web请求类型
|
||||
/// </summary>
|
||||
public enum WebRequestType {
|
||||
public enum EnumNetworkRequestType {
|
||||
/// <summary> GET </summary>
|
||||
GET = 0,
|
||||
/// <summary> POST 表单 </summary>
|
||||
@@ -14,6 +14,8 @@ namespace MuHua {
|
||||
/// <summary> POST Json </summary>
|
||||
PostJson = 2,
|
||||
/// <summary> GET 获取图片 </summary>
|
||||
Texture = 3
|
||||
Texture = 3,
|
||||
/// <summary> GET 获取音频 </summary>
|
||||
Audio = 4
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8ffb8f1efd4ef5547bcb507cb77cc43a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+15
-6
@@ -7,13 +7,14 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 协程网络请求
|
||||
/// </summary>
|
||||
public static class WebRequest {
|
||||
public static class NetworkRequest {
|
||||
/// <summary> 发送请求 </summary>
|
||||
public static IEnumerator Execute(DataRequest request) {
|
||||
if (request.RequestType == WebRequestType.GET) { yield return Get(request); }
|
||||
if (request.RequestType == WebRequestType.PostForm) { yield return PostForm(request); }
|
||||
if (request.RequestType == WebRequestType.PostJson) { yield return PostJson(request); }
|
||||
if (request.RequestType == WebRequestType.Texture) { yield return Texture(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.GET) { yield return Get(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.PostForm) { yield return PostForm(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.PostJson) { yield return PostJson(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.Texture) { yield return Texture(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.Audio) { yield return Audio(request); }
|
||||
}
|
||||
public static IEnumerator Get(DataRequest request) {
|
||||
string url = request.Url;
|
||||
@@ -37,7 +38,7 @@ namespace MuHua {
|
||||
#if UNITY_2022
|
||||
using UnityWebRequest web = UnityWebRequest.PostWwwForm(url, "POST");
|
||||
#else
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, "POST");
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, "POST");
|
||||
#endif
|
||||
web.uploadHandler.Dispose();
|
||||
web.uploadHandler = new UploadHandlerRaw(postBytes);
|
||||
@@ -54,5 +55,13 @@ namespace MuHua {
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
/// <summary> 下载音频 </summary>
|
||||
public static IEnumerator Audio(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.UNKNOWN); // 自动检测音频类型
|
||||
yield return web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
-6
@@ -10,13 +10,14 @@ namespace MuHua {
|
||||
/// <summary>
|
||||
/// 异步网络请求
|
||||
/// </summary>
|
||||
public static class WebRequestAsync {
|
||||
public static class NetworkRequestAsync {
|
||||
/// <summary> 发送请求 </summary>
|
||||
public static void Execute(DataRequest request) {
|
||||
if (request.RequestType == WebRequestType.GET) { Get(request); }
|
||||
if (request.RequestType == WebRequestType.PostForm) { PostForm(request); }
|
||||
if (request.RequestType == WebRequestType.PostJson) { PostJson(request); }
|
||||
if (request.RequestType == WebRequestType.Texture) { Texture(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.GET) { Get(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.PostForm) { PostForm(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.PostJson) { PostJson(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.Texture) { Texture(request); }
|
||||
if (request.RequestType == EnumNetworkRequestType.Audio) { Audio(request); }
|
||||
}
|
||||
public static async void Get(DataRequest request) {
|
||||
string url = request.Url;
|
||||
@@ -40,7 +41,7 @@ namespace MuHua {
|
||||
#if UNITY_2022
|
||||
using UnityWebRequest web = UnityWebRequest.PostWwwForm(url, "POST");
|
||||
#else
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, "POST");
|
||||
using UnityWebRequest web = UnityWebRequest.Post(url, "POST");
|
||||
#endif
|
||||
web.uploadHandler.Dispose();
|
||||
web.uploadHandler = new UploadHandlerRaw(postBytes);
|
||||
@@ -57,6 +58,14 @@ namespace MuHua {
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
/// <summary> 下载音频 </summary>
|
||||
public static async void Audio(DataRequest request) {
|
||||
string url = request.Url;
|
||||
using UnityWebRequest web = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.UNKNOWN); // 自动检测音频类型
|
||||
await web.SendWebRequest();
|
||||
bool isDone = web.isDone && web.result == UnityWebRequest.Result.Success;
|
||||
request.RequestResultHandle(isDone, web);
|
||||
}
|
||||
public static TaskAwaiter<object> GetAwaiter(this UnityWebRequestAsyncOperation op) {
|
||||
var tcs = new TaskCompletionSource<object>();
|
||||
op.completed += (obj) => { tcs.SetResult(null); };
|
||||
Reference in New Issue
Block a user