【问题标题】:How to get array of child objects contained in a GameObject如何获取包含在 GameObject 中的子对象数组
【发布时间】:2014-11-12 17:08:01
【问题描述】:

我在 Unity 中有一个 GameObject,它应该用作某些定义的容器。

我想访问该对象并检索Def 类实例(每个对象都有Def 通用类的实例)。

那么,如果我有一个GameObject 实例,我如何检索作为特定类实例的所有对象?

【问题讨论】:

    标签: c# unity3d gameobject


    【解决方案1】:

    您可以使用GameObject.GetComponents<Def>(); 检索GameObjectDef 类型的所有组件。
    更多信息请参阅 Unity 文档http://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        【解决方案3】:

        只要您有GameObject 引用,您就可以使用GetComponents()

        Def [] list = gameObject.GetComponents<Def>();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-16
          • 2022-01-22
          • 1970-01-01
          • 2016-03-21
          相关资源
          最近更新 更多