首页 > 数据库技术 > 详细

component及刚体rigidbody用法

时间:2017-10-25 20:26:54      阅读:331      评论:0      收藏:0      [点我收藏+]

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

1.getcomponent函数

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

本例的另外两个脚本中都有公有变量,假设一个脚本名为anotherscript,另一个为Yetanotherscript。我们需要在usinganotherscript

中访问这两个脚本。

1 public  GameObject otherGameObject;
2 private Anotherscript anotherscript;
3 private YetAnotherScript yetanotherscript;
4 void Awake{
5 anotherscript=getcomponent<Anotherscript>;
6 yetanotherscript=otherGameObject.getcomponent<Yetanotherscript>;
7 }

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

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

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

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

component及刚体rigidbody用法

原文:http://www.cnblogs.com/HiWord-ToFutureGKY/p/7731936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!