# 【获取网页url参数】
用到了 WebGL:与浏览器脚本交互 (opens new window) 的技术。
GetWebUrlScript.cs:
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class GetWebUrlScript
{
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
//UrlMsg的数据是 ?a=100&b=vp666
public static string UrlMsg = string.Empty;
// 参数字典
public static Dictionary<string, string> paramsMap = new Dictionary<string, string>();
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void InitUrlData()
{
#if !UNITY_EDITOR&&UNITY_WEBGL
UrlMsg = StringReturnValueFunction();
UrlMsg = UrlMsg.Substring(1);//去掉问号
string[] pArr = UrlMsg.Split('&');
for (int i = 0; i < pArr.Length; i++)
{
string[] tArr = pArr[i].Split('=');
string key = tArr[0];
string value = tArr[1];
paramsMap[key] = value;
}
#endif
}
}
Plugins/My.jslib
var MyPlugin = {
StringReturnValueFunction: function()
{
var returnStr = window.location.search;
var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
stringToUTF8(returnStr,buffer,lengthBytesUTF8(returnStr) + 1);
return buffer;
}
};
mergeInto(LibraryManager.library,MyPlugin);