按名称、标签或层查找不活动的游戏对象 [英] Find inactive GameObject by name, tag or layer

查看:25
本文介绍了按名称、标签或层查找不活动的游戏对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我需要停用一个游戏对象,然后在 10 秒后激活它,所以我认为协程是合适的:

First, I need to deactivate a game object and then after 10 seconds, activate it, so I thought coroutines are suitable:

IEnumerator BarDeactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = GameObject.Find("OBJ");
        obj.SetActive(false);
}

IEnumerator BarReactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = transform.Find("OBJ");
        obj.SetActive(true);
}

显然,我不能再使用 GameObject.Find 所以我使用 transform.Find 来查找 inactive 游戏对象,但当然.SetActive 现在不起作用,因为 obj 实际上不是游戏对象...

Obviously, I can no longer use GameObject.Find so I use transform.Find to find the inactive game object, but of course .SetActive now does not work, since obj is not actually a game object...

我如何投射找到的转换,以便可以再次将其设置为活动状态?

How do I cast the found transform, so that it can be set active again?

我试过 obj.gameObject.SetActive(true) 但肯定是错误的,因为对象不会复活...

I tried obj.gameObject.SetActive(true) but it must be wrong, because the object would not come back to life...

推荐答案

问题是 Unity 找不到不活动的 GameObjects.GameObject.Find 只会找到活动的游戏对象.您应该找到 GameObject 并将其存储在全局变量中,或者将变量设为公开,然后从编辑器分配它.

The problem is that Unity cannot find inactive GameObjects. GameObject.Find will only find active GameObject. You should either find and store the GameObject in a global variable or make the variable public then assign it from the Editor.

我的解决方案使用一个全局变量,然后将游戏对象存储在初学者中,这样您就不必再次寻找它.

My solution uses a global variable then stores the GameObject in the beginner so that you don't have to look for it again.

GameObject obj;

void Start()
{
    obj = GameObject.Find("OBJ");
}
IEnumerator BarDeactivate(float sec)
{
    yield return new WaitForSeconds(sec);
    obj.SetActive(false);
}

IEnumerator BarReactivate(float sec)
{
    yield return new WaitForSeconds(sec);
    obj.SetActive(true);
}

<小时>

以下是我制作的包装器,即使它们处于非活动状态,也可以按名称、标签或层查找游戏对象.


Below is a wrapper I made that finds GameObjects by name, tag or layer even if they are inactive.

你不应该每帧都使用这些,因为它们很慢.如果在 StartAwake 函数中使用,它们会很好.

You shouldn't be using these every frame because they are slow. They are good if used in the Start or Awake function.

找到一个游戏对象:

用法:

void Start()
{
    GameObject objByName = FindInActiveObjectByName("Cube");
    GameObject objByTag = FindInActiveObjectByTag("CubeTag");
    GameObject objByLayer = FindInActiveObjectByLayer(LayerMask.NameToLayer("CubeLayer"));
}

按名称查找不活动的游戏对象:

Find in-active GameObject by Name:

GameObject FindInActiveObjectByName(string name)
{
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].name == name)
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}

按标签查找不活动的游戏对象:

Find in-active GameObject by Tag:

GameObject FindInActiveObjectByTag(string tag)
{

    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].CompareTag(tag))
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}

按层查找不活动的游戏对象:

Find in-active GameObject by Layer:

GameObject FindInActiveObjectByLayer(int layer)
{

    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.layer == layer)
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}

<小时><小时>

查找所有游戏对象(注意以下所有函数名称中对象中的s"):



Find all GameObjects (Notice the "s" in the Object from all the function names below):

用法:

void Start()
{
    GameObject[] objByNames = FindInActiveObjectsByName("Cube");
    GameObject[] objByTags = FindInActiveObjectsByTag("CubeTag");
    GameObject[] objByLayers = FindInActiveObjectsByLayer(LayerMask.NameToLayer("CubeLayer"));
}

按名称查找不活动的 GameObject[s]:

Find in-active GameObject[s] by Name:

GameObject[] FindInActiveObjectsByName(string name)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.name == name)
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

按标签查找不活动的 GameObject[s]:

Find in-active GameObject[s] by Tag:

GameObject[] FindInActiveObjectsByTag(string tag)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.CompareTag(tag))
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

按层查找不活动的游戏对象[s]:

Find in-active GameObject[s] by Layer:

GameObject[] FindInActiveObjectsByLayer(int layer)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.layer == layer)
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

这篇关于按名称、标签或层查找不活动的游戏对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆