Unity WebGL ios 跳转URL

时间:2024-03-14 07:02:25

需求:

WebGL跳转网址

现象:

Application.OpenURL("https://www.baidu.com");

这个函数在安卓上可以用,IOS 不管用

解决方案:

编写js插件,unity调用js函数,由js跳转网址 

注意事项 :

插件后缀为.jslib ,并且放到Unity工程Plugins文件夹下

C#代码加上[DllImport]

    public void OpenURL()
    {
        JSOpenURL();
    }

    [DllImport("__Internal")]
    public static extern void JSOpenURL();
mergeInto(LibraryManager.library, {

  JSOpenURL: function () {
    //window.open("https://www.baidu.com");
    location.href = "https://www.baidu.com";
  },

  HelloString: function (str) {
    window.alert(Pointer_stringify(str));
  },

  PrintFloatArray: function (array, size) {
    for(var i = 0; i < size; i++)
    console.log(HEAPF32[(array >> 2) + i]);
  },

  AddNumbers: function (x, y) {
    return x + y;
  },

  StringReturnValueFunction: function () {
    var returnStr = "bla";
    var bufferSize = lengthBytesUTF8(returnStr) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(returnStr, buffer, bufferSize);
    return buffer;
  },

  BindWebGLTexture: function (texture) {
    GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
  },

});

高级跳转功能:

如跳转各大应用商店,唤醒APP等,暂时不写,催我就写,哈哈

IOSAppStore:

itms-apps://itunes.apple.com/us/developer/<short name>/id<your developer id> 

帮助链接:

官方文档

https://docs.unity3d.com/2021.1/Documentation/Manual/webgl-interactingwithbrowserscripting.html

JS页面跳转常用函数

https://share.ciyuanpi.cn/UnityWebGL/index.html?nickname=jiangbo&url=&avatarAssetName=wukong_t-pose&ismanghe=true