返回介绍

GameObject.Find 查找

发布于 2019-12-18 15:37:42 字数 3423 浏览 1065 评论 0 收藏 0

JavaScript => static function Find(name: string): GameObject;
C# => static GameObject Find(string name);

Description 描述

Finds a game object by /name/ and returns it.

找到并返回一个名字为name的游戏物体。

If no game object with /name/ can be found, null is returned. If /name/ contains a '/' character it will traverse the hierarchy like a path name. This function only returns active gameobjects.

如果以name为名字的游戏物体没有被找到,则返回空.如果name中包含'/'字符,这个名称将被视作一个hierarchy中的路径名.这个函数只返回活动的游戏物体。

For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.

除非迫不得已,建议不要在每一帧中使用这个函数。可以在开始的时候用一个成员变量来缓存结果或者使用GameObject.FindWithTag函数。

JavaScript:

var hand : GameObject;
// This will return the game object named Hand in the scene.
//这将返回名为Hand 的游戏物体
hand = GameObject.Find("Hand");
 
// This will return the game object named Hand.
// Hand may not have a parent in the hierarchy view!
//这将返回名为Hand 的游戏物体,在层次视图中hand也没有父级物体
hand = GameObject.Find("/Hand");
 
// This will return the game object named Hand,
// which is a child of Arm -> Monster.
// Monster may not have a parent in the hierarchy view!
//这将返回Monster/Arm 的子级下名为Hand 的游戏物体,在层次视图中Monster也没有父级物体
hand = GameObject.Find("/Monster/Arm/Hand");
 
// This will return the game object named Hand,
// which is a child of Arm -> Monster.
// Monster may have a parent.
//这将返回Monster/Arm 的子级下名为Hand 的游戏物体,在层次视图中Monster可以有父级物体
hand = GameObject.Find("Monster/Arm/Hand");

C#:

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");
    }
}

This function is most useful to automatically connect references to other objects at load time, eg. inside MonoBehaviour.Awake or MonoBehaviour.Start. You should avoid calling this function every frame eg. MonoBehaviour.Update for performance reasons. A common pattern is to assign a game object to a variable inside MonoBehaviour.Start. And use the variable in MonoBehaviour.Update.

JavaScript:

// Find the hand inside Start and rotate it every frame
// 在Start里查找hand物体,并在每帧旋转它
private var hand : GameObject;
function Start () {
	hand = GameObject.Find("/Monster/Arm/Hand");
}
 
function Update () {
	hand.transform.Rotate(0, 100 * Time.deltaTime, 0);
}

C#:

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);
    }
}

GameObject

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文