我GameObject
在 Unity 中有一个应该用作某些定义的容器。
我想访问该对象并检索Def
类实例(每个对象都有Def
通用类的实例)。
那么,如果我有一个GameObject
实例,如何检索作为特定类实例的所有对象?
我GameObject
在 Unity 中有一个应该用作某些定义的容器。
我想访问该对象并检索Def
类实例(每个对象都有Def
通用类的实例)。
那么,如果我有一个GameObject
实例,如何检索作为特定类实例的所有对象?
您可以使用GameObject.GetComponents<Def>();
检索Def
中的所有类型组件GameObject
。
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>();