Version: 2017.2

Object.FindObjectOfType

切换到手册
public static Object FindObjectOfType (Type type);

参数

type 要查找的对象类型。

返回

Object An array of objects which matched the specified type, cast as Object.

描述

返回第一个类型为 type 的已加载的激活对象。

请注意,该函数的运行速度非常缓慢。不建议对每一帧都使用该函数。 在大多数情况下,您可以改为使用单例模式。

See Also: Object.FindObjectsOfType.

using UnityEngine;
using System.Collections;

// Search for any object of Type GUITexture, // if found print its name, else print a message // that says that it was not found. public class ExampleClass : MonoBehaviour { void Start() { GUITexture texture = (GUITexture)FindObjectOfType(typeof(GUITexture)); if (texture) Debug.Log("GUITexture object found: " + texture.name); else Debug.Log("No GUITexture object could be found"); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961