【发布时间】:2014-11-12 17:08:01
【问题描述】:
我在 Unity 中有一个 GameObject,它应该用作某些定义的容器。
我想访问该对象并检索Def 类实例(每个对象都有Def 通用类的实例)。
那么,如果我有一个GameObject 实例,我如何检索作为特定类实例的所有对象?
【问题讨论】:
标签: c# unity3d gameobject
我在 Unity 中有一个 GameObject,它应该用作某些定义的容器。
我想访问该对象并检索Def 类实例(每个对象都有Def 通用类的实例)。
那么,如果我有一个GameObject 实例,我如何检索作为特定类实例的所有对象?
【问题讨论】:
标签: c# unity3d gameobject
您可以使用GameObject.GetComponents<Def>(); 检索GameObject 中Def 类型的所有组件。
更多信息请参阅 Unity 文档http://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html
【讨论】:
public Def[] defArray;
public Defs gameobject; ///if you want to access from another class assign this your Defs gameobject from inspector
defs = Defs.GetComponents<Def>(); ///if you want access from another game object
defs = gameObject.GetComponents<Def>(); ///if Defs is attached to this gameObject
【讨论】:
只要您有GameObject 引用,您就可以使用GetComponents()。
Def [] list = gameObject.GetComponents<Def>();
【讨论】: