当前位置: 首页 > news >正文

Unity UnityWebRequest封装类

简化api调用流程,非常奈斯。

RestWebClient.cs

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;namespace MYTOOL.RestClient
{/// <summary>/// UnityWebRequest封装类/// </summary>public class RestWebClient{private readonly MonoBehaviour executer;public RestWebClient(MonoBehaviour executer){if (executer == null) throw new ArgumentNullException();this.executer = executer;}#region >> GETpublic Coroutine Get(string url, Request req){if (req == null) throw new ArgumentNullException();return executer.StartCoroutine(InnerGet(url, req));}#endregion#region >> PUTpublic Coroutine Put(string url, Request req){if (req == null) throw new ArgumentNullException();return executer.StartCoroutine(InnerPut(url, req));}#endregion#region >> POSTpublic Coroutine Post(string url, Request req){if (req == null) throw new ArgumentNullException();return executer.StartCoroutine(InnerPost(url, req));}public Coroutine PostForm(string url, WWWForm formData, Request req){if (req == null) throw new ArgumentNullException();return executer.StartCoroutine(InnerPostForm(url, formData, req));}#endregion#region >> DELETEpublic Coroutine Delete(string url, Request req){if (req == null) throw new ArgumentNullException();return executer.StartCoroutine(InnerDelete(url, req));}#endregionprivate IEnumerator InnerGet(string url, Request req){req.OnBegin?.Invoke();ushort retryCount = req.RetryCount;do{using UnityWebRequest webRequest = UnityWebRequest.Get(url);InitUnityWebRequest(webRequest, req);yield return webRequest.SendWebRequest();bool isSuccess = webRequest.result == UnityWebRequest.Result.Success;if (isSuccess || retryCount == 0){if (DebugMode) LogInfo("GET", webRequest.responseCode, isSuccess, webRequest.url);req.Invoke(isSuccess, webRequest.responseCode, webRequest.downloadHandler.data, webRequest.error);yield break;}if (req.RetryInterval > 0){yield return new WaitForSeconds(req.RetryInterval);}} while (retryCount-- > 0);}private IEnumerator InnerPut(string url, Request req){req.OnBegin?.Invoke();ushort retryCount = req.RetryCount;do{using UnityWebRequest webRequest = UnityWebRequest.Put(url, req.BodyData);InitUnityWebRequest(webRequest, req);yield return webRequest.SendWebRequest();bool isSuccess = webRequest.result == UnityWebRequest.Result.Success;if (isSuccess || retryCount == 0){if (DebugMode) LogInfo("PUT", webRequest.responseCode, isSuccess, webRequest.url);req.Invoke(isSuccess, webRequest.responseCode, webRequest.downloadHandler.data, webRequest.error);yield break;}if (req.RetryInterval > 0){yield return new WaitForSeconds(req.RetryInterval);}} while (retryCount-- > 0);}private IEnumerator InnerPost(string url, Request req){req.OnBegin?.Invoke();ushort retryCount = req.RetryCount;do{using UnityWebRequest webRequest = UnityWebRequest.Post(url, req.BodyData);InitUnityWebRequest(webRequest, req);yield return webRequest.SendWebRequest();bool isSuccess = webRequest.result == UnityWebRequest.Result.Success;if (isSuccess || retryCount == 0){if (DebugMode) LogInfo("POST", webRequest.responseCode, isSuccess, webRequest.url);req.Invoke(isSuccess, webRequest.responseCode, webRequest.downloadHandler.data, webRequest.error);yield break;}if (req.RetryInterval > 0){yield return new WaitForSeconds(req.RetryInterval);}} while (retryCount-- > 0);}private IEnumerator InnerPostForm(string url, WWWForm formData, Request req){req.OnBegin?.Invoke();ushort retryCount = req.RetryCount;do{using UnityWebRequest webRequest = UnityWebRequest.Post(url, formData);InitUnityWebRequest(webRequest, req);yield return webRequest.SendWebRequest();bool isSuccess = webRequest.result == UnityWebRequest.Result.Success;if (isSuccess || retryCount == 0){if (DebugMode) LogInfo("POST", webRequest.responseCode, isSuccess, webRequest.url);req.Invoke(isSuccess, webRequest.responseCode, webRequest.downloadHandler.data, webRequest.error);yield break;}if (req.RetryInterval > 0){yield return new WaitForSeconds(req.RetryInterval);}} while (retryCount-- > 0);}private IEnumerator InnerDelete(string url, Request req){req.OnBegin?.Invoke();ushort retryCount = req.RetryCount;do{using UnityWebRequest webRequest = UnityWebRequest.Delete(url);webRequest.downloadHandler = new DownloadHandlerBuffer();InitUnityWebRequest(webRequest, req);yield return webRequest.SendWebRequest();bool isSuccess = webRequest.result == UnityWebRequest.Result.Success;if (isSuccess || retryCount == 0){if (DebugMode) LogInfo("DELETE", webRequest.responseCode, isSuccess, webRequest.url);req.Invoke(isSuccess, webRequest.responseCode, webRequest.downloadHandler.data, webRequest.error);yield break;}if (req.RetryInterval > 0){yield return new WaitForSeconds(req.RetryInterval);}} while (retryCount-- > 0);}private void InitUnityWebRequest(UnityWebRequest webRequest, Request req){webRequest.timeout = req.Timeout;if (webRequest.uploadHandler != null && string.IsNullOrEmpty(req.ContentType) == false){webRequest.uploadHandler.contentType = req.ContentType;}var queryString = req.GetQueryString();if (string.IsNullOrEmpty(queryString) == false){if (webRequest.url.EndsWith('?')){webRequest.url += queryString;}else{webRequest.url += $"?{queryString}";}}var headers = req.GetHeaders();foreach (var header in headers){webRequest.SetRequestHeader(header.Key, header.Value);}}public static bool DebugMode = true;private static void LogInfo(string method, long code, bool success, string info){string str = success ? $"<color=green>{info}</color>" : $"<color=red>{info}</color>";Debug.Log($"[{method}] [{code}] => {str}");}}
}

Request.cs

using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;namespace MYTOOL.RestClient
{public class Request{public const string APPLICATION_JSON = "application/json";/// <summary>/// 超时时间/// </summary>public int Timeout { get; private set; }/// <summary>/// 重试次数/// </summary>public ushort RetryCount { get; private set; }/// <summary>/// 重试间隔(秒)/// </summary>public float RetryInterval { get; private set; }/// <summary>/// 内容/// </summary>public string BodyData { get; private set; }/// <summary>/// 内容类型/// </summary>public string ContentType { get; private set; }/// <summary>/// 请求是否完成/// </summary>public bool IsDone { get; private set; }/// <summary>/// 请求开始前的回调/// </summary>public Action OnBegin;/// <summary>/// 请求完成后的回调/// </summary>private readonly Action<Response> OnComplete;private readonly Dictionary<string, string> Headers;private readonly Dictionary<string, List<string>> Query;public Request(Action<Response> onComplete, int timeout = 10){OnComplete = onComplete;Timeout = timeout;Headers = new Dictionary<string, string>();Query = new Dictionary<string, List<string>>();}public Request(Action<Response> onComplete, string bodyData, int timeout = 10) : this(onComplete, timeout){BodyData = bodyData;}public Request Reset(){IsDone = false;return this;}internal void Invoke(bool succeed, long statusCode, byte[] data, string error){IsDone = true;OnComplete?.Invoke(new Response(succeed, statusCode, data, error));}public Request AddQuery(string key, string value){if (Query.ContainsKey(key) == false){Query[key] = new List<string>();}Query[key].Add(value);return this;}public bool RemoveQuery(string key){return Query.Remove(key);}public Request SetRequestHeader(string key, string value){Headers[key] = value;return this;}public Dictionary<string, string> GetHeaders(){return new Dictionary<string, string>(Headers);}public string GetQueryString(){StringBuilder sb = new StringBuilder();foreach (var kv in Query){if (sb.Length > 0 && kv.Value.Count > 0) sb.Append('&');// 将每个键值对转换为字符串并连接起来var values = string.Join('&', kv.Value.Select(value => $"{kv.Key}={value}"));sb.Append(values);}return sb.ToString();}/// <summary>/// 设置超时时间/// </summary>/// <param name="timeout"></param>/// <returns></returns>public Request SetTimeout(int timeout){Timeout = timeout;return this;}/// <summary>/// 设置重试参数/// </summary>/// <param name="retryCount">重试次数</param>/// <param name="sec">重试间隔(秒)</param>/// <returns></returns>public Request SetRetryOption(ushort retryCount, float retryInterval){RetryCount = retryCount;RetryInterval = retryInterval;return this;}/// <summary>/// 设置Body内容/// </summary>/// <param name="bodyData"></param>/// <returns></returns>public Request SetBodyData(string bodyData){BodyData = bodyData;return this;}/// <summary>/// 设置Body内容,并指定内容类型/// </summary>/// <param name="bodyData"></param>/// <param name="contentType">如:application/json</param>/// <returns></returns>public Request SetBodyData(string bodyData, string contentType){BodyData = bodyData;ContentType = contentType;return this;}/// <summary>/// 设置内容类型/// </summary>/// <param name="contentType">如:application/json</param>/// <returns></returns>public Request SetContentType(string contentType){ContentType = contentType;return this;}}
}

Response.cs

using System.Text;namespace MYTOOL.RestClient
{public class Response{public bool Succeed { get; }public long StatusCode { get; }public byte[] Data { get; }public string Error { get; }public bool HasError => string.IsNullOrEmpty(Error) == false;public Response(bool succeed, long statusCode, byte[] data, string error){Succeed = succeed;StatusCode = statusCode;Data = data;Error = error;}public string GetResponseData(Encoding encoding = null){if (Data == null) return "";encoding ??= Encoding.UTF8;return encoding.GetString(Data);}}
}

使用例子

using MYTOOL.RestClient;
using UnityEngine;public class NewBehaviourScript : MonoBehaviour
{private RestWebClient client;private void Awake(){//初始化client = new RestWebClient(this);}private void Start(){//设置请求完成后回调 与 超时时间Request req = new Request(OnGetBaidu, 30);//设置重试次数 与 重试间隔req.SetRetryOption(3, 0.2f);client.Get("https://www.baidu.com/", req);}void OnGetBaidu(Response resp){if (resp.Succeed){if (resp.StatusCode == 200 || resp.StatusCode == 204){Debug.Log($"请求成功,{resp.GetResponseData()}");}}else{Debug.Log($"请求失败,{resp.Error}");}}
}

在这里插入图片描述

相关文章:

Unity UnityWebRequest封装类

简化api调用流程&#xff0c;非常奈斯。 RestWebClient.cs using System; using System.Collections; using UnityEngine; using UnityEngine.Networking;namespace MYTOOL.RestClient {/// <summary>/// UnityWebRequest封装类/// </summary>public class RestW…...

JVM内存划分

Java虚拟机&#xff08;JVM&#xff09;的内存划分是指JVM在运行时所使用的内存区域的组织和管理方式。JVM内存主要分为以下几个区域&#xff1a; 堆区&#xff08;Heap&#xff09;&#xff1a; 用途&#xff1a;用于存储所有对象实例和数组&#xff0c;是JVM中最大的一块内存…...

c++ 全排列

在C中&#xff0c;全排列&#xff08;permutation&#xff09;可以使用递归算法或标准库函数来实现。以下是使用递归和STL库std::next_permutation来生成一个集合的全排列的两种方法。 方法一&#xff1a;递归算法 递归方法通过交换元素来生成所有可能的排列组合。 #include…...

未授权访问漏洞系列详解⑤!

Kubernetes Api Server未授权访问漏洞 Kubernetes 的服务在正常启动后会开启两个端口:Localhost Port(默认8080)Secure Port(默认6443)。这两个端口都是提供 Api Server 服务的&#xff0c;一个可以直接通过Web 访问&#xff0c;另一个可以通过 kubectl 客户端进行调用。如果运…...

【CONDA】库冲突解决办法

如今&#xff0c;使用PYTHON作为开发语言时&#xff0c;或多或少都会使用到conda。安装Annaconda时一般都会选择在启动终端时进入conda的base环境。该操作&#xff0c;实际上是在~/.bashrc中添加如下脚本&#xff1a; # >>> conda initialize >>> # !! Cont…...

【网络世界】数据链路层

目录 &#x1f308;前言&#x1f308; &#x1f4c1; 初识数据链路层 &#x1f4c2; 概念 &#x1f4c2; 协议格式 &#x1f4c1; MAC地址 &#x1f4c2; 概念 &#x1f4c2; 与IP地址的区别 &#x1f4c1; MTU &#x1f4c2; 对IP协议的影响 &#x1f4c2; 对UDP协议的影响…...

AllReduce通信库;Reduce+LayerNorm+Broadcast 算子;LayerNorm(层归一化)和Broadcast(广播)操作;

目录 AllReduce通信库 一、定义与作用 二、常见AllReduce通信库 三、AllReduce通信算法 四、总结 Reduce+LayerNorm+Broadcast 算子 1. Reduce 算子 2. LayerNorm 算子 3. Broadcast 算子 组合作用 LayerNorm(层归一化)和Broadcast(广播)操作 提出的创新方案解析 优点与潜在…...

2024.8.5 作业

使用有名管道实现&#xff0c;一个进程用于给另一个进程发消息&#xff0c;另一个进程收到消息后&#xff0c;展示到终端上&#xff0c;并且将消息保存到文件上一份 代码&#xff1a; /*******************************************/ 文件名&#xff1a;create.c /********…...

MySQL数据库——数据库的基本操作

目录 三、数据库的基本操作 1.数据库中库的操作 ①创建数据库 ②字符集和校验规则 ③操纵数据库 ④备份与恢复 2.数据库中表的操作 ①创建表 ②查看表 1> 查看表位于的数据库 2>查看所有表 3>查看表中的数据 4>查看创建表的时候的详细信息 ③修改表 …...

SQL数据库语句练习

1、mysql常用的数据类型是_整数&#xff08;int&#xff09;__、_小数&#xff08;decimal&#xff09;__、_字符串&#xff08;varchar&#xff09;__、_日期时间&#xff08;datetime&#xff09;___。 2、mysql的约束有__主键&#xff08;primary key&#xff09;_、_非空&…...

【Python】常用的pdf提取库介绍对比

提取PDF内容的Python库有多种选择&#xff0c;每个库都有其独特的优缺点。以下是一些常用的库以及它们的优缺点和示例代码&#xff1a; pdfplumberPyMuPDF (fitz)PyPDF2PDFMinerCamelot 1. pdfplumber 优点&#xff1a; 易于使用&#xff0c;提供简单直观的API。能提取文本…...

sbatch提交并行作业 运行python程序 指定输入参数从1到100

#!/bin/bash #SBATCH --job-namemy_python_job #SBATCH --outputmy_python_job_%j.out #SBATCH --errormy_python_job_%j.err #SBATCH --ntasks100# 载入所需模块 # module load python/3.8.5# 执行Python脚本并传递任务ID作为参数 for i in {1..100}; dosrun python my_script…...

OD C卷 - 中庸行者

中庸行者 &#xff08;200&#xff09; 给一个m*n的整数矩阵作为地图&#xff0c;矩阵数值为地形的高度&#xff0c;选择图中任意一点作为起点&#xff0c;向左右上下四个方向移动&#xff1a; 只能上坡、下坡&#xff0c;不能走相同高度的点&#xff1b;不允许连续上坡 或者连…...

最新CSS3横向菜单的实现

横向菜单 原始代码&#xff1a; <nav class"list1"><ul><li><a href"#">Shirts</a></li><li><a href"#">Pants</a></li><li><a href"#">Dresses</a>…...

(2024,LlamaGen,Llama,自回归下一token预测,模型扩展)自回归模型优于扩散:Llama 用于可扩展图像生成

Autoregressive Model Beats Diffusion: Llama for Scalable Image Generation 目录 0. 摘要 1. 引言 2. 自回归模型在图像生成中的应用 2.1 概述 2.2 图像 tokenizer 2.3 自回归模型生成图像 2.4 规模扩展 2.5 服务 3. 实验 5. 结论 0. 摘要 我们介绍 LlamaGen&…...

重新安装操作系统的软件都有哪些?

重新安装操作系统关键步骤 重新安装操作系统通常涉及到几个关键步骤&#xff1a;创建可启动媒体、备份现有数据、安装操作系统、以及系统初始化和配置。以下是一些常用工具和软件&#xff0c;它们可以帮助你完成这些步骤&#xff1a; 创建可启动媒体 Rufus&#xff1a;用于W…...

深圳水务展|2025深圳国际水务科技博览会

2025深圳国际水务科技博览会 展会主题: 新质生产力赋能水务产业高质量发展 展会时间&#xff1a;2025年7月24-26日 展会地点&#xff1a;深圳会展中心&#xff08;福田&#xff09; 主办单位&#xff1a; 广东省水利学会 深圳市水务学会 协办单位&#xff1a; 中国水利…...

OpenAI not returning a result?

题意&#xff1a;OpenAI 没有返回结果吗&#xff1f; 问题背景&#xff1a; Im trying to use the OpenAI beta but I cant seem to get a result. Im accessing the API via an NPM package (openai-api - npm). I have that setup and working but when I make a request th…...

[Windows]_[初级]_[GetVersionEx获取系统版本错误的原因]

场景 开发WTL/ATL/Win32程序时, 有时候需要获取系统版本号&#xff0c;以便判断用户在使用软件时的系统。在某一天突然发现获取的系统版本号是错的, 一直是版本号6.2.x,什么情况&#xff1f; 说明 如果软件没有针对Win8.1或Win10以上的系统做兼容处理&#xff0c;返回的是Wi…...

2024,Java开发在中国市场还有发展前景吗?

随着2024年的到来&#xff0c;Java作为一种经典而强大的编程语言&#xff0c;依然在中国的软件开发市场中扮演着重要角色。然而&#xff0c;许多人对Java的未来发展前景持有不同的看法。让我们来探讨一下当前情况和未来的走向。 Java程序员真的过剩了吗&#xff1f; 2023年, 各…...

业务系统对接大模型的基础方案:架构设计与关键步骤

业务系统对接大模型&#xff1a;架构设计与关键步骤 在当今数字化转型的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中&#xff0c;不仅可以优化用户体验&#xff0c;还能为业务决策提供…...

CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型

CVPR 2025 | MIMO&#xff1a;支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题&#xff1a;MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者&#xff1a;Yanyuan Chen, Dexuan Xu, Yu Hu…...

遍历 Map 类型集合的方法汇总

1 方法一 先用方法 keySet() 获取集合中的所有键。再通过 gey(key) 方法用对应键获取值 import java.util.HashMap; import java.util.Set;public class Test {public static void main(String[] args) {HashMap hashMap new HashMap();hashMap.put("语文",99);has…...

QMC5883L的驱动

简介 本篇文章的代码已经上传到了github上面&#xff0c;开源代码 作为一个电子罗盘模块&#xff0c;我们可以通过I2C从中获取偏航角yaw&#xff0c;相对于六轴陀螺仪的yaw&#xff0c;qmc5883l几乎不会零飘并且成本较低。 参考资料 QMC5883L磁场传感器驱动 QMC5883L磁力计…...

使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台

🎯 使用 Streamlit 构建支持主流大模型与 Ollama 的轻量级统一平台 📌 项目背景 随着大语言模型(LLM)的广泛应用,开发者常面临多个挑战: 各大模型(OpenAI、Claude、Gemini、Ollama)接口风格不统一;缺乏一个统一平台进行模型调用与测试;本地模型 Ollama 的集成与前…...

SAP学习笔记 - 开发26 - 前端Fiori开发 OData V2 和 V4 的差异 (Deepseek整理)

上一章用到了V2 的概念&#xff0c;其实 Fiori当中还有 V4&#xff0c;咱们这一章来总结一下 V2 和 V4。 SAP学习笔记 - 开发25 - 前端Fiori开发 Remote OData Service(使用远端Odata服务)&#xff0c;代理中间件&#xff08;ui5-middleware-simpleproxy&#xff09;-CSDN博客…...

重启Eureka集群中的节点,对已经注册的服务有什么影响

先看答案&#xff0c;如果正确地操作&#xff0c;重启Eureka集群中的节点&#xff0c;对已经注册的服务影响非常小&#xff0c;甚至可以做到无感知。 但如果操作不当&#xff0c;可能会引发短暂的服务发现问题。 下面我们从Eureka的核心工作原理来详细分析这个问题。 Eureka的…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...

招商蛇口 | 执笔CID,启幕低密生活新境

作为中国城市生长的力量&#xff0c;招商蛇口以“美好生活承载者”为使命&#xff0c;深耕全球111座城市&#xff0c;以央企担当匠造时代理想人居。从深圳湾的开拓基因到西安高新CID的战略落子&#xff0c;招商蛇口始终与城市发展同频共振&#xff0c;以建筑诠释对土地与生活的…...

比较数据迁移后MySQL数据库和OceanBase数据仓库中的表

设计一个MySQL数据库和OceanBase数据仓库的表数据比较的详细程序流程,两张表是相同的结构,都有整型主键id字段,需要每次从数据库分批取得2000条数据,用于比较,比较操作的同时可以再取2000条数据,等上一次比较完成之后,开始比较,直到比较完所有的数据。比较操作需要比较…...