component.getcomponent 获取组件

2015-1-5 04:17| 发布者: 杨炎| 查看: 2259| 评论: 0|原作者: 蛮牛|来自: yzc88亚洲城脚本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 ();

相关阅读

文章点评
相关文章