作者:手机用户2502923017 | 来源:互联网 | 2023-05-17 12:28
我蠢怪我咯︿( ̄︶ ̄)︿Unity中常用类GameObjectComponent脚本要成为一个组件才能运行一、组件之间进行访问方法一使用这个this对象可以访
我蠢怪我咯︿( ̄︶ ̄)︿
Unity中常用类
GameObject
Component
脚本要成为一个组件才能运行
一、组件之间进行访问
- 方法一使用这个this对象可以访问到父类(MonoBehavirou)里面的一些默认的系统组件(这个组件不存在的话,会返回null)
this.transfrom
第二种访问其他组件的方式是通过定义一个public变量,然后在Inspecotr面板中选择或者手动拖放指定
第三种访问其他组件的方法,首先得到这个组件所在的GameObject,然后通过GetComponent方法取得这个GameObject上的组件
Transform t = cube1.GetComponent<Transform>();
print (t.position);
二、如何查找GameObject
1、直接拖拽赋值
public GameObject Player
2、查找
GameObject.Find(保证名字是唯一的)最耗时
GameObject go = GameObject.Find("Player")
GameObject.FindGameObjectWithTag(查找相同标签)
GameObject[] goEs = GameObject.FindGameObjectWithTag("Player")
GameObject.FindWithTag()
三、Component和常用操作
得到Transform组件,移动位置
得到游戏物体
GameObject go;
得到Transform组件
Transform tran = go.transform;
进行移动操作
得到Component
GetComponent//可以在一个GameObject上调用go.GetCOm… 也可以在一个脚本对象(组件对象)上直接调用tranfrom.GetCOm…
查找的范围都是这个GameObject上挂在的所有脚本(组件)
GetComponentInChildren
GetComponentInParent
GetComponents
GetComponentsInChildren
GetComponentsInParent
GUITexture和GUIText