2014-11-05 55 views
0

我想在UnityScript中定義一個靜態var。Unity3D定義靜態js var可從c#

問題是我的腳本沒有駐留在類中。

如何創建一個可以從C#類訪問的全局變量和 UnityScript腳本?

回答

0

要做的第一件事是將腳本放在項目選項卡中正確的文件夾中。您想要訪問的腳本必須位於標準資產插件文件夾內。另一個腳本必須放在這些文件夾之外。完成此步驟後,只需將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 
} 
//... 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

+0

感謝。這也幫助我http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html – amirye 2014-11-05 11:06:58