GameFrameX Web 组件是一个高性能的 Unity HTTP 网络请求库,提供简洁易用的 API 来处理各种网络请求场景。支持 GET、POST 请求,可处理字符串、JSON、二进制数据等多种格式。
https://github.com/gameframex/com.gameframex.unity.web.git
在项目的 Packages/manifest.json 文件中添加:
{
"dependencies": {
"com.gameframex.unity.web": "https://github.com/gameframex/com.gameframex.unity.web.git",
"com.gameframex.unity": "https://github.com/gameframex/com.gameframex.unity.git"
}
}
Packages 目录下using GameFrameX.Web.Runtime;
using System.Threading.Tasks;
using System.Collections.Generic;
public class WebExample : MonoBehaviour
{
private IWebManager webManager;
private async void Start()
{
// 获取 Web 管理器实例
webManager = GameFrameworkEntry.GetModule<IWebManager>();
// 发送 GET 请求获取字符串
string result = await webManager.GetToString("https://api.example.com/data");
Debug.Log("GET Response: " + result);
// 发送 POST 请求带表单数据
var formData = new Dictionary<string, string>
{
{ "username", "testuser" },
{ "password", "testpass" }
};
string postResult = await webManager.PostToString("https://api.example.com/login", formData);
Debug.Log("POST Response: " + postResult);
}
}
using GameFrameX.Web.Runtime;
using System.Threading.Tasks;
using System.Collections.Generic;
public class MyWebService : MonoBehaviour
{
private WebComponent webComponent;
private void Awake()
{
webComponent = gameObject.GetOrAddComponent<WebComponent>();
}
public async Task<string> GetUserDataAsync(string userId)
{
var queryParams = new Dictionary<string, string>
{
{ "userId", userId }
};
var headers = new Dictionary<string, string>
{
{ "Authorization", "Bearer your-token-here" }
};
return await webComponent.GetToString(
"https://api.example.com/users",
queryParams,
headers
);
}
public async Task<byte[]> DownloadFileAsync(string fileUrl)
{
return await webComponent.GetToBytes(fileUrl);
}
}
// 获取字符串响应
Task<string> GetToString(string url);
Task<string> GetToString(string url, Dictionary<string, string> queryString);
Task<string> GetToString(string url, Dictionary<string, string> queryString, Dictionary<string, string> header);
// 获取字节数组响应
Task<byte[]> GetToBytes(string url);
Task<byte[]> GetToBytes(string url, Dictionary<string, string> queryString);
Task<byte[]> GetToBytes(string url, Dictionary<string, string> queryString, Dictionary<string, string> header);
// 表单 POST 请求
Task<string> PostToString(string url, Dictionary<string, string> formData = null);
Task<string> PostToString(string url, Dictionary<string, string> formData, Dictionary<string, string> queryString);
Task<string> PostToString(string url, Dictionary<string, string> formData, Dictionary<string, string> queryString, Dictionary<string, string> header);
Task<byte[]> PostToBytes(string url, Dictionary<string, string> formData);
Task<byte[]> PostToBytes(string url, Dictionary<string, string> formData, Dictionary<string, string> queryString);
Task<byte[]> PostToBytes(string url, Dictionary<string, string> formData, Dictionary<string, string> queryString, Dictionary<string, string> header);
// 二进制数据 POST 请求
Task<WebBufferResult> PostToBytes(string url, byte[] binaryData, Dictionary<string, string> queryString, Dictionary<string, string> header, object userData = null);
// Protocol Buffers 支持
Task<T> GetProtoBuf<T>(string url) where T : class, IExtensible;
Task<T> PostProtoBuf<T>(string url, IExtensible requestData) where T : class, IExtensible;
// JSON 支持(通过扩展方法)
Task<T> GetJson<T>(string url);
Task<T> PostJson<T>(string url, object data);
// 设置请求超时时间(默认:30秒)
TimeSpan RequestTimeout { get; set; }
// 设置最大并发连接数(默认:8)
int MaxConnectionPerServer { get; set; }
// 启用/禁用详细日志
bool EnableWebLog { get; set; }
public async Task UploadBinaryDataAsync(byte[] fileData, string fileName)
{
var webManager = GameFrameworkEntry.GetModule<IWebManager>();
var queryParams = new Dictionary<string, string>
{
{ "fileName", fileName }
};
var headers = new Dictionary<string, string>
{
{ "Content-Type", "application/octet-stream" },
{ "Authorization", "Bearer your-token" }
};
WebBufferResult result = await webManager.PostToBytes(
"https://api.example.com/upload",
fileData,
queryParams,
headers
);
if (result.IsSuccess)
{
Debug.Log("Upload successful!");
byte[] responseData = result.Data;
// 处理响应数据
}
}
[ProtoContract]
public class UserRequest
{
[ProtoMember(1)]
public string UserId { get; set; }
}
[ProtoContract]
public class UserResponse
{
[ProtoMember(1)]
public string UserName { get; set; }
[ProtoMember(2)]
public string Email { get; set; }
}
public async Task<UserResponse> GetUserProtoBufAsync(string userId)
{
var request = new UserRequest { UserId = userId };
// 使用扩展方法发送 Protocol Buffers 请求
return await webManager.PostProtoBuf<UserResponse>(
"https://api.example.com/user/protobuf",
request
);
}
public async Task<string> SafeWebRequestAsync(string url)
{
try
{
return await webManager.GetToString(url);
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.Timeout)
{
Debug.LogError("请求超时: " + ex.Message);
return null;
}
catch (IOException ex)
{
Debug.LogError("网络IO错误: " + ex.Message);
return null;
}
catch (Exception ex)
{
Debug.LogError("请求失败: " + ex.Message);
return null;
}
}
private void ConfigureWebManager()
{
var webManager = GameFrameworkEntry.GetModule<IWebManager>();
// 设置请求超时为 60 秒
webManager.RequestTimeout = TimeSpan.FromSeconds(60);
// 设置最大并发连接数为 16
webManager.MaxConnectionPerServer = 16;
// 启用详细日志
webManager.EnableWebLog = true;
}
WebGL 平台限制
await 异步等待而不是阻塞调用跨域问题 (CORS)
HTTPS 证书问题
// 启用详细调试日志
webManager.EnableWebLog = true;
// 在 Player Settings 中启用 Development Build 和 Script Debugging
// 这样可以获得更详细的错误信息
查看 CHANGELOG.md 获取详细的版本更新信息。
欢迎提交 Issue 和 Pull Request!
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)本项目采用 MIT 许可证 - 查看 LICENSE.md 文件了解详情。
如果你有任何问题或需要帮助,可以通过以下方式联系我们: