修改网络模块
增加下载音频, 增加下载纹理的调试功能
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// 请求数据
|
||||
/// </summary>
|
||||
public abstract class DataRequest {
|
||||
/// <summary> Web请求地址 </summary>
|
||||
public abstract string Url { get; }
|
||||
/// <summary> Web请求类型 </summary>
|
||||
public abstract EnumNetworkRequestType RequestType { get; }
|
||||
/// <summary> 提交json数据 </summary>
|
||||
public virtual string Json { get; }
|
||||
/// <summary> 提交Form表单数据 </summary>
|
||||
public virtual WWWForm Form { get; }
|
||||
|
||||
/// <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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a81036c14ed55ea4aa3beea60ebaa871
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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:
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Get请求数据
|
||||
/// </summary>
|
||||
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 EnumNetworkRequestType RequestType => EnumNetworkRequestType.GET;
|
||||
|
||||
/// <summary> Web Get请求数据 </summary>
|
||||
public DataRequestGet(string url, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.OnCallback = OnCallback;
|
||||
}
|
||||
|
||||
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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf1da230bb48a6a4aa51686ff32e9f16
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace MuHua {
|
||||
/// <summary>
|
||||
/// Post请求数据
|
||||
/// </summary>
|
||||
public class DataRequestPost : DataRequest {
|
||||
public readonly string url;
|
||||
public readonly string json;
|
||||
public readonly WWWForm form;
|
||||
public readonly EnumNetworkRequestType type;
|
||||
|
||||
public string result;
|
||||
public Action<string> OnError;
|
||||
public Action<string> OnCallback;
|
||||
|
||||
public override string Url => url;
|
||||
public override EnumNetworkRequestType RequestType => type;
|
||||
public override string Json => json;
|
||||
public override WWWForm Form => form;
|
||||
|
||||
/// <summary> Web Post请求 提交json数据 </summary>
|
||||
public DataRequestPost(string url, string json, Action<string> OnCallback = null) {
|
||||
this.url = url;
|
||||
this.json = json;
|
||||
this.OnCallback = OnCallback;
|
||||
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 = 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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f43bea5409c953347974214f4eb5320c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b0407bff8565543469def55f9788ecf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user