在进行Unity3D 编程时,有些情况下,我们需要访问另一个不是我们正在使用的编程语言的脚本。虽然强烈推荐将所有脚本转换成一种,但是知道如何从一个JavaScript类访问一个C#脚本及反过来的情况 是很有用的。
首先要做的是将脚本放在project的正确目录。你要访问的脚本必须要放在Standard Assets 或者 Plugins目录(注:本人Unity4.2测试的时候,Unity是不允许新建 Plugins 目录的)。其他脚本放在这些目录的外面。完了之后,就可以像其他Component一样调用GetComponent() 方法。这里是一个JavaScript 例子:
//create a variable to access the C# script private var csScript : CSharp1; function Awake() { //Get the CSharp Script csScript = this.GetComponent("CSharp1"); //Don‘t forget to place the ‘CSharp1‘ file inside the ‘Standard Assets‘ folder } //...
using UnityEngine; using System.Collections; public class CSharp2 : MonoBehaviour { //create a variable to access the JavaScript script private JS1 jsScript; void Awake() { //Get the JavaScript component jsScript = this.GetComponent<JS1>(); //Don‘t forget to place the ‘JS1‘ file inside the ‘Standard Assets‘ folder } //... }
Unity3D: JavaScript->C# 或 C#->JavaScript的调用
原文:http://blog.csdn.net/wlj613613/article/details/19830147