Unity WebGL获取地址栏参数

1、在Assets/Plugins/WebGL文件夹下创建MyPlugin.jslib文件
文件内容:

var MyPlugin = {
    
    
  StringReturnValueFunction: function () {
    
    
    var returnStr = window.top.location.href;
    var bufferSize = lengthBytesUTF8(returnStr) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(returnStr, buffer, bufferSize);
    return buffer;
  }
};
 
mergeInto(LibraryManager.library, MyPlugin);

2、创建脚本显示地址

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    
    
    [DllImport("__Internal")]
    private static extern string StringReturnValueFunction();

    public Text text;
    void Start()
    {
    
    
        string UrlMsg = "地址参数";
        try
        {
    
    
            UrlMsg = StringReturnValueFunction();
        }
        catch (System.Exception e)
        {
    
    
            UrlMsg = "[catch]"+e.Message;
        }
        text.text = UrlMsg;
    }
}

3、BuildAndRun或者打包后用IIS部署一个服务器运行就行了

猜你喜欢

转载自blog.csdn.net/weixin_38359813/article/details/128533674