新建一个场景。
新建一个csScript.cs脚本
using UnityEngine;
using System.Collections;
public class csScript : MonoBehaviour {
void OnGUI()
{
if(GUI.Button(new Rect(100,170,200,100),"C# call JS"))
{
jsScript js = (jsScript)GetComponent("jsScript");
js.CallMe("I came from C#");
}
}
public void CallMe(string str)
{
Debug.Log(str);
}
}
在Standard Assets目录下新建一个jsScript脚本
#pragma strict
function OnGUI()
{
if(GUI.Button(Rect(100,50,200,100),"JS call C#"))
{
var cs = this.GetComponent("csScript");
cs.CallMe("I came from JS");
}
}
function CallMe(test:String)
{
Debug.Log(test);
}
两个脚本都附加给Main Camera
运行后点击按钮,看Console