| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using Hertzole.ScriptableValues.Helpers; |
| | | 5 | | using UnityEngine; |
| | | 6 | | using UnityEngine.Events; |
| | | 7 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 8 | | using Unity.Properties; |
| | | 9 | | #endif |
| | | 10 | | |
| | | 11 | | namespace Hertzole.ScriptableValues |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// A <see cref="ScriptableObject" /> that can be invoked to trigger an event without any arguments. |
| | | 15 | | /// </summary> |
| | | 16 | | #if UNITY_EDITOR |
| | | 17 | | [CreateAssetMenu(fileName = "New Runtime Event", menuName = "Hertzole/Scriptable Values/Events/Runtime Event", order |
| | | 18 | | [HelpURL(Documentation.SCRIPTABLE_EVENT_URL)] |
| | | 19 | | #endif |
| | | 20 | | public partial class ScriptableEvent : RuntimeScriptableObject |
| | | 21 | | { |
| | | 22 | | [SerializeField] |
| | | 23 | | [EditorTooltip("Called when the event is invoked.")] |
| | | 24 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 25 | | [DontCreateProperty] |
| | | 26 | | #endif |
| | 209 | 27 | | internal UnityEvent onInvoked = new UnityEvent(); |
| | | 28 | | |
| | | 29 | | #if UNITY_INCLUDE_TESTS |
| | | 30 | | /// <summary> |
| | | 31 | | /// A test only check to see if the event has any subscribers. |
| | | 32 | | /// </summary> |
| | | 33 | | internal bool InvokedHasSubscribers |
| | | 34 | | { |
| | 180 | 35 | | get { return OnInvoked != null; } |
| | | 36 | | } |
| | | 37 | | #endif |
| | | 38 | | |
| | | 39 | | #if UNITY_EDITOR |
| | | 40 | | // Used for the CreateAssetMenu attribute order. |
| | | 41 | | internal const int ORDER = ScriptableValue.ORDER + 50; |
| | | 42 | | #endif |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Called when the event is invoked. |
| | | 46 | | /// </summary> |
| | | 47 | | public event EventHandler? OnInvoked; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Invokes the event with this <see cref="ScriptableEvent" /> as the sender. |
| | | 51 | | /// </summary> |
| | | 52 | | public void Invoke() |
| | 42 | 53 | | { |
| | 42 | 54 | | InvokeInternal(this, 1); |
| | 42 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Invokes the event with the specified sender. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 61 | | public void Invoke(object sender) |
| | 678 | 62 | | { |
| | 678 | 63 | | InvokeInternal(sender, 1); |
| | 678 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Invokes the event with the specified sender and argument. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 70 | | /// <param name="skipFrames">How many frames of stack trace to skip.</param> |
| | | 71 | | private void InvokeInternal(object sender, int skipFrames) |
| | 720 | 72 | | { |
| | 720 | 73 | | if (!OnBeforeInvoked(sender)) |
| | 6 | 74 | | { |
| | 6 | 75 | | return; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Skip at least one frame to avoid the Invoke method itself being included in the stack trace. |
| | 714 | 79 | | AddStackTrace(1 + skipFrames); |
| | | 80 | | |
| | 714 | 81 | | OnInvoked?.Invoke(sender, EventArgs.Empty); |
| | 714 | 82 | | onInvoked.Invoke(); |
| | | 83 | | |
| | 714 | 84 | | OnAfterInvoked(sender); |
| | 720 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Called before the event is invoked. This can be used to prevent the event from being invoked. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 91 | | /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns> |
| | | 92 | | protected virtual bool OnBeforeInvoked(object sender) |
| | 702 | 93 | | { |
| | 702 | 94 | | return true; |
| | 702 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Called after the event has been invoked. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="sender">The object that invoked the event.</param> |
| | 1404 | 101 | | protected virtual void OnAfterInvoked(object sender) { } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc /> |
| | | 104 | | protected override void OnPreStart() |
| | 161 | 105 | | { |
| | | 106 | | // Remove any subscribers that are left over from play mode. |
| | | 107 | | // Don't warn if there are any subscribers left over because we already do that in OnExitPlayMode. |
| | 161 | 108 | | ClearSubscribers(); |
| | 161 | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Warns if there are any left-over subscribers to the event. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <remarks>This will only be called in the Unity editor and builds with the DEBUG flag.</remarks> |
| | | 115 | | protected override void WarnIfLeftOverSubscribers() |
| | 167 | 116 | | { |
| | 167 | 117 | | base.WarnIfLeftOverSubscribers(); |
| | 167 | 118 | | EventHelper.WarnIfLeftOverSubscribers(OnInvoked, nameof(OnInvoked), this); |
| | 167 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Removes any subscribers from the event. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="warnIfLeftOver"> |
| | | 125 | | /// If <c>true</c>, a warning will be printed in the console if there are any subscribers. |
| | | 126 | | /// The warning will only be printed in the editor and debug builds. |
| | | 127 | | /// </param> |
| | | 128 | | public void ClearSubscribers(bool warnIfLeftOver = false) |
| | 167 | 129 | | { |
| | | 130 | | #if DEBUG |
| | 167 | 131 | | if (warnIfLeftOver) |
| | 6 | 132 | | { |
| | 6 | 133 | | WarnIfLeftOverSubscribers(); |
| | 6 | 134 | | } |
| | | 135 | | #endif |
| | | 136 | | |
| | 167 | 137 | | OnInvoked = null; |
| | 167 | 138 | | } |
| | | 139 | | |
| | | 140 | | /// <inheritdoc /> |
| | | 141 | | protected override void OnDisabled() |
| | 161 | 142 | | { |
| | 161 | 143 | | WarnIfLeftOverSubscribers(); |
| | 161 | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |