我有一个'GameObject'的序列化字段,我想自动填充要手动指定的游戏对象的子代。
如果我无法手动更改组件上的此数组,而只能以“灰色”状态显示它,则奖励积分。
可以进行此操作吗?预先感谢。
答案 0 :(得分: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;
}
就以不可编辑状态显示它是可能的,但需要为该组件编写自定义编辑器/检查器。
答案 1 :(得分: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;
}
}
}