using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
 
public static class LightProbeGroupMerger
{
    const string MergedLightProbeGroupName = "MergedLightProbeGroup";
 
    [MenuItem("Tools/Lights/Merge Light Probe Groups")]
    static void MergeLightProbeGroups()
    {
        var lightProbeGroups = Object.FindObjectsOfType<LightProbeGroup>();
        if (lightProbeGroups.Length <= 1) return;
        // Collect light probe positions from all light probe groups in the scene.
        var mergedLightProbePositions = new List<Vector3>();
        foreach (var lightProbeGroup in lightProbeGroups)
        {
            foreach (var lightProbePosition in lightProbeGroup.probePositions)
            {
                mergedLightProbePositions.Add(lightProbeGroup.transform.TransformPoint(lightProbePosition));
            }
        }
 
        // Create a new light probe group that merges all the light probe positions.
        var newGameObject = new GameObject(MergedLightProbeGroupName);
        Undo.RegisterCreatedObjectUndo(newGameObject, $"Created {MergedLightProbeGroupName}");
        var mergedLightProbeGroup = newGameObject.AddComponent<LightProbeGroup>();
        mergedLightProbeGroup.probePositions = mergedLightProbePositions.ToArray();
 
        // Destroy the old individual light probe groups.
        foreach (var lightProbeGroup in lightProbeGroups)
        {
            if (lightProbeGroup != null && lightProbeGroup.gameObject != null)
            {
                Undo.DestroyObjectImmediate(lightProbeGroup.gameObject);
            }
        }
 
        Debug.Log($"Merged {mergedLightProbePositions.Count} light probes from {lightProbeGroups.Length} light probe groups into '{MergedLightProbeGroupName}'.");
 
        Selection.activeObject = newGameObject;
    }
}