| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using Hertzole.ScriptableValues.Helpers; |
| | | 5 | | using UnityEngine; |
| | | 6 | | using UnityEngine.Events; |
| | | 7 | | using UnityEngine.Serialization; |
| | | 8 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 9 | | using Unity.Properties; |
| | | 10 | | #endif |
| | | 11 | | |
| | | 12 | | namespace Hertzole.ScriptableValues |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// A <see cref="ScriptableObject" /> that can be invoked to trigger an event with an argument. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">The type of the argument.</typeparam> |
| | | 18 | | #if UNITY_EDITOR |
| | | 19 | | [HelpURL(Documentation.SCRIPTABLE_EVENT_GENERIC_URL)] |
| | | 20 | | #endif |
| | | 21 | | public abstract partial class ScriptableEvent<T> : RuntimeScriptableObject |
| | | 22 | | { |
| | | 23 | | [FormerlySerializedAs("onInvokedWithArgs")] |
| | | 24 | | [SerializeField] |
| | | 25 | | [EditorTooltip("Called when the event is invoked.")] |
| | | 26 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 27 | | [DontCreateProperty] |
| | | 28 | | #endif |
| | 8798 | 29 | | internal UnityEvent<T> onInvoked = new UnityEvent<T>(); |
| | | 30 | | |
| | | 31 | | #if UNITY_EDITOR // This does not need to be included in the build as it's only used in the editor. |
| | | 32 | | #pragma warning disable CS0414 // The field is assigned but its value is never used |
| | | 33 | | /// <summary> |
| | | 34 | | /// Used in the editor to invoke the event with an argument from the editor. |
| | | 35 | | /// </summary> |
| | | 36 | | [SerializeField] |
| | | 37 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 38 | | [DontCreateProperty] |
| | | 39 | | #endif |
| | 8798 | 40 | | internal T? editorInvokeValue = default; |
| | | 41 | | #pragma warning restore CS0414 // The field is assigned but its value is never used |
| | | 42 | | #endif |
| | | 43 | | |
| | | 44 | | // The arguments that was passed to the event when it was invoked. |
| | | 45 | | // This is used to set the previous value. |
| | | 46 | | private T? currentArgs; |
| | | 47 | | private T? previousArgs; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// The arguments that was passed to the event when it was invoked. |
| | | 51 | | /// </summary> |
| | | 52 | | #if SCRIPTABLE_VALUES_PROPERTIES |
| | | 53 | | #if SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY |
| | | 54 | | [CreateProperty(ReadOnly = true)] |
| | | 55 | | #else |
| | | 56 | | [CreateProperty] |
| | | 57 | | #endif // SCRIPTABLE_VALUES_PROPERTIES_SUPPORTS_READONLY |
| | | 58 | | #endif // SCRIPTABLE_VALUES_PROPERTIES |
| | | 59 | | public T? PreviousArgs |
| | | 60 | | { |
| | 19404 | 61 | | get { return previousArgs; } |
| | 53352 | 62 | | private set { SetField(ref previousArgs, value, ScriptableEvent.previousArgsChanging, ScriptableEvent.previo |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | #if UNITY_INCLUDE_TESTS |
| | | 66 | | internal bool InvokedHasSubscribers |
| | | 67 | | { |
| | 4680 | 68 | | get { return OnInvoked != null; } |
| | | 69 | | } |
| | | 70 | | #endif |
| | | 71 | | |
| | | 72 | | #if UNITY_EDITOR |
| | | 73 | | // Used for the CreateAssetMenu attribute order. |
| | | 74 | | internal const int ORDER = ScriptableEvent.ORDER + 1; |
| | | 75 | | #endif |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Called when the event is invoked. |
| | | 79 | | /// </summary> |
| | | 80 | | public event EventHandler<T>? OnInvoked; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Invokes the event with the specified argument and this <see cref="ScriptableEvent{T}" /> as the sender. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="args">The argument to send with the event.</param> |
| | | 86 | | public void Invoke(T args) |
| | 2364 | 87 | | { |
| | 2364 | 88 | | InvokeInternal(this, args, 1); |
| | 2364 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Invokes the event with the specified sender and argument. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 95 | | /// <param name="args">The argument to send with the event.</param> |
| | | 96 | | public void Invoke(object sender, T args) |
| | 7140 | 97 | | { |
| | 7140 | 98 | | InvokeInternal(sender, args, 1); |
| | 7140 | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Invokes the event with the specified sender and argument. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 105 | | /// <param name="args">The argument to send with the event.</param> |
| | | 106 | | /// <param name="skipFrames">How many frames of stack trace to skip.</param> |
| | | 107 | | private void InvokeInternal(object sender, T args, int skipFrames) |
| | 9504 | 108 | | { |
| | 9504 | 109 | | if (!OnBeforeInvoked(sender, args)) |
| | 156 | 110 | | { |
| | 156 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Skip at least one frame to avoid the Invoke method itself being included in the stack trace. |
| | 9348 | 115 | | AddStackTrace(1 + skipFrames); |
| | | 116 | | |
| | 9348 | 117 | | PreviousArgs = currentArgs; |
| | 9348 | 118 | | currentArgs = args; |
| | | 119 | | |
| | 9348 | 120 | | OnInvoked?.Invoke(sender, args); |
| | 9348 | 121 | | onInvoked.Invoke(args); |
| | | 122 | | |
| | 9348 | 123 | | OnAfterInvoked(sender, args); |
| | 9504 | 124 | | } |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Called before the event is invoked. This can be used to prevent the event from being invoked. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 130 | | /// <param name="args">The argument that was passed to the event.</param> |
| | | 131 | | /// <returns><c>true</c> if the event should be invoked; otherwise, <c>false</c>.</returns> |
| | | 132 | | protected virtual bool OnBeforeInvoked(object sender, T args) |
| | 9036 | 133 | | { |
| | 9036 | 134 | | return true; |
| | 9036 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Called after the event has been invoked. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="sender">The object that invoked the event.</param> |
| | | 141 | | /// <param name="args">The argument that was passed to the event.</param> |
| | 18072 | 142 | | protected virtual void OnAfterInvoked(object sender, T args) { } |
| | | 143 | | |
| | | 144 | | /// <inheritdoc /> |
| | | 145 | | protected override void OnPreStart() |
| | 8228 | 146 | | { |
| | | 147 | | // Remove any subscribers that are left over from play mode. |
| | | 148 | | // Don't warn if there are any subscribers left over because we already do that in OnExitPlayMode. |
| | 8228 | 149 | | ClearSubscribers(); |
| | 8228 | 150 | | PreviousArgs = default; |
| | 8228 | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Warns if there are any left-over subscribers to the event. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <remarks>This will only be called in the Unity editor and builds with the DEBUG flag.</remarks> |
| | | 157 | | protected override void WarnIfLeftOverSubscribers() |
| | 8306 | 158 | | { |
| | 8306 | 159 | | base.WarnIfLeftOverSubscribers(); |
| | 8306 | 160 | | EventHelper.WarnIfLeftOverSubscribers(OnInvoked, nameof(OnInvoked), this); |
| | 8306 | 161 | | } |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Removes any subscribers from the event. |
| | | 165 | | /// </summary> |
| | | 166 | | /// <param name="warnIfLeftOver"> |
| | | 167 | | /// If <c>true</c>, a warning will be printed in the console if there are any subscribers. |
| | | 168 | | /// The warning will only be printed in the editor and debug builds. |
| | | 169 | | /// </param> |
| | | 170 | | public void ClearSubscribers(bool warnIfLeftOver = false) |
| | 8234 | 171 | | { |
| | | 172 | | #if DEBUG |
| | 8234 | 173 | | if (warnIfLeftOver) |
| | 6 | 174 | | { |
| | 6 | 175 | | WarnIfLeftOverSubscribers(); |
| | 6 | 176 | | } |
| | | 177 | | #endif |
| | | 178 | | |
| | 8234 | 179 | | OnInvoked = null; |
| | 8234 | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <inheritdoc /> |
| | | 183 | | protected override void OnDisabled() |
| | 8300 | 184 | | { |
| | 8300 | 185 | | WarnIfLeftOverSubscribers(); |
| | 8300 | 186 | | } |
| | | 187 | | } |
| | | 188 | | } |