符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
小编给大家分享一下Unity3D网络通讯中HttpRestful请求如何封装,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于企业网站制作,高端网页制作,对成都葡萄架等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业营销推广优化,H5建站,响应式网站。
整理代码脚本
02
HttpRestful封装
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class HttpRestful : MonoBehaviour
{
private static HttpRestful _instance;
public static HttpRestful Instance
{
get
{
if(_instance == null)
{
GameObject goRestful = new GameObject("HttpRestful");
_instance = goRestful.AddComponent
(); }
return _instance;
}
}
#region Get请求
///
/// Get请求
///
///
///
public void Get(string url, Action
actionResult = null) {
StartCoroutine(_Get(url, actionResult));
}
private IEnumerator _Get(string url, Action
action) {
using (UnityWebRequest request = UnityWebRequest.Get(url))
{
yield return request.SendWebRequest();
string resstr = "";
if (request.isNetworkError || request.isHttpError)
{
resstr = request.error;
}
else
{
resstr = request.downloadHandler.text;
}
if (action != null)
{
action(request.isHttpError, resstr);
}
}
}
#endregion
#region POST请求
public void Post(string url, string data, Action
actionResult = null) {
StartCoroutine(_Post(url, data, actionResult));
}
private IEnumerator _Post(string url, string data, Action
action) {
using (UnityWebRequest request = new UnityWebRequest(url, "POST"))
{
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(data));
request.SetRequestHeader("content-type", "application/json;charset=utf-8");
request.downloadHandler = new DownloadHandlerBuffer();
yield return request.SendWebRequest();
string resstr = "";
if (request.isNetworkError || request.isHttpError)
{
resstr = request.error;
}
else
{
resstr = request.downloadHandler.text;
}
if (action != null)
{
action(request.isHttpError, resstr);
}
}
}
#endregion
}
03
调用HttpRestful
HttpRestful.Instance.Get(url, actionRes);HttpRestful.Instance.Post(url, json, actionRes);
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UIScripts : MonoBehaviour
{
[Header("按钮")]
public Button btnget;
public Button btngetparm;
public Button btnjson;
public Button btnpost;
[Space]
[Header("显示")]
public Text txtshow;
[Space]
[Header("输入框")]
public InputField edturl;
public InputField edtparm;
//定义一个返回数据的Action
private Action
actionRes; // Start is called before the first frame update
void Start()
{
InitAction();
//Get按钮操作
btnget.onClick.AddListener(() =>
{
Debug.Log(edturl.text);
string url = edturl.text;
HttpRestful.Instance.Get(url, actionRes);
});
btngetparm.onClick.AddListener(() =>
{
string url = edturl.text;
string param = edtparm.text;
string allurl = url + "/Info?Summary=" + param;
HttpRestful.Instance.Get(allurl, actionRes);
});
btnjson.onClick.AddListener(() => StartCoroutine(JsonConvert()));
btnpost.onClick.AddListener(() =>
{
WeatherForecast item = new WeatherForecast();
item.Summary = "Alvin";
item.Date = DateTime.Now;
item.TemperatureC = 10;
item.TemperatureF = 20;
string json = JsonUtility.ToJson(item);
string url = edturl.text + "/Reg";
Debug.Log(url);
HttpRestful.Instance.Post(url, json, actionRes);
});
}
///
/// 写返回Action的处理方法
///
private void InitAction()
{
actionRes = new Action
((bl, str) => {
if (bl)
{
txtshow.text = str;
}
else
{
string resjson = "{\"array\":" + str + "}";
txtshow.text = resjson;
WeatherData lists = JsonUtility.FromJson
(resjson); StringBuilder sb = new StringBuilder();
foreach (WeatherForecast item in lists.array)
{
sb.Append("Date:" + item.Date + " Summary:" + item.Summary + " TemperatureF:"
+ item.TemperatureF + "TemperatureC:" + item.TemperatureC + "\r\n");
}
txtshow.text = sb.ToString();
}
});
}
IEnumerator JsonConvert()
{
WeatherForecast item = new WeatherForecast();
item.Summary = "Alvin";
item.Date = DateTime.Now;
item.TemperatureC = 10;
item.TemperatureF = 20;
string json = JsonUtility.ToJson(item);
txtshow.text = json;
yield return new WaitForSeconds(3f);
WeatherForecast newitem = JsonUtility.FromJson
(json); string showtext = "Summary:" + newitem.Summary + " Date:" + newitem.Date +
" C:" + newitem.TemperatureC + " F:" + newitem.TemperatureF;
txtshow.text = showtext;
}
}
实现效果
看完了这篇文章,相信你对“Unity3D网络通讯中HttpRestful请求如何封装”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!