This commit is contained in:
yulong 2024-01-08 16:24:56 +08:00
commit 36d8db289d
1 changed files with 52 additions and 1 deletions

View File

@ -136,12 +136,18 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
}
unmannedAerialVehicleManages.Add(this);
equipmentCommon = GetComponent<EquipmentCommon>();
Formation(1);//默认阵型
//Formation(1);//默认阵型
// 订阅布尔值变化事件
OnActivationChanged += OnActivationChangedHandler;
}
[ContextMenu("Test")]
public void Test()
{
MatrixFormation(30, 6);//根据想定数据去设置无人机数量
}
// Update is called once per frame
void Update()
{
@ -467,6 +473,8 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
}
/// <summary>
/// 无人机雁式阵型
/// </summary>
@ -504,6 +512,8 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
}
}
/// <summary>
/// 矩阵阵型
/// </summary>
@ -537,6 +547,47 @@ public class UnmannedAerialVehicleManage : MonoBehaviour
}
}
/// <summary>
/// 根据想定数据去设置无人机数量
/// </summary>
private void MatrixFormation(int rows, int wrjCount)
{
for (int i = 0; i < unmannedAerialVehicles.Count; i++)
{
unmannedAerialVehicles[i].gameObject.SetActive(false);
}
for (int i = 0; i < wrjCount; i++)
{
unmannedAerialVehicles[i].gameObject.SetActive(true);
}
float offsetX = 2.0f; // 子物体之间的水平间距
float offsetY = 2.0f; // 子物体之间的垂直间距
float offsetZ = 2.0f; // 子物体之间的垂直间距
int currentCount = 0;
int cols = CanDivideEvenly(totalObjects, rows) ? totalObjects / rows : totalObjects / rows + 1;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
if (currentCount < totalObjects)
{
Vector3 position = new Vector3(col * offsetX, 0, -row * offsetZ);
if (unmannedAerialVehicles[currentCount])
unmannedAerialVehicles[currentCount].transform.localPosition = position;
currentCount++;
}
}
}
BoxCollider box = transform.GetComponent<BoxCollider>();
if (box)
{
Debug.Log("cols..:" + cols);
Debug.Log("rows..:" + rows);
box.center = new Vector3(cols - 1, 0, -(wrjCount - 1));
box.size = new Vector3(cols * 2, 1, 2 * wrjCount);
}
}
bool CanDivideEvenly(int dividend, int divisor)
{
return dividend % divisor == 0;