js c#互相调用

這里必須要注意的是JS文件必須是在 "Standard Assets"、 "Pro Standard Assets" 和 "Plugins" 這三個目錄中的任何一個里,而CS文件不能與JS文件在一個目錄中。原因是,這三個目錄里的腳本被最先編譯,"Editor"目錄里的稍后編譯,其他的腳本最后編譯。目前Unity3D的2.5的版本似乎不支持C# 3.0,所以無法用var的關鍵字,這樣就只支持強類型,所以如果在一個目錄下則CS文件無法讀取JS里的方法,也就無法編譯通過了。而JS調用CS方法則無此限制。

function Start () {

}

function Update () {

}
function OnGUI() {
    if (GUI.Button(Rect(10,60,100,50),"js call c#")) {
        var cs = this.GetComponent("CallTest1");
        cs.Call("hello wrold from javascript");
    }
}

function CallMe(str : String)
{
    Debug.Log(str);
}
View Code
using UnityEngine;

    public class CallTest1 : MonoBehaviour {

        // Use this for initialization
        void Start () {
    
        }
    
        // Update is called once per frame
        void Update () {

    
        }

        void OnGUI(){
            if (GUI.Button(new Rect(10,10,100,50),"c# call javascript")){
                CallTest0 test0 = (CallTest0) GetComponent("CallTest0");
                test0.CallMe("call from c#");
            }
        }

        void Call(string str){
            Debug.Log(str);
        }
    }
View Code
原文地址:https://www.cnblogs.com/lansor/p/3318180.html