如何通过Unity使用WebGL / C#在新选项卡上打开链接?

问题描述 投票:0回答:1

当前正在通过我的游戏打开链接会在相同的界面中打开该链接。我希望它在新选项卡上打开。我尝试研究插件之类的东西,以便.jslib文件与Unity项目交互,但与此同时也存在问题。我是这种交互的新手,因此在查找Plugin Inspector本身时也遇到了问题。

当前,我的代码执行此操作:

private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            Application.OpenURL("www.google.com");
        }
    }

因此,这将在同一浏览器上打开链接。我正在尝试做到这一点,以便当用户按下返回键时,他们会打开指向新浏览器的链接。

感谢您的任何帮助!

c# unity3d unity-webgl
1个回答
1
投票

一种正确的方法是使用.jslib文件

资产/插件/plugin.jslib

var plugin = {
    OpenNewTab : function(url)
    {
        url = Pointer_stringify(url);
        window.open(url,'_blank');
    },
};
mergeInto(LibraryManager.library, plugin);

您的C#脚本

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

public class OpenURL : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void OpenNewTab(string url);

    public void openIt(string url)
    {
#if !UNITY_EDITOR && UNITY_WEBGL
             OpenNewTab(url);
#endif
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Return))
        {
            openIt("www.wateverurluwant.com");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.