与网页端具体的交互参考我的例外一篇博客:传送门
Unity与浏览器交互的官方地址
Unity版本:Unity2021.2.15f1
.jslib的代码内容:
mergeInto(LibraryManager.library, {
GetToken: function () {
var url = window.location.href;
var temp = url.split('?')[1];
var pram = new URLSearchParams('?' + temp);
var tookenStr = pram.get('token');
var bufferSize = lengthBytesUTF8(tookenStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(tookenStr, buffer, bufferSize);
return buffer;
},
});
注意:代码中添加注释会打包报错,打包的文件夹会缺少文件,具体是什么原因,我也不清楚。
报错内容:Building Library\Bee\artifacts\WebGL\build\debug_WebGL_wasm\build.js failed with output:
C#代码
using System.Runtime.InteropServices;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[DllImport("__Internal")]
private static extern string GetToken();
void Start()
{
string str = GetToken();
Debug.Log("unity得到的token值: " + str);
}
}