Unity3D打包WebGL接收URL 地址中的参数

近期遇到一个需求,在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中就会显示出来 参数

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
Unity3D ,可以使用 WebCamTexture 类来访问摄像头。WebCamTexture 可以从摄像头捕获视频流,并将其作为纹理传递给 Unity3D。以下是在 Unity3D 打包 WebGL 并调用手机摄像头的步骤: 1. 在 Unity3D 创建一个新的场景。 2. 在场景创建一个 Plane 对象,并将其放置在场景心。 3. 在 Inspector 窗口,将 Plane 对象的 Scale 设置为 (10, 1, 10)。 4. 在场景创建一个 Cube 对象,将其放置在 Plane 对象上方,并将其旋转 45 度。 5. 在 Cube 对象上添加一个新的脚本,并将其命名为 WebcamTextureScript。 6. 在脚本编写以下代码: ``` using UnityEngine; using System.Collections; public class WebcamTextureScript : MonoBehaviour { // The webcam texture private WebCamTexture webcamTexture; // Use this for initialization void Start () { // Get the webcam device WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length > 0) { // Create a new webcam texture webcamTexture = new WebCamTexture(devices[0].name); // Set the texture on the material GetComponent<Renderer>().material.mainTexture = webcamTexture; // Start the webcam webcamTexture.Play(); } } } ``` 7. 在 Unity3D 菜单选择 File > Build Settings。 8. 在 Build Settings 窗口,选择 WebGL 平台,并点击 Build 按钮。 9. 在生成的项目目录,找到 index.html 文件并编辑它。 10. 在文件找到以下代码: ``` <script src="Build/UnityLoader.js"></script> <script> var gameInstance = UnityLoader.instantiate("gameContainer", "Build/Build.json", {onProgress: UnityProgress}); </script> ``` 11. 在上面的代码后面添加以下代码: ``` <script> navigator.mediaDevices.getUserMedia({video: true}).then(function(stream) { var video = document.querySelector('video'); video.srcObject = stream; video.onloadedmetadata = function(e) { video.play(); }; }).catch(function(err) { console.log(err.name + ": " + err.message); }); </script> ``` 12. 保存并关闭 index.html 文件。 13. 在浏览器打开 index.html 文件,应该可以看到摄像头的视频流在 Unity3D 场景。 注意:调用摄像头需要 HTTPS 协议或 localhost 环境。如果您使用的是 localhost 环境,则需要在浏览器输入 https://localhost:port 打开项目。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值