< Summary

Information
Class: Hertzole.ScriptableValues.ScriptableEvent
Assembly: Hertzole.ScriptableValues
File(s): File 1: ./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.cs
File 2: ./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.EventArgs.cs
Tag: 52_28197466029
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 156
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
File 1: ScriptableEvent()0%110100%
File 1: Invoke()0%110100%
File 1: Invoke(...)0%110100%
File 1: InvokeInternal(...)0%330100%
File 1: OnBeforeInvoked(...)0%110100%
File 1: OnAfterInvoked(...)0%110100%
File 1: OnPreStart()0%110100%
File 1: WarnIfLeftOverSubscribers()0%110100%
File 1: ClearSubscribers(...)0%220100%
File 1: OnDisabled()0%110100%
File 2: ScriptableEvent()0%110100%

File(s)

./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.cs

#LineLine coverage
 1#nullable enable
 2
 3using System;
 4using Hertzole.ScriptableValues.Helpers;
 5using UnityEngine;
 6using UnityEngine.Events;
 7#if SCRIPTABLE_VALUES_PROPERTIES
 8using Unity.Properties;
 9#endif
 10
 11namespace 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
 20927        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        {
 18035            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()
 4253        {
 4254            InvokeInternal(this, 1);
 4255        }
 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)
 67862        {
 67863            InvokeInternal(sender, 1);
 67864        }
 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)
 72072        {
 72073            if (!OnBeforeInvoked(sender))
 674            {
 675                return;
 76            }
 77
 78            // Skip at least one frame to avoid the Invoke method itself being included in the stack trace.
 71479            AddStackTrace(1 + skipFrames);
 80
 71481            OnInvoked?.Invoke(sender, EventArgs.Empty);
 71482            onInvoked.Invoke();
 83
 71484            OnAfterInvoked(sender);
 72085        }
 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)
 70293        {
 70294            return true;
 70295        }
 96
 97        /// <summary>
 98        ///     Called after the event has been invoked.
 99        /// </summary>
 100        /// <param name="sender">The object that invoked the event.</param>
 1404101        protected virtual void OnAfterInvoked(object sender) { }
 102
 103        /// <inheritdoc />
 104        protected override void OnPreStart()
 161105        {
 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.
 161108            ClearSubscribers();
 161109        }
 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()
 167116        {
 167117            base.WarnIfLeftOverSubscribers();
 167118            EventHelper.WarnIfLeftOverSubscribers(OnInvoked, nameof(OnInvoked), this);
 167119        }
 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)
 167129        {
 130#if DEBUG
 167131            if (warnIfLeftOver)
 6132            {
 6133                WarnIfLeftOverSubscribers();
 6134            }
 135#endif
 136
 167137            OnInvoked = null;
 167138        }
 139
 140        /// <inheritdoc />
 141        protected override void OnDisabled()
 161142        {
 161143            WarnIfLeftOverSubscribers();
 161144        }
 145    }
 146}

./Packages/se.hertzole.scriptable-values/Runtime/Events/ScriptableEvent.EventArgs.cs

#LineLine coverage
 1using System.ComponentModel;
 2
 3namespace Hertzole.ScriptableValues
 4{
 5    partial class ScriptableEvent
 6    {
 27        public static readonly PropertyChangedEventArgs previousArgsChanged = new PropertyChangedEventArgs(nameof(Script
 28        public static readonly PropertyChangingEventArgs previousArgsChanging = new PropertyChangingEventArgs(nameof(Scr
 9    }
 10}