Appearance
游戏对象(游戏物体)
常用API总结
| 属性方法 | 详解 |
|---|---|
| activeInHierarchy | 游戏物体在场景中真正的激活状态 |
| activeSelf | 游戏物体在场景中设置的激活状态 |
| tag | 游戏物体的标签 |
| layer | 游戏物体的图层 |
| transform | 游戏物体的Transform组件 |
| name | 游戏物体的名称 |
| AddComponent | 添加组件 |
| GetComponent | 得到某个组件 |
| GetComponents | 得到多个组件 |
| GetComponentInChildren | 在子物体上得到组件 |
| GetComponentInParent | 在父物体上得到组件 |
| SetActive | 设置物体的激活状态 |
| ===== | ===== |
| Find | 静态方法,在场景中根据物体名称寻找游戏物体并返回 |
| FindWithTag | 静态方法,在场景中根据标签寻找游戏物体并返回 |
| FindGameObjectsWithTag | 静态方法,在场景中根据标签寻找多个游戏物体并返回 |
| Destroy | 静态方法,移除并销毁一个物体 |
| Instantiate | 静态方法,复制一个游戏物体。一般用此方法实例化预制体 |
| DontDestroyOnLoad | 静态方法,设置此游戏物体在切换场景时不会销毁 |
创建游戏对象的方法
- 使用
new GameObject("名称")直接构造csharpGameObject go = new GameObject("Player"); - 使用
Instantiate克隆预设体(Prefab)csharppublic GameObject prefab; GameObject go = Instantiate(prefab); - 使用
GameObject.CreatePrimitive创建内置几何体csharpGameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
查找游戏对象
- 使用
GameObject.Find查找场景中名称匹配的游戏物体csharpGameObject go = GameObject.Find("Player"); - 使用
GameObject.FindWithTag查找场景中标签匹配的游戏物体csharpGameObject go = GameObject.FindWithTag("Player"); - 使用
GameObject.FindGameObjectsWithTag查找场景中标签匹配的多个游戏物体csharpGameObject[]gos = GameObject.FindGameObjectsWithTag("Player"); - 使用
GameObject.FindObjectsOfType查找场景中所有指定类型的游戏物体csharpGameObject[]gos = GameObject.FindObjectsOfType<Player>();