Version: 2017.3
public static GameObject Find (string name);

描述

name 查找 GameObject,然后返回它。

此函数仅返回活动 GameObject。如果未找到具有 name 的 GameObject,则返回 null。如果 name 包含“/”字符,则会向路径名称那样遍历此层级视图。

出于性能原因,建议不要每帧都使用此函数,而是在启动时将结果缓存到成员变量中,或者使用 GameObject.FindWithTag

Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject hand; void Example() { hand = GameObject.Find("Hand"); hand = GameObject.Find("/Hand"); hand = GameObject.Find("/Monster/Arm/Hand"); hand = GameObject.Find("Monster/Arm/Hand"); } }

GameObject.Find is useful for automatically connecting references to other objects at load time; for example, inside MonoBehaviour.Awake or MonoBehaviour.Start.

出于性能原因,建议不要每帧都使用此函数。

A common pattern is to assign a GameObject to a variable inside MonoBehaviour.Start, and use the variable in MonoBehaviour.Update.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { private GameObject hand; void Start() { hand = GameObject.Find("/Monster/Arm/Hand"); } void Update() { hand.transform.Rotate(0, 100 * Time.deltaTime, 0); } }