admin管理员组文章数量:1122879
Unity 启动时带参数,网页后端进行数据交互 UnityWebRequest ,Post,Get,Delete
打包后启动.exe带参数
启动传参数可以参考这篇文章: 浏览器调用本地exe(应用程序)方法
Unity准备工作
需要用到这个API
Environment.GetCommandLineArgs()
官方解释:返回包含当前进程的命令行参数的字符串数组
返回值是一个string[],当Length>1的时候就是带参数,注意:在UnityEditor模式也就是编辑器模式会有默认参数返回,Length是大于1的
UnityWebRequest API使用
常用的就是Get与Post,代码比较简单,有的地方传参时需要注意有没有RequestHeader,这个需要进行特殊处理
代码
IEnumerator IePostRequest(string url, object mRequestData, GetDate getDate){string paramStr = JsonUtility.ToJson(mRequestData);byte[] databyte = Encoding.UTF8.GetBytes(paramStr);using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST)){request.uploadHandler = new UploadHandlerRaw(databyte);request.downloadHandler = new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");request.SetRequestHeader("Authorization", "Bearer" + sessionId);request.SetRequestHeader("Content-Length", databyte.Length.ToString());yield return request.SendWebRequest();if (request.result != UnityWebRequest.Result.Success){Debug.Log(request.error);}elsegetDate(request.downloadHandler.text);}}IEnumerator IeGetRequest(string url, GetDate getDate){using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)){request.SetRequestHeader("Content-Type", "application/json");request.SetRequestHeader("Authorization", "Bearer" + sessionId);request.downloadHandler = new DownloadHandlerBuffer();yield return request.SendWebRequest();if (request.result != UnityWebRequest.Result.Success){Debug.Log(request.error);}elsegetDate(request.downloadHandler.text);}}
其中GetDate getDate是一个委托,用来接收返回的数据
public delegate void GetDate(string data);
完整代码
using System;
using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;public class UnityPageRequest : MonoBehaviour
{/// <summary>/// token/// </summary>string sessionId;public delegate void GetDate(string data);private void Awake(){string[] CommandLineArgs = Environment.GetCommandLineArgs();string testData = "";if (CommandLineArgs.Length < 2){Debug.Log("没有参数");//Application.Quit();//启动时没有参数则退出}else{if (CommandLineArgs[1] == ""){//启动参数是空的}else{//解析各种IDtestData = CommandLineArgs[1];}}ParseInitData(testData);}void ParseInitData(string data){//启动数据在这里进行解析//sessionId = 解析的数据}public void PostRequest(string url, object mRequestData, GetDate getDate){StartCoroutine(IePostRequest(url, mRequestData, getDate));}public void GetRequest(string url, string mRequestData, GetDate getDate){StartCoroutine(IeGetRequest(url + mRequestData, getDate));}public void DeleteRequest(string url){StartCoroutine(IeDeleteRequest(url));}IEnumerator IePostRequest(string url, object mRequestData, GetDate getDate){string paramStr = JsonUtility.ToJson(mRequestData);byte[] databyte = Encoding.UTF8.GetBytes(paramStr);using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST)){request.uploadHandler = new UploadHandlerRaw(databyte);request.downloadHandler = new DownloadHandlerBuffer();request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");request.SetRequestHeader("Authorization", "Bearer" + sessionId);request.SetRequestHeader("Content-Length", databyte.Length.ToString());yield return request.SendWebRequest();if (request.result != UnityWebRequest.Result.Success){Debug.Log(request.error);}elsegetDate(request.downloadHandler.text);}}IEnumerator IeGetRequest(string url, GetDate getDate){using (var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)){request.SetRequestHeader("Content-Type", "application/json");request.SetRequestHeader("Authorization", "Bearer" + sessionId);request.downloadHandler = new DownloadHandlerBuffer();yield return request.SendWebRequest();if (request.result != UnityWebRequest.Result.Success){Debug.Log(request.error);}elsegetDate(request.downloadHandler.text);}}/// <summary>/// Delete请求(携程)/// </summary>/// <returns></returns>public IEnumerator IeDeleteRequest(string url){using (UnityWebRequest webRequest = UnityWebRequest.Delete(url)){yield return webRequest.SendWebRequest();if (!string.IsNullOrEmpty(webRequest.error)){Debug.Log(webRequest.error);}else{Debug.Log("Okk");}}}}
本文标签: Unity 启动时带参数网页后端进行数据交互 UnityWebRequestPOSTgetdelete
版权声明:本文标题:Unity 启动时带参数,网页后端进行数据交互 UnityWebRequest ,Post,Get,Delete 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1703328267a602645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论