unity中js脚本与c#脚本互相调用

unity中js脚本与c#脚本互相调用

 

test1.js

  1. function OnGUI()  
  2. {   
  3. if(GUI.Button(Rect(25,25,100,30),"JS Call CS" ))  
  4. {  
  5. var c = gameObject.GetComponent("test2");  
  6. c.PrintTest();  
  7. }  
  8. }  
  9. function testPrint()  
  10. {  
  11. print("CS Call JS");  
  12. }  

 

test2.cs

  1. using UnityEngine;  
  2. using System.Collections;  
  3. public class test2: MonoBehaviour {  
  4. void OnGUI()  
  5. {  
  6. if(GUI.Button(new Rect(25,70,100,30), "CS Call JS"))  
  7. {  
  8. test1 c = (test1)gameObject.GetComponent("test1");  
  9. c.testPrint();  
  10. }  
  11. }  
  12. void PrintTest()  
  13. {  
  14. print("JS Call CS");  
  15. }  
  16. }  

这里必须要注意的是JS文件必须是在 "StandardAssets"、 "Pro StandardAssets“和 "Plugins"这三个目录中的任何一个里,而CS文件不能与JS文件在一个目录中。原因是,这三个目录里的脚本被最先编译,"Editor"目录里的稍后编译,其他的脚本最后编译。如果在一个目录下则CS文件无法读取JS里的方法,也就无法编译通过了。而JS调用CS方法则无此限制。

posted @ 2015-11-03 13:35  IT1990  阅读(1120)  评论(0编辑  收藏  举报