如何通过给定层次结构中的检查器访问预制件阵列

时间:2017-06-16 03:11:37

标签: c# unity3d unity5

以下脚本将添加到空游戏对象WeaponGroup,可以使用编辑器填充该对象。我创建了一个新的游戏对象WeaponGroups,它应该有一个脚本SetupWeaponGroupsScript。我如何传输下面的属性,以便每个WeaponGroupSetupWeaponGroupsScript将有一个WeaponGroup个对象的数组)的设置方式与下面的方法相似,这样我就可以SetupWeaponsScript隐藏给检查员的属性,并通过SetupWeaponGroupsScript填充它们?

public class SetupWeaponsScript {

// Here's our tuple combining a weapon prefab and a direction.
[System.Serializable]
public struct DirectedWeapon {
    public WeaponScript weapon;
    public WeaponScript.Direction direction;
}

// The prefabs array is now this tuple type.
public DirectedWeapon[] weaponPrefabs;
public WeaponScript[] weapons;

void Start ()
{  
    weapons = new WeaponScript[weaponPrefabs.Length];

    for (int i = 0; i < weaponPrefabs.Length; i++)
    {
        // Using typed Instantiate to save a GetComponent call.
        weapons[i] = Instantiate<WeaponScript>(weaponPrefabs[i].weapon);
        weapons[i].weaponDir = weaponPrefabs[i].direction;
    }      
}  
}

从图形上看,我希望在编辑器中为新脚本SetupWeaponGroupsScript设置以下层次结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

这应该是关闭的:

   public class SetupWeaponsScript
    {
        [System.Serializable]
        public struct DirectedWeaponGroup
        {
            public DirectedWeaponScript[] weaponPrefabs;
        }

        // Here's our tuple combining a weapon prefab and a direction.
        [System.Serializable]
        public struct DirectedWeapon
        {
            public WeaponScript weapon;
            public WeaponScript.Direction direction;
        }

        // The prefabs array is now this tuple type.

        public DirectedWeaponGroup[] weaponGroups;

        public DirectedWeapon[] weaponPrefabs;
        public WeaponScript[] weapons;

        void Start()
        {
            weaponGroups = new DirectedWeaponScript[weaponGroups.Length];

            for (int h = 0; h <  weaponGroups.Length; h++)
            {
                weaponPrefabs = weaponGroups[h].weaponPrefabs;
                weaponPrefabs[h] = Instantiate<DirectedWeaponScript>(weaponGroups[h].weaponPrefabs);

                weapons = new WeaponScript[weaponPrefabs.Length];

                for (int i = 0; i < weaponPrefabs.Length; i++)
                {
                    // Using typed Instantiate to save a GetComponent call.
                    weapons[i] = Instantiate<WeaponScript>(weaponPrefabs[i].weapon);
                    weapons[i].weaponDir = weaponPrefabs[i].direction;
                }
            }
        }