Unity发布webgl获取浏览器的URL

Unity发布webgl获取浏览器的URL

Unity发布webgl之后获取浏览器的url
在unity中创建文件夹Plugins,然后添加添加文件UnityGetBrowserURL.jslib

var GetUrlFunc = {
   
  // 获取地址栏的URL(当前页面URL)
  GetCurrentUrl: function () {
    var returnStr = window.location.href;
    var title = decodeURI(returnStr);
    var bufferSize = lengthBytesUTF8(title) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(title, buffer, bufferSize);
    return buffer;
  },

  // 获取浏览器顶部的URL(顶级窗口URL)
  GetTopUrl: function () {
    var returnStr = window.top.location.href;
    var title = decodeURI(returnStr);
    var bufferSize = lengthBytesUTF8(title) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(title, buffer, bufferSize);
    return buffer;
  }
  
};

mergeInto(LibraryManager.library, GetUrlFunc);

在Unity中添加代码

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

public class NewBehaviourScript : MonoBehaviour
{
    // 导入 JavaScript 方法
    [DllImport("__Internal")]
    private static extern string GetCurrentUrl(); // 获取当前页面的 URL

    [DllImport("__Internal")]
    private static extern string GetTopUrl(); // 获取顶级窗口的 URL

    public string currentUrl = ""; // 当前页面的 URL
    public string topUrl = ""; // 顶级窗口的 URL

    public Button btnGetCurrentUrl; // 获取当前页面 URL 的按钮
    public Button btnGetTopUrl; // 获取顶级窗口 URL 的按钮
    public Text txtCurrentUrl; // 显示当前页面 URL 的文本框
    public Text txtTopUrl; // 显示顶级窗口 URL 的文本框

    private void Start()
    {
        // 初始化时获取 URL
        GetCurrentURL();
        GetTopURL();

        // 为按钮添加点击事件
        btnGetCurrentUrl.onClick.AddListener(() =>
        {
            GetCurrentURL();
        });

        btnGetTopUrl.onClick.AddListener(() =>
        {
            GetTopURL();
        });
    }

    // 获取当前页面的 URL
    private void GetCurrentURL()
    {
#if UNITY_WEBGL && !UNITY_EDITOR
        currentUrl = GetCurrentUrl();
        txtCurrentUrl.text = currentUrl;
        Debug.Log("Current URL: " + currentUrl);
#else
        txtCurrentUrl.text = "Not running in WebGL";
        Debug.Log("Not running in WebGL");
#endif
    }

    // 获取顶级窗口的 URL
    private void GetTopURL()
    {
#if UNITY_WEBGL && !UNITY_EDITOR
        topUrl = GetTopUrl();
        txtTopUrl.text = topUrl;
        Debug.Log("Top URL: " + topUrl);
#else
        txtTopUrl.text = "Not running in WebGL";
        Debug.Log("Not running in WebGL");
#endif
    }

        /// <summary>
        /// 如果URL中有参数可以进行参数的解释,
        /// </summary>
        /// <param name="queryString">查询字符串</param>
        /// <returns>键值对字典key是参数key,value是参数值</returns>
        public Dictionary<string, string> ParseQueryString(string queryString)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            // 使用正则表达式匹配键值对
            string pattern = @"([^&=]+)=([^&]*)";
            MatchCollection matches = Regex.Matches(queryString, pattern);

            foreach (Match match in matches)
            {
                string key = Uri.UnescapeDataString(match.Groups[1].Value); // 解码键
                string value = Uri.UnescapeDataString(match.Groups[2].Value); // 解码值
                result[key] = value;
            }

            return result;
        }

}

场景布局
在这里插入图片描述
发布webgl
在这里插入图片描述
拷贝到nginx
在这里插入图片描述

运行结果
在这里插入图片描述

在地址栏输入Ip:端口号/?serligblsdhroivbaelirbgvkersab ,/?后面是随便打的字符,然后按下enter会刷新网页并重新显示URL,(显示的文字和在编辑器下显示的文字不一致的原因是:webgl不支持unity自带的字体中的中文,换个其他的字体就行了)
在这里插入图片描述

Enjoy

资源可以私信获取,有不明白的欢迎私信留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值