Unity3D 里 C# 和JS互相访问方法

js脚本

var obj:GameObject=GameObject.Find("csharpobj");
function OnGUI()
{ 
    if(GUI.Button(Rect(25,25,100,30),"JS Call CS" )) 
    { 
        var c = obj.GetComponent("test2"); c.PrintTest(); 
     }
}
function testPrint(){
    print("CS Call JS");
}

 

 

c#脚本,注:c#脚本赋给csharpobj物体
 
 
using UnityEngine;
using System.Collections;

public class test2: MonoBehaviour {

    void OnGUI()
    {
        if(GUI.Button(new Rect(25,70,100,30), "CS Call JS"))
        {
            test1 c = (test1)gameObject.GetComponent("test1");
            c.testPrint();
        }
    }

    void PrintTest()
    {
        print("JS Call CS");
    }
}
 
 
 

你可能感兴趣的:(c,function,C#,脚本,Class)