GameObject.FindObjectOfType()

纯粹是为了记载下来防止忘记,没什么技术含量。顾名思义,该函数就是按照游戏类型来查找游戏对象。

首先来看Unity Manual手册:https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html

public static Object FindObjectOfType(Type type);

typeThe type of object to find.

Returns

Object This returns the Object that matches the specified type. It returns null if no Object matches the type

Description

Returns the first active loaded object of Type type.

::ref::.FindObjectOfType will return no Assets (meshes, textures, prefabs, ...) or inactive objects. It is used to locate a GameObject This does not return an Object that has HideFlags.DontSave set.

This method calls Object.FindObjectOfType and returns the object that matches the type and null if no object matches the type.

Please note that this function is very slow. It is not recommended to use this function every frame. In most cases you can use the singleton pattern instead.

 意思就是返回和type一样的被激活的物体。至于顺序可以看此博客:

https://www.cnblogs.com/Peng18233754457/p/8966458.html

手册里给出的示例代码GUITexture已经被弃用,所以此处实验代码如下:

using UnityEngine;
using System.Collections;

// Search for any object of Type ScriptExample ,
// if found print its name, else print a message
// that says that it was not found.
public class ExampleClass : MonoBehaviour
{
    void Start()
    {
        ScriptExample se = GameObject.FindObjectOfType<ScriptExample>();
        if (se)
            Debug.Log("ScriptExample object found: " + se.name);
        else
            Debug.Log("No ScriptExample object could be found");
    }
}

 其中这两个cs文件都挂载到了Camera上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

keneyr

老爷~给小的赏点盘缠吧555~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值