近期遇到一个需求,在PC版实时生成二维码,并且每个二维码代表着不同的数据,然后手机扫码用浏览器打开相应的数据,(这些数据是后期实时更新的,所以网页端不能写死),这里用到url 传参。
Unity3D打包WebGL接收URL 地址中的参数,比如接收 http://1.1.1.1:1/?1024dssawwcdffds 中的 “?1024dssawwcdffds ”
参考于: https://www.cnblogs.com/daluo/articles/5815686.html
但是他只写了核心,相信很多人看了会一脸懵逼, 下边我来做一个比较详细的介绍,
1.新建一个CS脚本 名为test 内容如下
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class Test : MonoBehaviour
{
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
public static string UrlMsg = string.Empty;
public UnityEngine.UI.Text Text;
void Start()
{
UrlMsg = "空";
try
{
UrlMsg = StringReturnValueFunction();
}
catch (System.Exception e)
{
UrlMsg = "[catch]"+e.Message;
}
Text.text = UrlMsg;
}
void Update()
{
}
}
然后挂载到你喜欢的位置,并且创建一个Text 给脚本赋值, 这里比较简单。
然后创建一个.jslib文件
内容如下:
var MyPlugin = {
StringReturnValueFunction: function()
{
var returnStr = window.location.search;
var buffer = _malloc(lengthBytesUTF8(returnStr) + 1);
writeStringToMemory(returnStr, buffer);
return buffer;
}
};
mergeInto(LibraryManager.library, MyPlugin);
放到这个位置
其他位置本人没测试,这样放置 是因为 见过的插件或者其他工程 都是这样放置的。
然后打包就可以了。
配置好IIS后 在浏览器输入你的 url Text中就会显示出来 参数