// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
#if GT_USE_XRI
using UnityEngine.XR.Interaction.Toolkit;
#endif
namespace Microsoft.MixedReality.GraphicsTools
{
///
/// An abstract light class used for all light types within Graphics Tools.
///
[ExecuteInEditMode]
public abstract class BaseLight : MonoBehaviour
{
///
/// Method called before updating any lights.
///
protected abstract void Initialize();
///
/// Called when a light is added to a scene.
///
protected abstract void AddLight();
///
/// Called when a light is removed from a scene.
///
protected abstract void RemoveLight();
///
/// Called when light data needs to be updated in the shader system.
///
protected abstract void UpdateLights(bool forceUpdate = false);
#region MonoBehaviour Implementation
///
/// This function is called when the light becomes enabled and active.
///
protected virtual void OnEnable()
{
AddLight();
Application.onBeforeRender += UpdateLightsCallback;
}
///
/// This function is called when the light becomes disabled.
///
protected virtual void OnDisable()
{
Application.onBeforeRender -= UpdateLightsCallback;
RemoveLight();
UpdateLights(true);
}
#if UNITY_EDITOR
///
/// Only called in the editor since LateUpdate is not called with ExecuteInEditMode.
///
protected virtual void Update()
{
if (Application.isPlaying)
{
return;
}
Initialize();
UpdateLights();
}
#endif // UNITY_EDITOR
// Retained for breaking change purposes
protected virtual void LateUpdate() { }
///
/// Lights are updated in Application.onBeforeRender to ensure other behaviors have had a chance to move the lights.
///
#if GT_USE_XRI
[BeforeRenderOrder(XRInteractionUpdateOrder.k_BeforeRenderLineVisual)]
#endif
protected virtual void UpdateLightsCallback()
{
UpdateLights();
}
#endregion MonoBehaviour Implementation
}
}