YAMO-Unity6_MainProject/Assets/Scripts/LightGroupController.cs
2025-12-05 18:47:59 +09:00

74 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace YAMO.Scripts
{
[ExecuteAlways]
public class LightGroupController : MonoBehaviour
{
[Tooltip("List of lights to control. If empty and Auto Find is true, will populate from children.")]
public List<Light> lights = new List<Light>();
[Tooltip("Master intensity value. Controls the brightness of all lights directly.")]
public float masterIntensity = 1.0f;
[Tooltip("If true, automatically finds all Light components in children on Awake and OnValidate.")]
public bool autoFindLights = false;
private void Awake()
{
if (autoFindLights)
{
FindLightsInChildren();
}
}
private void Update()
{
// In Edit Mode, we want to update constantly to react to inspector changes
// In Play Mode, we also update to support animation
UpdateLightIntensities();
}
private void OnValidate()
{
// Called when a value is changed in the inspector
if (autoFindLights)
{
FindLightsInChildren();
}
UpdateLightIntensities();
}
/// <summary>
/// Finds all Light components in children and adds them to the list.
/// </summary>
public void FindLightsInChildren()
{
// Avoid clearing if we just want to ensure we have them,
// but for simplicity, let's refresh the list if requested.
// We check for nulls or missing items.
var childLights = GetComponentsInChildren<Light>();
if (lights.Count != childLights.Length)
{
lights.Clear();
lights.AddRange(childLights);
}
}
/// <summary>
/// Updates the intensity of all lights to the master intensity.
/// </summary>
public void UpdateLightIntensities()
{
foreach (var light in lights)
{
if (light != null)
{
light.intensity = masterIntensity;
}
}
}
}
}