Unity3D开发之unity和js通信交互

官方 方法分享: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

猜你喜欢

转载自blog.csdn.net/qq_37524903/article/details/127411946