component及刚体rigidbody用法

component及刚体rigidbody用法,第1张

component及刚体rigidbody用法 关于getcomponent函数,rigidbody(2d)的嵌套关系及用法

1.getcomponent函数

在unity中脚本可以看成是可定义的组件,我们经常要访问同一对象或不同对象中的脚本,可以运用getcomponent<>来访问其他脚本,

本例的另外两个脚本中都有公有变量,假设一个脚本名为anotherscript,另一个为Yetanotherscript。


我们需要在usinganotherscript

中访问这两个脚本。


 public  GameObject otherGameObject;
private Anotherscript anotherscript;
private YetAnotherScript yetanotherscript;
void Awake{
anotherscript=getcomponent<Anotherscript>;
yetanotherscript=otherGameObject.getcomponent<Yetanotherscript>;
}

在rigidbody中,同样可以使用此函数来绑定刚体对象

 public Rigidbody boat ;
void Start(){
boat=this.GetComponent<Rigidbody>();
}

此例先声明一个名为boat的变量,需要使用getcomponent方法来取得刚体组件的属性;

注: getcomponent方法对内存消耗较大,尽量少用,并且需要在Start(),或者Awake()中调用。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/586733.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存