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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961