小编Lob*_*ite的帖子

如何返回特定类型

我正在构建一个方法,在GameObjects数组中返回一个"Type".每个GameObject都附加了一个组件,所以我在尝试这个没有结果.

private Type GetComponentOfType(string name)
{
    var component; // Error here

    for (int i = 0; i < sceneObjectsLength; i++)
    {
        component = sceneObjects[i].GetComponent(Type.GetType(name));
    }

    return component;
}
Run Code Online (Sandbox Code Playgroud)

举例说明了这个想法:

private Start()
{
    Circle circle:
    circle = GetComponentOfType("Circle");
    Debug.Log(circle.Radius());

    Square square;
    square = GetComponentOfType("Square");
    Debug.Log(square.Sides());
}
Run Code Online (Sandbox Code Playgroud)

c# unity-game-engine

0
推荐指数
1
解决办法
62
查看次数

标签 统计

c# ×1

unity-game-engine ×1