Application.ExternalCall 已过时 另一种unity脚本调用JS的方法

2 篇文章 0 订阅
1 篇文章 0 订阅

我现在用的unity2018版本的调用JS的API:Application.ExternalCall 过时了,官方给出了另一种方案:https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

现在的这种方式可以传递各种返回值,官方文档上写的很详细,我简单的再描述一下这个过程: 

  1. 首先需要写一个JS的脚本,主要是调用mergeInto();方法,第一个参数不用变,第二个参数就是JS的方法集合。写完之后将这个文件的后缀改为.jslib,放到Plugins文件夹中
mergeInto(LibraryManager.library, 
{

  Hello: function ()
  {
    window.alert("Hello, world!");
  },

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

   HelloFloat: function () 
   {
       return 1;
   },

 });

 

        2.C#脚本

using UnityEngine;
using System.Runtime.InteropServices;

public class TestJS : MonoBehaviour
{
    [DllImport("__Internal")]
    private static extern void Hello();

    [DllImport("__Internal")]
    private static extern void HelloString(string str);

    [DllImport("__Internal")]
    private static extern float HelloFloat();
    void Start()
    {
        Hello();
        HelloString("This is a string.");
    }

    void OnGUI()
    {
        GUIStyle gUIStyle = new GUIStyle();
        gUIStyle.fontSize = 20;

        float f = HelloFloat();
        GUI.Label(new Rect(500, 200, 500, 500), f.ToString(), gUIStyle);
    }
}

效果图: 

最后说一下这种方式的缺点:就是发布之后就没法在外面更改,一旦方法要改动,就只能改完之后重新发布了。。。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值