Files
LowPolyBattleSim/Assets/Scripts/Editor/UnitSetupHelper.cs
Caleb Sandford deQuincey ecdd3e2a9e intial commit
2025-06-27 23:27:49 +01:00

179 lines
6.5 KiB
C#

using UnityEngine;
using UnityEditor;
using RaycastPro.Detectors;
public class UnitSetupHelper : EditorWindow
{
[MenuItem("Battle Sim/Unit Setup Helper")]
public static void ShowWindow()
{
GetWindow<UnitSetupHelper>("Unit Setup Helper");
}
private UnitData selectedUnitData;
private GameObject targetUnit;
private void OnGUI()
{
GUILayout.Label("Unit Setup Helper", EditorStyles.boldLabel);
EditorGUILayout.Space();
EditorGUILayout.HelpBox("This tool helps you set up units with the proper components for RaycastPro integration.", MessageType.Info);
EditorGUILayout.Space();
targetUnit = (GameObject)EditorGUILayout.ObjectField("Target Unit", targetUnit, typeof(GameObject), true);
selectedUnitData = (UnitData)EditorGUILayout.ObjectField("Unit Data", selectedUnitData, typeof(UnitData), false);
EditorGUILayout.Space();
if (GUILayout.Button("Setup Unit Components"))
{
SetupUnitComponents();
}
EditorGUILayout.Space();
if (GUILayout.Button("Configure All Selected Units"))
{
ConfigureSelectedUnits();
}
EditorGUILayout.Space();
EditorGUILayout.HelpBox("Setup will add:\n• UnitController\n• UnitSightSystem\n• SightDetector\n• Configure all components with UnitData", MessageType.Info);
}
private void SetupUnitComponents()
{
if (targetUnit == null)
{
EditorUtility.DisplayDialog("Error", "Please select a target unit GameObject.", "OK");
return;
}
Undo.RegisterCompleteObjectUndo(targetUnit, "Setup Unit Components");
// Add UnitController if not present
UnitController unitController = targetUnit.GetComponent<UnitController>();
if (unitController == null)
{
unitController = targetUnit.AddComponent<UnitController>();
}
// Set unit data if provided
if (selectedUnitData != null)
{
unitController.unitData = selectedUnitData;
}
// Add SightDetector if not present
SightDetector sightDetector = targetUnit.GetComponent<SightDetector>();
if (sightDetector == null)
{
sightDetector = targetUnit.AddComponent<SightDetector>();
}
// Add UnitSightSystem if not present
UnitSightSystem sightSystem = targetUnit.GetComponent<UnitSightSystem>();
if (sightSystem == null)
{
sightSystem = targetUnit.AddComponent<UnitSightSystem>();
}
// Configure components
ConfigureUnitComponents(unitController, sightDetector, sightSystem);
EditorUtility.SetDirty(targetUnit);
Debug.Log($"Successfully set up components for {targetUnit.name}");
}
private void ConfigureSelectedUnits()
{
GameObject[] selectedObjects = Selection.gameObjects;
if (selectedObjects.Length == 0)
{
EditorUtility.DisplayDialog("Error", "Please select one or more GameObjects in the scene.", "OK");
return;
}
int setupCount = 0;
foreach (GameObject obj in selectedObjects)
{
// Skip if this object doesn't seem like a unit
if (obj.GetComponent<UnitController>() == null && selectedUnitData == null)
continue;
Undo.RegisterCompleteObjectUndo(obj, "Setup Unit Components");
// Add UnitController if not present
UnitController unitController = obj.GetComponent<UnitController>();
if (unitController == null)
{
unitController = obj.AddComponent<UnitController>();
}
// Set unit data if provided
if (selectedUnitData != null)
{
unitController.unitData = selectedUnitData;
}
// Add SightDetector if not present
SightDetector sightDetector = obj.GetComponent<SightDetector>();
if (sightDetector == null)
{
sightDetector = obj.AddComponent<SightDetector>();
}
// Add UnitSightSystem if not present
UnitSightSystem sightSystem = obj.GetComponent<UnitSightSystem>();
if (sightSystem == null)
{
sightSystem = obj.AddComponent<UnitSightSystem>();
}
// Configure components
ConfigureUnitComponents(unitController, sightDetector, sightSystem);
EditorUtility.SetDirty(obj);
setupCount++;
}
Debug.Log($"Successfully set up components for {setupCount} units");
EditorUtility.DisplayDialog("Success", $"Set up components for {setupCount} units.", "OK");
}
private void ConfigureUnitComponents(UnitController unitController, SightDetector sightDetector, UnitSightSystem sightSystem)
{
if (unitController.unitData == null || sightDetector == null || sightSystem == null)
return;
UnitData unitData = unitController.unitData;
// Configure SightDetector based on UnitData
sightDetector.radius = unitData.detectionRange;
sightDetector.angleX = unitData.sightAngleX;
sightDetector.angleY = unitData.sightAngleY;
sightDetector.fullAwareness = unitData.fullAwarenessRadius;
sightDetector.minRadius = unitData.minDetectionRadius;
sightDetector.Limited = unitData.limitDetectedTargets;
if (unitData.limitDetectedTargets)
{
sightDetector.LimitCount = unitData.maxDetectedTargets;
}
// Set up basic layer masks (you may want to customize these)
sightDetector.detectLayer = LayerMask.GetMask("Default"); // Adjust as needed
sightDetector.blockLayer = LayerMask.GetMask("Obstacles"); // Adjust as needed
// Configure UnitSightSystem
sightSystem.unitController = unitController;
sightSystem.sightDetector = sightDetector;
sightSystem.enemyLayerMask = LayerMask.GetMask("Enemy"); // Adjust as needed
sightSystem.allyLayerMask = LayerMask.GetMask("Ally"); // Adjust as needed
sightSystem.obstacleLayerMask = LayerMask.GetMask("Obstacles"); // Adjust as needed
}
}