// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using MonoBehaviour = WaveHarmonic.Crest.Internal.EditorBehaviour;
#else
using MonoBehaviour = UnityEngine.MonoBehaviour;
#endif
namespace WaveHarmonic.Crest.Internal
{
///
/// Implements logic to smooth out Unity's wrinkles.
///
public abstract class CustomBehaviour : MonoBehaviour
{
bool _AfterStart;
#pragma warning disable 114
private protected virtual void Awake()
{
#if UNITY_EDITOR
base.Awake();
#endif
}
///
/// Unity's Start method. Make sure to call base if overriden.
///
protected void Start()
{
_AfterStart = true;
#if UNITY_EDITOR
base.Start();
if (!enabled) return;
#endif
OnStart();
}
#pragma warning restore 114
///
/// Called in OnEnable only after Start has ran.
///
private protected virtual void Initialize()
{
}
///
/// Replaces Start. Only called in the editor if passes validation.
///
private protected virtual void OnStart()
{
Initialize();
}
///
/// Unity's OnEnable method. Make sure to call base if overriden.
///
private protected virtual void OnEnable()
{
if (!_AfterStart) return;
Initialize();
}
#if UNITY_EDITOR
[InitializeOnEnterPlayMode]
static void OnEnterPlayModeInEditor(EnterPlayModeOptions options)
{
foreach (var @object in FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None))
{
@object._AfterStart = false;
}
}
#endif
}
}