找回密碼
 注冊帳號

掃一掃,訪問微社區

component.getcomponent 獲取組件

2015-1-5 04:17| 發布者: 楊炎| 查看: 2575| 評論: 0|原作者: 蠻牛|來自: unity3d腳本manual

摘要: component.getcomponent 獲取組件functiongetcomponent(type: type) : componentdescription描述returns the component of type type if the game object has one attached, null if it doesn't.如果游戲物體有一個 ...

component.getcomponent 獲取組件

functiongetcomponent(type: type) : component

description描述

returns the component of type type if the game object has one attached, null if it doesn't.

如果游戲物體有一個附加,則返回type類型的組件,如果沒有則為null。

c# users can use a generic version.

c#用戶可以使用普通的版本。

c#

javascript

using unityengine;

using system.collections;

public class example :monobehaviour{

publictransformcurtransform;

void start() {

examplescript somescript;

somescript = getcomponent();

somescript.dosomething();

}

public void awake() {

curtransform = getcomponent();

}

}

// equivalent to:transformcurtransform = transform;

//等同于變換 curtransform = transform

var curtransform :transform;

curtransform = getcomponent (transform);

// you can access script components in the same way as other components.

//你可以像其他組件一樣訪問腳本組件

function start () {

var somescript : examplescript;

somescript = getcomponent (examplescript);

somescript.dosomething ();

}

o functiongetcomponent(type: string) : component

description描述

returns the component with name type if the game object has one attached, null if it doesn't.

如果游戲物體有一個附加,則返回名字類型組件,如果沒有則為null。

it is better to use getcomponent with a type instead of a string for performance reasons. sometimes you might not be able to get to the type however, for example when trying to access a c# script from javascript. in that case you can simply access the component by name instead of type. example:

由于性能原因,最好使用type的getcomponent,而不是用字符串。不過有時你可能無法得到type,例如當你從javascript訪問c#腳本時,這個時候你可以簡單通過名字而不是type來訪問組件。例如:

c#

javascript

using unityengine;

using system.collections;

public class example :monobehaviour{

public scriptname script;

public void awake() {

script = getcomponent("scriptname") as scriptname;

script.dosomething();

}

}

// to access public variables and functions in another script attached to the same game object.

//訪問另一個腳本附加到一樣的游戲物體的公共變量和函數

var script : scriptname;

script = getcomponent("scriptname");

script.dosomething ();

相關閱讀

文章點評
相關文章
女校游泳队彩金