官方 方法分享:Unity - 手动:与欣赏器脚本的交互 (unity3d.com)
- 起首必要写一个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
来源:https://blog.csdn.net/qq_37524903/article/details/127411946
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |