佚枫吧 关注:14贴子:2,987
  • 1回复贴,共1

转贴:unity中js脚本与c#脚本互相调用 - xqan的专栏 - 博客频道 -...

只看楼主收藏回复

学习~

unity中js脚本与c#脚本互相调用 - xqan的专栏 - 博客频道 - CSDN.NET

来自:blog.csdn.net



1楼2014-04-24 15:17回复
    unity中js脚本与c#脚本互相调用
    test1.js
    function OnGUI()
    {
    if(GUI.Button(Rect(25,25,100,30),"JS Call CS" ))
    {
    var c = gameObject.GetComponent("test2");
    c.PrintTest();
    }
    }
    function testPrint()
    {
    print("CS Call JS");
    }
    test2.cs
    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");
    }
    }
    这里必须要注意的是JS文件必须是在 "StandardAssets"、 "Pro StandardAssets“和 "Plugins"这三个目录中的任何一个里,而CS文件不能与JS文件在一个目录中。原因是,这三个目录里的脚本被最先编译,"Editor"目录里的稍后编译,其他的脚本最后编译。如果在一个目录下则CS文件无法读取JS里的方法,也就无法编译通过了。而JS调用CS方法则无此限制。


    2楼2014-04-24 15:19
    回复