简体   繁体   English

在Unity中将泛型用作变量类型

[英]Using generics as variable types in Unity

I wish to change the following code so I could pass a parameter of type T or Component in this function such that it can be used in the type arguments . 我希望更改以下代码,以便可以在此函数中传递类型T或Component的参数,以便可以在类型实参中使用它 This would be used in place of ButtonManager in the following code, Is this possible? 在下面的代码ButtonManager用它代替ButtonManager ,这可能吗? Could I use generics to accomplish this? 我可以使用generics来完成此操作吗? I wish to do this so I can reuse this method for a variety of Component types, vice having to make a method per component type. 我希望这样做,以便可以将这种方法重新用于多种组件类型,而不必为每种组件类型都创建一个方法。

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

You are already using the generic overload Resources.FindObjectsOfTypeAll<T>() which always returns T[] 您已经在使用通用重载Resources.FindObjectsOfTypeAll<T>() ,该重载始终返回T[]

In order to make the rest (eg GetComponent ) work you only have to make sure that T is always of type Component so you would do 为了使其余部分(例如GetComponent )正常工作,您只需要确保T始终为Component类型,就可以

public GameObject[] YourMethod<T>() where T : Component
{
    List<GameObject> validTransforms = new List<GameObject>();
    T[] objs = Resources.FindObjectsOfTypeAll<T>();
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            //objs[i].gameObject.GetComponent<T>().ConfigureInteractives();
            validTransforms.Add(objs[i].gameObject);
        }
    }
    return validTransforms.ToArray();
}

Note however that ConfigureInteractives() seems to be specificly related to your ButtonManager and will throw an exception since it is not part of Component . 但是请注意ConfigureInteractives()似乎与ButtonManager特别相关,并且由于它不是Component一部分而将引发异常。

So in case you need this for other stuff inheriting from ButtonManager then simply exchange the Component with ButtonManager in 因此,如果您需要其他继承自ButtonManager东西,则只需与ButtonManager中的Component交换ButtonManager

public void YourMethod<T>() where T : ButtonManager

How could I check the passed in Type 我如何检查传入的类型

There are multiple ways. 有多种方法。 You could eg check the exact passed type using 您可以使用例如检查确切的传递类型

if(typeof(T) == typeof(ButtonManager)) (objs[i].gameObject.GetComponent<T>() as ButtonManager).ConfigureInteractives();

but then you could also simply g back to using 但是您也可以简单地回到使用

if(typeof(T) == typeof(ButtonManager)) objs[i].gameObject.GetComponent<ButtonManager>().ConfigureInteractives();

or you could use maybe typeof(T).IsSubclassOf(typeof(ButtonManager)) for also matching inherited types. 或者您也可以使用typeof(T).IsSubclassOf(typeof(ButtonManager))来匹配继承的类型。

Sure, you can do the following: 当然,您可以执行以下操作:

public void YourFunction<T>() where T : Component
{
    T component = gameObject.GetComponent<T>();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM