您当前的位置: 首页 >  unity

云小川

暂无认证

  • 10浏览

    0关注

    78博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3D开发之unity和js通信交互

云小川 发布时间:2022-10-19 21:05:14 ,浏览量:10

官方 方法分享:Unity - 手动:与浏览器脚本的交互 (unity3d.com)

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

 Test  内容:

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.Unity3D开发之unity和js通信交互(老版方法)

Unity3D开发之unity和js通信交互_染指流年灬的博客-CSDN博客_unity3d js

关注
打赏
1663746399
查看更多评论
立即登录/注册

微信扫码登录

0.0516s