-1

我有一个序列化的“GameObject”字段,我想自动填充我想手动指定的游戏对象的子项。

如果我无法手动更改组件上的此数组,而只能将其显示为“灰显”状态,则可以加分。

这个操作可以吗?提前致谢。

4

2 回答 2

0

那会有帮助吗?

GameObject[] myArray;
GameObject parent;

void GetChildren()
{

    myArray=new GameObject[parent.transform.childCount];
    for (int i=0;i<parent.transform.childCount;i++)
    myArray[i]=parent.transform.GetChild(i).gameObject;

}

至于以不可编辑状态显示它 - 它可能但需要为该组件编写自定义编辑器/检查器。

于 2018-06-29T15:36:35.967 回答
0

解决方案:

[CustomEditor( typeOf( ClassYouWantToManage ) )]
class ClassYouWantToManageEditor
{
  ClassYouWantToManage value;

  void OnEnable()
  {
   // target is the upcasted version of your managed class
   value = (ClassYouWantToManage) target;
  }

  public override void OnInspectorGUI()
  {
    // specifiedObject is the 'parent', with whos children you want to populate the array. has to be public
    if (value.specifiedObject == null)
    {
      throw new Exception ("Specified object is null");
    }

    // targetArray is the array you want to populate with children. has to be public
    // simple population alg
    value.targetArray = new GameObject[specifiedObject.transform.ChildCount];
    for (int i = 0; i < specifiedObject.transform.ChildCount; i++)
    {
      value.targetArray[ i ] = specifiedObject.transform.GetChild( i ).gameObject;
    } 
  }
}  
于 2018-06-29T16:48:50.673 回答