C# 与 JS通信
1.如果绑定在同一个游戏对象上,那么可以用GameObject.SendMessage 来通信(效率更高点的方式是用GetComponent)
C#: public void OnRecv() { print("C# Recv"); } JS: function Start () { gameObject.SendMessage("OnRecv"); } function OnRecv() { Debug.Log("js recv"); }
2.如果C# 没有继承自Monobehaviour 那么 吧C#代码放置到Plugins文件夹下,(Plugins是类似于Resources的特殊文件夹 里面的脚本会优先执行。。一些特定需要优先执行 编译的脚本 或功能就可以放置在这里,比如原声Android接口生成的jar包,等)
JS: var x:Out; function Start () { x=new Out(); x.OnRecv(); } C#: using UnityEngine; using System.Collections; public class Out { public void OnRecv() { Debug.Log("C# Recv"); } }
c# GameObject Wapper SRC Code
[ExcludeFromDocs] public void SendMessage(string methodName) { SendMessageOptions options; object obj2; options = 0; obj2 = null; this.SendMessage(methodName, obj2, options); return; } [ExcludeFromDocs] public void SendMessage(string methodName, object value) { SendMessageOptions options; options = 0; this.SendMessage(methodName, value, options); return; } public void SendMessage(string methodName, SendMessageOptions options) { this.SendMessage(methodName, null, options); return; } [MethodImpl(MethodImplOptions.InternalCall), WrapperlessIcall] public extern void SendMessage(string methodName, [DefaultValue("null")] object value, [DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);