移除水
This commit is contained in:
@@ -1,238 +0,0 @@
|
||||
// Crest Water System
|
||||
// Copyright © 2024 Wave Harmonic. All rights reserved.
|
||||
|
||||
#pragma warning disable IDE0005 // Using directive is unnecessary.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace WaveHarmonic.Crest
|
||||
{
|
||||
static class Symbols
|
||||
{
|
||||
public const string k_UnityEditor = "UNITY_EDITOR";
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
|
||||
sealed class ExecuteDuringEditMode : Attribute
|
||||
{
|
||||
[Flags]
|
||||
public enum Include
|
||||
{
|
||||
None,
|
||||
PrefabStage,
|
||||
BuildPipeline,
|
||||
All = PrefabStage | BuildPipeline,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum Options
|
||||
{
|
||||
None,
|
||||
Singleton,
|
||||
}
|
||||
|
||||
public Include _Including;
|
||||
public Options _Options;
|
||||
|
||||
public ExecuteDuringEditMode(Include including = Include.PrefabStage, Options options = Options.None)
|
||||
{
|
||||
_Including = including;
|
||||
_Options = options;
|
||||
}
|
||||
}
|
||||
|
||||
enum Getter
|
||||
{
|
||||
Default,
|
||||
Custom,
|
||||
}
|
||||
|
||||
enum Setter
|
||||
{
|
||||
Default,
|
||||
Custom,
|
||||
Internal,
|
||||
Dirty,
|
||||
None,
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = false)]
|
||||
sealed class GenerateAPI : Attribute
|
||||
{
|
||||
public readonly Getter _Getter;
|
||||
public readonly Setter _Setter;
|
||||
public readonly string _Name;
|
||||
public readonly string _ScriptingSymbol;
|
||||
|
||||
public GenerateAPI(Getter getter = Getter.Default, Setter setter = Setter.Default, string name = null, string symbol = null)
|
||||
{
|
||||
_Getter = getter;
|
||||
_Setter = setter;
|
||||
_Name = name;
|
||||
_ScriptingSymbol = symbol;
|
||||
}
|
||||
|
||||
public GenerateAPI(Setter setter, string name = null, string symbol = null)
|
||||
{
|
||||
_Setter = setter;
|
||||
_Name = name;
|
||||
_ScriptingSymbol = symbol;
|
||||
}
|
||||
}
|
||||
|
||||
#if !UNITY_EDITOR
|
||||
|
||||
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
|
||||
abstract class Decorator : PropertyAttribute { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Layer : Decorator { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Stripped : Decorator { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Delayed : Decorator { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Disabled : Decorator { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class AttachMaterialEditor : Decorator { }
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Embedded : Decorator
|
||||
{
|
||||
public Embedded(int margin = 0) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class DecoratedField : Decorator
|
||||
{
|
||||
public DecoratedField(bool isCustomFoldout = false) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Group : Decorator
|
||||
{
|
||||
public enum Style { None, Foldout, Accordian, }
|
||||
public Group(string title = null, Style style = Style.Foldout, bool isCustomFoldout = false) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Stepped : Decorator
|
||||
{
|
||||
public Stepped(int minimum, int maximum, int step = 1, bool power = false) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Range : Decorator
|
||||
{
|
||||
[Flags]
|
||||
public enum Clamp { None = 0, Minimum = 1, Maximum = 2, Both = Minimum | Maximum }
|
||||
public Range(float minimum, float maximum, Clamp clamp = Clamp.Both, float scale = 1f, bool delayed = false, int step = 0, bool power = false) {}
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Minimum : Decorator
|
||||
{
|
||||
public Minimum(float minimum) {}
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Maximum : Decorator
|
||||
{
|
||||
public Maximum(float maximum) {}
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class WarnIfAbove : Decorator
|
||||
{
|
||||
public WarnIfAbove(float maximum) {}
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Filtered : Decorator
|
||||
{
|
||||
public enum Mode { Include, Exclude, }
|
||||
public Filtered(int unset = 0) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class FilterEnum : Decorator
|
||||
{
|
||||
public FilterEnum(string property, Filtered.Mode mode, params int[] values) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Label : Decorator
|
||||
{
|
||||
public Label(string label) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Heading : Decorator
|
||||
{
|
||||
public enum Style { Normal, Settings, }
|
||||
public Heading(string heading, Style style = Style.Normal, bool alwaysVisible = false, bool alwaysEnabled = false, string helpLink = null) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Space : Decorator
|
||||
{
|
||||
public Space(float height, bool isAlwaysVisible = false) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class Predicated : Decorator
|
||||
{
|
||||
public Predicated(Type type, string member, bool inverted = false, bool hide = false) { }
|
||||
public Predicated(Type type, bool inverted = false, bool hide = false) { }
|
||||
public Predicated(string property, bool inverted = false, object disableValue = null, bool hide = false) { }
|
||||
public Predicated(RenderPipeline rp, bool inverted = false, bool hide = false) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class OnChange : Decorator
|
||||
{
|
||||
public OnChange(bool skipIfInactive = true) { }
|
||||
public OnChange(Type type, bool skipIfInactive = true) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class HelpURL : Decorator
|
||||
{
|
||||
public HelpURL(string path = "") { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class HelpBox : Decorator
|
||||
{
|
||||
public enum MessageType { Info, Warning, Error, }
|
||||
public enum Visibility { Always, PropertyEnabled, PropertyDisabled, }
|
||||
public HelpBox(string message, MessageType messageType = MessageType.Info, Visibility visibility = Visibility.Always) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class MaterialField : Decorator
|
||||
{
|
||||
public MaterialField(string shader, string title = "", string name = "", string parent = null) { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class PrefabField : Decorator
|
||||
{
|
||||
public PrefabField(string title = "", string name = "") { }
|
||||
}
|
||||
|
||||
[Conditional(Symbols.k_UnityEditor)]
|
||||
sealed class ShowComputedProperty : Decorator
|
||||
{
|
||||
public ShowComputedProperty(string name) { }
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10b67a4d95adf4592abe527745be027b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,81 +0,0 @@
|
||||
// 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
|
||||
{
|
||||
/// <summary>
|
||||
/// Implements logic to smooth out Unity's wrinkles.
|
||||
/// </summary>
|
||||
public abstract class CustomBehaviour : MonoBehaviour
|
||||
{
|
||||
bool _AfterStart;
|
||||
|
||||
#pragma warning disable 114
|
||||
private protected virtual void Awake()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
base.Awake();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity's Start method. Make sure to call base if overriden.
|
||||
/// </summary>
|
||||
protected void Start()
|
||||
{
|
||||
_AfterStart = true;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
base.Start();
|
||||
if (!enabled) return;
|
||||
#endif
|
||||
|
||||
OnStart();
|
||||
}
|
||||
#pragma warning restore 114
|
||||
|
||||
/// <summary>
|
||||
/// Called in OnEnable only after Start has ran.
|
||||
/// </summary>
|
||||
private protected virtual void Initialize()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replaces Start. Only called in the editor if passes validation.
|
||||
/// </summary>
|
||||
private protected virtual void OnStart()
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity's OnEnable method. Make sure to call base if overriden.
|
||||
/// </summary>
|
||||
private protected virtual void OnEnable()
|
||||
{
|
||||
if (!_AfterStart) return;
|
||||
Initialize();
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[InitializeOnEnterPlayMode]
|
||||
static void OnEnterPlayModeInEditor(EnterPlayModeOptions options)
|
||||
{
|
||||
foreach (var @object in FindObjectsByType<CustomBehaviour>(FindObjectsInactive.Include, FindObjectsSortMode.None))
|
||||
{
|
||||
@object._AfterStart = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 636b808a07ad9446c84091c05f393663
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,184 +0,0 @@
|
||||
// Crest Water System
|
||||
// Copyright © 2024 Wave Harmonic. All rights reserved.
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
|
||||
namespace WaveHarmonic.Crest.Internal
|
||||
{
|
||||
using Include = ExecuteDuringEditMode.Include;
|
||||
|
||||
/// <summary>
|
||||
/// Implements custom behaviours common to all components.
|
||||
/// </summary>
|
||||
public abstract partial class EditorBehaviour : MonoBehaviour
|
||||
{
|
||||
bool _IsFirstOnValidate = true;
|
||||
internal bool _IsPrefabStageInstance;
|
||||
|
||||
private protected virtual void Awake()
|
||||
{
|
||||
// When copy and pasting from one scene to another, destroy instance objects as
|
||||
// they will have bad state.
|
||||
foreach (var generated in transform.GetComponentsInChildren<ManagedGameObject>(includeInactive: true))
|
||||
{
|
||||
if (generated.Owner == this)
|
||||
{
|
||||
Helpers.Destroy(generated.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start method. Must be called if overriden.
|
||||
/// </summary>
|
||||
protected virtual void Start()
|
||||
{
|
||||
if (Application.isPlaying && !(bool)s_ExecuteValidators.Invoke(null, new object[] { this }))
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Unity does not call OnDisable/OnEnable on Reset.
|
||||
private protected virtual void Reset()
|
||||
{
|
||||
if (!enabled) return;
|
||||
enabled = false;
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OnValidate method. Must be called if overriden.
|
||||
/// </summary>
|
||||
private protected virtual void OnValidate()
|
||||
{
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_IsFirstOnValidate)
|
||||
{
|
||||
var attribute = Helpers.GetCustomAttribute<ExecuteDuringEditMode>(GetType());
|
||||
|
||||
var enableInEditMode = attribute != null;
|
||||
|
||||
if (enableInEditMode && !attribute._Including.HasFlag(Include.BuildPipeline))
|
||||
{
|
||||
// Do not execute when building the player.
|
||||
enableInEditMode = !BuildPipeline.isBuildingPlayer;
|
||||
}
|
||||
|
||||
// Components that use the singleton pattern are candidates for not executing in the prefab stage
|
||||
// as a new instance will be created which could interfere with the scene stage instance.
|
||||
if (enableInEditMode && !attribute._Including.HasFlag(Include.PrefabStage))
|
||||
{
|
||||
var stage = PrefabStageUtility.GetCurrentPrefabStage();
|
||||
_IsPrefabStageInstance = stage != null && gameObject.scene == stage.scene;
|
||||
|
||||
// Do not execute in prefab stage.
|
||||
enableInEditMode = !_IsPrefabStageInstance;
|
||||
}
|
||||
|
||||
// When singleton, destroy instance objects.
|
||||
if (enableInEditMode && attribute._Options.HasFlag(ExecuteDuringEditMode.Options.Singleton) &&
|
||||
FindObjectsByType(GetType(), FindObjectsSortMode.None).Length > 1)
|
||||
{
|
||||
enableInEditMode = false;
|
||||
EditorApplication.update -= InternalDestroyNonSaveables;
|
||||
EditorApplication.update += InternalDestroyNonSaveables;
|
||||
}
|
||||
|
||||
// runInEditMode will immediately call Awake and OnEnable so we must not do this in OnValidate as there
|
||||
// are many restrictions which Unity will produce warnings for:
|
||||
// https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnValidate.html
|
||||
if (enableInEditMode)
|
||||
{
|
||||
if (BuildPipeline.isBuildingPlayer)
|
||||
{
|
||||
// EditorApplication.update and Invoke are not called when building.
|
||||
InternalEnableEditMode();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Called between OnAwake/OnEnable and Start which makes it seamless.
|
||||
EditorApplication.update -= InternalEnableEditMode;
|
||||
EditorApplication.update += InternalEnableEditMode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_IsFirstOnValidate = false;
|
||||
}
|
||||
|
||||
void InternalDestroyNonSaveables()
|
||||
{
|
||||
EditorApplication.update -= InternalDestroyNonSaveables;
|
||||
|
||||
// See comment below.
|
||||
if (this == null) return;
|
||||
|
||||
foreach (Transform transform in transform.GetComponentInChildren<Transform>(includeInactive: true))
|
||||
{
|
||||
if (transform.gameObject.hideFlags.HasFlag(HideFlags.DontSaveInEditor))
|
||||
{
|
||||
Helpers.Destroy(transform.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InternalEnableEditMode()
|
||||
{
|
||||
EditorApplication.update -= InternalEnableEditMode;
|
||||
|
||||
// If the scene that is being built is already opened then, there can be a rogue instance which registers
|
||||
// an event but is destroyed by the time it gets here. It has something to do with OnValidate being called
|
||||
// after the object is destroyed with _isFirstOnValidate being true.
|
||||
if (this == null) return;
|
||||
// Workaround to ExecuteAlways also executing during building which is often not what we want.
|
||||
runInEditMode = true;
|
||||
}
|
||||
|
||||
static MethodInfo s_ExecuteValidators;
|
||||
[InitializeOnLoadMethod]
|
||||
static void Load()
|
||||
{
|
||||
var type = System.Type.GetType("WaveHarmonic.Crest.Editor.ValidatedHelper, WaveHarmonic.Crest.Shared.Editor");
|
||||
s_ExecuteValidators = type.GetMethod
|
||||
(
|
||||
"ExecuteValidators",
|
||||
BindingFlags.Public | BindingFlags.Static,
|
||||
null,
|
||||
new[] { typeof(Object) },
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace WaveHarmonic.Crest
|
||||
{
|
||||
// Stores a reference to the owner so the GO can be deleted safely when duplicated/pasted.
|
||||
sealed class ManagedGameObject : MonoBehaviour
|
||||
{
|
||||
[field: SerializeField]
|
||||
public Component Owner { get; set; }
|
||||
}
|
||||
|
||||
static class Extentions
|
||||
{
|
||||
[System.Diagnostics.Conditional("UNITY_EDITOR")]
|
||||
public static void Manage(this Component owner, GameObject @object)
|
||||
{
|
||||
@object.AddComponent<ManagedGameObject>().Owner = owner;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f9b0b7f0a05449e4952db2c0e3a5516
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,177 +0,0 @@
|
||||
// Crest Water System
|
||||
// Copyright © 2024 Wave Harmonic. All rights reserved.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace WaveHarmonic.Crest.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages ManagedBehaviours. Replaces Unity's event system.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The manager type.</typeparam>
|
||||
public abstract class ManagerBehaviour<T> : CustomBehaviour where T : ManagerBehaviour<T>
|
||||
{
|
||||
internal static readonly List<Action<T>> s_OnUpdate = new();
|
||||
internal static readonly List<Action<T>> s_OnLateUpdate = new();
|
||||
internal static readonly List<Action<T>> s_OnFixedUpdate = new();
|
||||
internal static readonly List<Action<T>> s_OnEnable = new();
|
||||
internal static readonly List<Action<T>> s_OnDisable = new();
|
||||
|
||||
/// <summary>
|
||||
/// The singleton instance.
|
||||
/// </summary>
|
||||
public static T Instance { get; private set; }
|
||||
|
||||
void Broadcast(List<Action<T>> listeners, T instance)
|
||||
{
|
||||
for (var i = listeners.Count - 1; i >= 0; --i)
|
||||
{
|
||||
listeners[i].Invoke(instance);
|
||||
}
|
||||
}
|
||||
|
||||
void Broadcast(List<Action<T>> listeners)
|
||||
{
|
||||
Broadcast(listeners, Instance);
|
||||
}
|
||||
|
||||
private protected virtual void Enable()
|
||||
{
|
||||
// Setting up instance should be last.
|
||||
Instance = (T)this;
|
||||
Broadcast(s_OnEnable);
|
||||
}
|
||||
|
||||
private protected virtual void Disable()
|
||||
{
|
||||
Broadcast(s_OnDisable);
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
private protected virtual void FixedUpdate() => Broadcast(s_OnFixedUpdate);
|
||||
private protected void BroadcastUpdate() => Broadcast(s_OnUpdate);
|
||||
private protected virtual void LateUpdate() => Broadcast(s_OnLateUpdate);
|
||||
|
||||
// OnLoad etc cannot be used on open generic types.
|
||||
internal static void AfterRuntimeLoad()
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
internal static void AfterScriptReload()
|
||||
{
|
||||
Instance = FindFirstObjectByType<T>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A behaviour which is driven by a ManagerBehaviour instead of Unity's event system.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The manager type.</typeparam>
|
||||
public abstract class ManagedBehaviour<T> : CustomBehaviour where T : ManagerBehaviour<T>
|
||||
{
|
||||
readonly Action<T> _OnUpdate;
|
||||
readonly Action<T> _OnLateUpdate;
|
||||
readonly Action<T> _OnFixedUpdate;
|
||||
readonly Action<T> _OnEnable;
|
||||
readonly Action<T> _OnDisable;
|
||||
|
||||
/// <summary>
|
||||
/// The Update method called by the manager class.
|
||||
/// </summary>
|
||||
private protected virtual Action<T> OnUpdateMethod => null;
|
||||
|
||||
/// <summary>
|
||||
/// The LateUpdate method called by the manager class.
|
||||
/// </summary>
|
||||
private protected virtual Action<T> OnLateUpdateMethod => null;
|
||||
|
||||
/// <summary>
|
||||
/// The FixedUpdated method called by the manager class.
|
||||
/// </summary>
|
||||
private protected virtual Action<T> OnFixedUpdateMethod => null;
|
||||
|
||||
/// <summary>
|
||||
/// The OnEnable method called by the manager class.
|
||||
/// </summary>
|
||||
private protected virtual Action<T> OnEnableMethod => null;
|
||||
|
||||
/// <summary>
|
||||
/// The OnDisable method called by the manager class.
|
||||
/// </summary>
|
||||
private protected virtual Action<T> OnDisableMethod => null;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor which caches Actions to avoid allocations.
|
||||
/// </summary>
|
||||
public ManagedBehaviour()
|
||||
{
|
||||
if (OnUpdateMethod != null) _OnUpdate = new(OnUpdateMethod);
|
||||
if (OnLateUpdateMethod != null) _OnLateUpdate = new(OnLateUpdateMethod);
|
||||
if (OnFixedUpdateMethod != null) _OnFixedUpdate = new(OnFixedUpdateMethod);
|
||||
if (OnEnableMethod != null) _OnEnable = new(OnEnableMethod);
|
||||
if (OnDisableMethod != null) _OnDisable = new(OnDisableMethod);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
private protected override void OnEnable()
|
||||
{
|
||||
base.OnEnable();
|
||||
|
||||
UpdateSubscription(listen: true);
|
||||
|
||||
// Trigger OnEnable as it has already passed.
|
||||
if (_OnEnable != null && ManagerBehaviour<T>.Instance != null)
|
||||
{
|
||||
_OnEnable(ManagerBehaviour<T>.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity's OnDisable method. Make sure to call base if overriden.
|
||||
/// </summary>
|
||||
private protected virtual void OnDisable()
|
||||
{
|
||||
UpdateSubscription(listen: false);
|
||||
|
||||
if (_OnDisable != null && ManagerBehaviour<T>.Instance != null)
|
||||
{
|
||||
_OnDisable(ManagerBehaviour<T>.Instance);
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateSubscription(bool listen)
|
||||
{
|
||||
if (_OnUpdate != null)
|
||||
{
|
||||
ManagerBehaviour<T>.s_OnUpdate.Remove(_OnUpdate);
|
||||
if (listen) ManagerBehaviour<T>.s_OnUpdate.Add(_OnUpdate);
|
||||
}
|
||||
|
||||
if (_OnLateUpdate != null)
|
||||
{
|
||||
ManagerBehaviour<T>.s_OnLateUpdate.Remove(_OnLateUpdate);
|
||||
if (listen) ManagerBehaviour<T>.s_OnLateUpdate.Add(_OnLateUpdate);
|
||||
}
|
||||
|
||||
if (_OnFixedUpdate != null)
|
||||
{
|
||||
ManagerBehaviour<T>.s_OnFixedUpdate.Remove(_OnFixedUpdate);
|
||||
if (listen) ManagerBehaviour<T>.s_OnFixedUpdate.Add(_OnFixedUpdate);
|
||||
}
|
||||
|
||||
if (_OnEnable != null)
|
||||
{
|
||||
ManagerBehaviour<T>.s_OnEnable.Remove(_OnEnable);
|
||||
if (listen) ManagerBehaviour<T>.s_OnEnable.Add(_OnEnable);
|
||||
}
|
||||
|
||||
if (_OnDisable != null)
|
||||
{
|
||||
ManagerBehaviour<T>.s_OnDisable.Remove(_OnDisable);
|
||||
if (listen) ManagerBehaviour<T>.s_OnDisable.Add(_OnDisable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7b697363737b2425180c036dc593512e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user