Unity3D定义了来自c#的静态js var

时间:2014-11-05 09:37:49

标签: unity3d

我正在尝试在UnityScript中定义一个静态var。

问题是我的脚本不在一个类中。

如何创建可以从C#类访问的全局变量 UnityScript脚本?

1 个答案:

答案 0 :(得分:0)

要做的第一件事是将脚本放在“项目”选项卡中的正确文件夹中。您要访问的脚本必须位于标准资源插件文件夹中。另一个脚本必须放在这些文件夹之外。完成此步骤后,只需将 GetComponent()方法调用为任何其他Component。这是一个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  
}  
//... code here

现在,这是一个C#示例:

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  
    }  
//...  
}

这就是它的完成方式。由于其中一个脚本必须位于标准资产插件文件夹,因此无法同时访问C#和JavaScript。放置在其中一个文件夹中的脚本首先被编译,这意味着这些脚本无法访问它们之外的脚本,因为它们尚未编译(更多信息here)。

有一个带有示例here

的项目
相关问题