GameObject
基础创建与销毁
https://docs.unity.cn/cn/current/ScriptReference/GameObject.html
https://docs.unity3d.com/6000.3/Documentation/ScriptReference/GameObject.html
1. 创建 GameObject
csharp
// 1.1 创建空物体
GameObject obj = new GameObject("MyObject");
// 1.2 使用预制体创建(实例化)
public GameObject prefab;
GameObject instance = Instantiate(prefab);
// 1.3 带位置和旋转的创建
GameObject instance = Instantiate(prefab, position, rotation);
// 1.4 创建带组件的物体
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// 1.5 克隆现有物体
GameObject clone = Instantiate(originalObject);
2. 销毁 GameObject
csharp
// 2.1 立即销毁
Destroy(gameObject);
// 2.2 延迟销毁
Destroy(gameObject, 3f); // 3秒后销毁
// 2.3 销毁组件
Destroy(GetComponent<Rigidbody>());
// 2.4 销毁所有特定标签的物体
GameObject[] objects = GameObject.FindGameObjectsWithTag("Enemy");
foreach (GameObject obj in objects)
{
Destroy(obj);
}
二、查找与访问
1. 查找单个 GameObject
csharp
// 1.1 通过名称查找
GameObject player = GameObject.Find("Player");
// 1.2 通过标签查找
GameObject player = GameObject.FindWithTag("Player");
// 1.3 通过类型查找(第一个)
Camera mainCamera = GameObject.FindObjectOfType<Camera>();
// 1.4 查找所有指定类型的物体
Camera[] allCameras = GameObject.FindObjectsOfType<Camera>();
2. 层级关系查找
csharp
// 2.1 查找子物体(按名称)
Transform child = transform.Find("ChildName");
GameObject childObj = transform.Find("ChildName").gameObject;
// 2.2 查找所有子物体
foreach (Transform child in transform)
{
Debug.Log(child.name);
}
// 2.3 递归查找子物体
Transform FindDeepChild(Transform parent, string childName)
{
foreach (Transform child in parent)
{
if (child.name == childName)
return child;
Transform result = FindDeepChild(child, childName);
if (result != null)
return result;
}
return null;
}
三、组件管理
1. 获取组件
csharp
// 1.1 获取组件(如果存在)
Rigidbody rb = GetComponent<Rigidbody>();
// 1.2 强制获取组件(不存在则添加)
Rigidbody rb = GetComponent<Rigidbody>();
if (rb == null)
rb = gameObject.AddComponent<Rigidbody>();
// 1.3 扩展方法:安全获取
public static T GetOrAddComponent<T>(this GameObject obj) where T : Component
{
T component = obj.GetComponent<T>();
if (component == null)
component = obj.AddComponent<T>();
return component;
}
// 使用:gameObject.GetOrAddComponent<Rigidbody>();
// 1.4 获取子物体组件
MeshRenderer childRenderer = GetComponentInChildren<MeshRenderer>();
// 1.5 获取父物体组件
Rigidbody parentRb = GetComponentInParent<Rigidbody>();
2. 添加/移除组件
csharp
// 2.1 添加组件
Rigidbody rb = gameObject.AddComponent<Rigidbody>();
// 2.2 移除组件
Destroy(GetComponent<Rigidbody>());
// 2.3 禁用组件(而不是移除)
GetComponent<Renderer>().enabled = false;
GetComponent<Collider>().enabled = false;
3. 检查组件
csharp
// 3.1 检查是否存在组件
bool hasRigidbody = TryGetComponent<Rigidbody>(out Rigidbody rb);
// 3.2 检查多个组件
bool hasRenderer = GetComponent<Renderer>() != null;
bool hasCollider = GetComponent<Collider>() != null;
四、激活与状态管理
1. 激活状态
csharp
// 1.1 激活/禁用物体
gameObject.SetActive(true); // 激活
gameObject.SetActive(false); // 禁用(不会执行Update)
// 1.2 检查激活状态
bool isActive = gameObject.activeSelf;
bool isActiveInHierarchy = gameObject.activeInHierarchy;
// 1.3 激活层级中的父物体
transform.parent.gameObject.SetActive(true);
2. 标签管理
csharp
// 2.1 设置标签
gameObject.tag = "Player";
// 2.2 检查标签
if (gameObject.CompareTag("Player"))
{
// 玩家逻辑
}
// 2.3 安全比较标签(避免空引用)
bool isPlayer = gameObject.CompareTag("Player");
3. 层管理
csharp
// 3.1 设置层
gameObject.layer = LayerMask.NameToLayer("Player");
// 3.2 检查层
if (gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
// 敌人逻辑
}
// 3.3 批量设置子物体层
void SetLayerRecursively(GameObject obj, int layer)
{
obj.layer = layer;
foreach (Transform child in obj.transform)
{
SetLayerRecursively(child.gameObject, layer);
}
}
五、父子关系与变换
1. 父子关系操作
csharp
// 1.1 设置父物体
transform.SetParent(parentTransform);
// 1.2 保持世界坐标
transform.SetParent(parentTransform, false);
// 1.3 保持局部坐标
transform.SetParent(parentTransform, true);
// 1.4 移除父物体
transform.SetParent(null);
// 1.5 获取父物体
GameObject parent = transform.parent?.gameObject;