打包后启动.exe带参数
启动传参数可以参考这篇文章: 浏览器调用本地exe(应用程序)方法
Unity准备工作
需要用到这个API
Environment.GetCommandLineArgs()
官方解释:返回包含当前进程的命令行参数的字符串数组
返回值是一个string[],当Length>1的时候就是带参数,注意:在UnityEditor模式也就是编辑器模式会有默认参数返回,Length是大于1的
UnityWebRequest API使用
常用的就是Get与Post,代码比较简单,有的地方传参时需要注意有没有RequestHeader,这个需要进行特殊处理文章来源:https://www.toymoban.com/news/detail-573997.html
代码
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);
}
else
getDate(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);
}
else
getDate(request.downloadHandler.text);
}
}
其中GetDate getDate是一个委托,用来接收返回的数据文章来源地址https://www.toymoban.com/news/detail-573997.html
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
{
//解析各种ID
testData = 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);
}
else
getDate(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);
}
else
getDate(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 启动时带参数,网页后端进行数据交互 UnityWebRequest ,Post,Get,Delete的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!