using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif [ExecuteAlways] [RequireComponent(typeof(Light))] public class MenuLightAnimator : MonoBehaviour { [Header("Rotation")] // Yaw oscillates around the base direction rather than spinning all the way around, so the // light never swings past the wall's edge into a back-lighting angle that goes dark. public float yawAmplitude = 35f; public float yawSpeed = 0.2f; public float pitchAmplitude = 10f; public float pitchSpeed = 0.35f; [Header("Intensity & Color")] public float baseIntensity = 1.1f; public float intensityPulseAmount = 0.25f; public float pulseSpeed = 0.6f; public Color colorA = new Color(1f, 0.96f, 0.86f); public Color colorB = new Color(0.8f, 0.88f, 1f); Light _light; float _baseYaw; float _basePitch; void OnEnable() { _light = GetComponent(); Vector3 e = transform.eulerAngles; _baseYaw = e.y; _basePitch = e.x; #if UNITY_EDITOR EditorApplication.update -= EditorTick; if (!Application.isPlaying) EditorApplication.update += EditorTick; #endif } void OnDisable() { #if UNITY_EDITOR EditorApplication.update -= EditorTick; #endif } #if UNITY_EDITOR void EditorTick() { if (Application.isPlaying) return; Animate((float)EditorApplication.timeSinceStartup); } #endif void Update() { if (!Application.isPlaying) return; Animate(Time.time); } void Animate(float time) { float yaw = _baseYaw + Mathf.Sin(time * yawSpeed) * yawAmplitude; float pitch = _basePitch + Mathf.Sin(time * pitchSpeed + 1.3f) * pitchAmplitude; transform.rotation = Quaternion.Euler(pitch, yaw, 0f); if (_light == null) _light = GetComponent(); _light.intensity = baseIntensity + Mathf.Sin(time * pulseSpeed) * intensityPulseAmount; float colorT = (Mathf.Sin(time * pulseSpeed * 0.5f) + 1f) * 0.5f; _light.color = Color.Lerp(colorA, colorB, colorT); } }