以下脚本将添加到空游戏对象WeaponGroup
,可以使用编辑器填充该对象。我创建了一个新的游戏对象WeaponGroups
,它应该有一个脚本SetupWeaponGroupsScript
。我如何传输下面的属性,以便每个WeaponGroup
(SetupWeaponGroupsScript
将有一个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
设置以下层次结构:
答案 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;
}
}
}