private void OnGUI()
{/在场景中物体激活状态(物体实际激活状态)
this.game0bject.activelnHierarchy
1/物体自身激活状恋(物体在Inspector面板中的状恋)
this.gameObject.activeSelf
/没置物体后用/禁用
this.game0bject.SetAstive()
}
GameObject.AddComponent将类型为 componentType 的组件类添加到该游戏对象
using UnityEngine;
using System.Collections;
public class AddComponentExample : MonoBehaviour
{
void Start()
{
SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider;
}
}
using UnityEngine;
using System.Collections;
public class GameObjectDemo : MonoBehaviour
{
public GameObject targetGO;
private void OnGUI()
{
if (GUILayout.Button("代码创建红色的聚光灯"))
{
//创建游戏对象
GameObject go = new GameObject("MyLight");
//添加灯光组件(创建Ligth类型对象 将引用添加到游戏对象列表中)
Light light = go.AddComponent<Light>();
light.type = LightType.Spot;
light.color = Color.red;
}
if (GUILayout.Button("启用/禁用指定物体"))
{
targetGO.SetActive(!targetGO.activeInHierarchy);
}
if (GUILayout.Button("FindGameObjectsWithTag"))
{
//获取所有敌人
var allEnemy = GameObject.FindGameObjectsWithTag("Enemy");
var playerGO = GameObject.FindWithTag("Player");
}
if (GUILayout.Button("FindObjectsOfType"))
{
//无论当前组件位于哪个物体,都可以被查找到
var allRenderer = FindObjectsOfType<MeshRenderer>();
}
}
}
1/在场景中根据名称查找物体(慎用)
GameObject.Find("游戏对象名称")
1/获取所有使用该标签的物体
GameObject[] allEnemy = GameObject.FindGameObjectsWithTag("Enemy")
1/获取单个使用该标签的物体
GameObject playerGO = GameObject.FindWithTag("Player");
//Object
根据类型査找対象
Object.FindObjectOfType <MeshRenderer>();
FindObjectsOfType <MeshRenderer>();
銷毀対象
Object.Destroy
GameObject 类
-- Variables
activeInHierarchy :理解为物体实际激活状态(物体自身被禁用或者父物体被禁用都为false)
activeSelf:物体在Inspector面板激活状态
一个物体被禁用,可能是自己的勾被取消了,可能是自己的父亲的勾被取消了,
layer
-- Public Functions
AddComponent:为游戏对象添加组件
GameObject.SetActive:根据给定的值 true
或 /false/,激活/停用 GameObject。
-- Static Functions
* Find :根据名称查找游戏对象(慎用,如同大海捞针)
* FindGameObjectsWithTag :获取使用指定标签的所有物体
* FindWithTag:获取使用标签的一个物体
*
* Object 类 Static Functions