UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
GameObject.Find
# [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html).Find public static [GameObject](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.html) **Find** (string **name**); ## 描述 按 `name` 查找 GameObject,然后返回它。 此函数仅返回活动 GameObject。如果未找到具有 `name` 的 GameObject,则返回 null。如果 `name` 包含“/”字符,则会向路径名称那样遍历此层级视图。 出于性能原因,建议不要每帧都使用此函数,而是在启动时将结果缓存到成员变量中,或者使用 [GameObject.FindWithTag](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.FindWithTag.html)。 **注意:** 如果您要查找子 GameObject,使用 [Transform.Find](https://docs.unity3d.com/cn/2019.4/ScriptReference/Transform.Find.html) 通常会更加轻松。 **注意:** 如果正在使用多个场景运行此游戏,则 [Find](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.Find.html) 将在所有这些场景中进行搜索。 ``` using UnityEngine; using System.Collections; // This returns the GameObject named Hand in one of the Scenes. public class ExampleClass : MonoBehaviour { public GameObject hand; void Example() { // This returns the GameObject named Hand. hand = GameObject.Find("Hand"); // This returns the GameObject named Hand. // Hand must not have a parent in the Hierarchy view. hand = GameObject.Find("/Hand"); // This returns the GameObject named Hand, // which is a child of Arm > Monster. // Monster must not have a parent in the Hierarchy view. hand = GameObject.Find("/Monster/Arm/Hand"); // This returns the GameObject named Hand, // which is a child of Arm > Monster. hand = GameObject.Find("Monster/Arm/Hand"); } } ``` [GameObject.Find](https://docs.unity3d.com/cn/2019.4/ScriptReference/GameObject.Find.html) 有助于在加载时自动连接对其他对象的引用;例如在 [MonoBehaviour.Awake](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.Awake.html) 或 [MonoBehaviour.Start](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.Start.html) 内。 出于性能原因,建议不要每帧都使用此函数。 常见模式是将 GameObject 分配到 [MonoBehaviour.Start](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.Start.html) 内的变量,然后在 [MonoBehaviour.Update](https://docs.unity3d.com/cn/2019.4/ScriptReference/MonoBehaviour.Update.html) 中使用此变量。 ``` using UnityEngine; using System.Collections; // Find the GameObject named Hand and rotate it every frame 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); } } ```
da
2022年5月26日 18:03
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码