UnityHTTP 使用与技术文档
2024-12-28 08:14:39作者:齐冠琰
1. 安装指南
UnityHTTP 是基于 Simon Wittber 的 UnityWeb 代码的一个 HTTP 库,适用于 Unity 的 TcpClient。在开始使用之前,请确保您了解并接受该项目的 GPL 许可。
- 确认 Unity 版本与 UnityHTTP 兼容。
- 下载 UnityHTTP 库文件。
- 将下载的文件导入到您的 Unity 项目中。
2. 项目的使用说明
UnityHTTP 支持在 Unity 的独立播放器和网络播放器中使用。该项目提供了与 JSON 数据交互的便捷方法。
基础用法
使用协程(Coroutine)进行 HTTP 请求:
public IEnumerator SomeRoutine() {
HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
someRequest.Send();
while( !someRequest.isDone )
{
yield return null;
}
JSONObject thing = new JSONObject( someRequest.response.Text );
}
使用闭包(Closure)进行 HTTP 请求(不需要在协程中):
HTTP.Request someRequest = new HTTP.Request( "get", "http://someurl.com/somewhere" );
someRequest.Send( ( request ) => {
JSONObject thing = new JSONObject( request.response.Text );
});
表单数据 POST 请求
WWWForm form = new WWWForm();
form.AddField( "something", "yo" );
form.AddField( "otherthing", "hey" );
HTTP.Request someRequest = new HTTP.Request( "post", "http://someurl.com/some/post/handler", form );
someRequest.Send( ( request ) => {
bool result = false;
Hashtable thing = (Hashtable)JSON.JsonDecode( request.response.Text, ref result );
if ( !result )
{
Debug.LogWarning( "JSON 解析失败!" );
return;
}
});
JSON 数据 POST 请求
Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );
HTTP.Request theRequest = new HTTP.Request( "post", "http://someurl.com/a/json/post/handler", data );
theRequest.Send( ( request ) => {
Hashtable result = request.response.Object;
if ( result == null )
{
Debug.LogWarning( "JSON 解析失败!" );
return;
}
});
非播放模式下的请求
在非播放模式下(例如自定义编辑器菜单命令或向导),您必须同步使用请求,因为 Unity 的主更新循环没有运行。调用将阻塞,直到响应可用。
Hashtable data = new Hashtable();
data.Add( "something", "hey!" );
data.Add( "otherthing", "YO!!!!" );
HTTP.Request theRequest = new HTTP.Request("post", "http://someurl.com/a/json/post/handler", data );
theRequest.synchronous = true;
theRequest.Send((request) => {
EditorUtility.DisplayDialog("请求已发送。", request.response.Text, "确定");
});
3. 项目 API 使用文档
UnityHTTP 提供了以下类和方法供您使用:
-
HTTP.Request: 构造和发送 HTTP 请求。Send(): 发送请求。Send(Action<HTTP.Request> callback): 发送请求,并提供完成后的回调函数。isDone: 检查请求是否完成。response: 获取响应数据。synchronous: 设置请求是否为同步。
-
JSONObject: 解析 JSON 字符串。 -
WWWForm: 构建 POST 请求的表单数据。
4. 项目安装方式
请遵循以下步骤安装 UnityHTTP:
- 确认您的 Unity 项目与 UnityHTTP 兼容。
- 下载 UnityHTTP 库文件。
- 将下载的文件导入到您的 Unity 项目中。
- 开始使用 UnityHTTP 进行 HTTP 请求。
登录后查看全文
热门项目推荐
相关项目推荐
GLM-5智谱 AI 正式发布 GLM-5,旨在应对复杂系统工程和长时域智能体任务。Jinja00
GLM-5-w4a8GLM-5-w4a8基于混合专家架构,专为复杂系统工程与长周期智能体任务设计。支持单/多节点部署,适配Atlas 800T A3,采用w4a8量化技术,结合vLLM推理优化,高效平衡性能与精度,助力智能应用开发Jinja00
jiuwenclawJiuwenClaw 是一款基于openJiuwen开发的智能AI Agent,它能够将大语言模型的强大能力,通过你日常使用的各类通讯应用,直接延伸至你的指尖。Python0204- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
AtomGit城市坐标计划AtomGit 城市坐标计划开启!让开源有坐标,让城市有星火。致力于与城市合伙人共同构建并长期运营一个健康、活跃的本地开发者生态。01
awesome-zig一个关于 Zig 优秀库及资源的协作列表。Makefile00
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
27
12
OpenHarmony documentation | OpenHarmony开发者文档
Dockerfile
609
4.05 K
Ascend Extension for PyTorch
Python
447
534
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
924
774
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.47 K
829
暂无简介
Dart
851
205
React Native鸿蒙化仓库
JavaScript
322
377
🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解
Java
69
21
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
372
251
昇腾LLM分布式训练框架
Python
131
157