diff --git a/crest/Assets/Crest/Crest-Examples/Shared/Scripts/UnderwaterEnvironmentalLighting.cs b/crest/Assets/Crest/Crest-Examples/Shared/Scripts/UnderwaterEnvironmentalLighting.cs index 875e804278..974a098631 100644 --- a/crest/Assets/Crest/Crest-Examples/Shared/Scripts/UnderwaterEnvironmentalLighting.cs +++ b/crest/Assets/Crest/Crest-Examples/Shared/Scripts/UnderwaterEnvironmentalLighting.cs @@ -15,7 +15,7 @@ namespace Crest /// (directional light, reflections, ambient etc) with the underwater depth. This works with vanilla lighting, but /// uncommon or custom lighting will require a custom solution (use this for reference). /// - public class UnderwaterEnvironmentalLighting : MonoBehaviour + public class UnderwaterEnvironmentalLighting : CrestComponent { Light _primaryLight; float _lightIntensity; @@ -27,13 +27,12 @@ public class UnderwaterEnvironmentalLighting : MonoBehaviour public const float DEPTH_OUTSCATTER_CONSTANT = 0.25f; - void OnEnable() + public override void OnEnable() { - if (OceanRenderer.Instance == null) + base.OnEnable(); + + if (!enabled) { - OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; - OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled; - enabled = false; return; } @@ -71,18 +70,6 @@ void OnDisable() RenderSettings.fogDensity = _fogDensity; } - private void OnDestroy() - { - // We need this event registered even when this component is disabled. So we unregister only when the - // component is destroyed. - OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; - } - - void OnOceanRendererEnabled() - { - enabled = true; - } - void LateUpdate() { if (OceanRenderer.Instance == null) diff --git a/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs b/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs new file mode 100644 index 0000000000..8b1e05faa0 --- /dev/null +++ b/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs @@ -0,0 +1,34 @@ +// Crest Ocean System + +// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE) + +// Base class for MonoBehaviours. + +using UnityEngine; + +namespace Crest +{ + public abstract class CrestComponent : MonoBehaviour + { + public virtual void OnEnable() + { + // Delayed initialisation. + if (OceanRenderer.Instance == null) + { + OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; + OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled; + enabled = false; + } + } + + void OnOceanRendererEnabled() => enabled = true; + + void OnDestroy() + { + // We need this event registered even when this component is disabled. So we unregister only when the + // component is destroyed. + OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; + } + } +} + diff --git a/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs.meta b/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs.meta new file mode 100644 index 0000000000..afdc021258 --- /dev/null +++ b/crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 60fd143d55b024c619beaabed6f9b90f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/crest/Assets/Crest/Crest/Scripts/Reflection/OceanPlanarReflection.cs b/crest/Assets/Crest/Crest/Scripts/Reflection/OceanPlanarReflection.cs index d3cc9966af..04f953a93b 100644 --- a/crest/Assets/Crest/Crest/Scripts/Reflection/OceanPlanarReflection.cs +++ b/crest/Assets/Crest/Crest/Scripts/Reflection/OceanPlanarReflection.cs @@ -79,7 +79,7 @@ static void InitStatics() /// /// Attach to a camera to generate a reflection texture which can be sampled in the ocean shader. /// - public class OceanPlanarReflection : MonoBehaviour + public class OceanPlanarReflection : CrestComponent { [SerializeField] LayerMask _reflectionLayers = 1; [SerializeField] bool _disableOcclusionCulling = true; @@ -117,13 +117,12 @@ public class OceanPlanarReflection : MonoBehaviour const int CULL_DISTANCE_COUNT = 32; float[] _cullDistances = new float[CULL_DISTANCE_COUNT]; - private void OnEnable() + public override void OnEnable() { - if (OceanRenderer.Instance == null) + base.OnEnable(); + + if (!enabled) { - OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; - OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled; - enabled = false; return; } @@ -148,11 +147,6 @@ private void OnEnable() #endif } - void OnOceanRendererEnabled() - { - enabled = true; - } - bool RequestRefresh(long frame) { if (_lastRefreshOnFrame <= 0 || RefreshPerFrames < 2) @@ -399,12 +393,5 @@ private void OnDisable() _camReflections = null; } } - - private void OnDestroy() - { - // We need this event registered even when this component is disabled. So we unregister only when the - // component is destroyed. - OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled; - } } }