Version: 2017.1

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