Skip to content

Commit bb861cd

Browse files
committedJan 31, 2024·
Merge branch 'develop'
2 parents c25469d + be0ff6e commit bb861cd

26 files changed

+163
-163
lines changed
 

‎Editor/Events/EventChannelEditor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace JUtils.Editor
77
{
88
[CanEditMultipleObjects]
9-
[CustomEditor(typeof(EventChannel<>), true)]
9+
[CustomEditor(typeof(BaseEventChannel<>), true)]
1010
public class EventChannelEditor : UnityEditor.Editor
1111
{
1212
[SerializeField] private VisualTreeAsset _eventChannelTree;

‎Runtime/Events/BaseEventChannel.cs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace JUtils
5+
{
6+
/// <summary>
7+
/// The base class for an simple event channel
8+
///
9+
/// Event channels are scriptable objects that can dynamically be assigned
10+
/// </summary>
11+
/// <remarks>When inheriting this class, it is recommended to leave the body empty</remarks>
12+
[ResourcePlayModeChangeCallbackReceiver("Events")]
13+
public abstract class BaseEventChannel<T> : ScriptableObject
14+
{
15+
[SerializeField] private bool _clearEventsOnPlayModeExit = true;
16+
[SerializeField] private bool _checkForLeaksOnPlayModeExit = true;
17+
18+
private event Action<T> listeners;
19+
20+
21+
/// <summary>
22+
/// Add a listener to this event channel
23+
/// </summary>
24+
public void AddListener(Action<T> listener)
25+
{
26+
listeners += listener;
27+
}
28+
29+
30+
/// <summary>
31+
/// Remove a listener from this event channel
32+
/// </summary>
33+
public void RemoveListener(Action<T> listener)
34+
{
35+
listeners -= listener;
36+
}
37+
38+
39+
/// <summary>
40+
/// Unsafe version of the <see cref="EventChannelExtensions.Invoke"/> function. This is directly on the class, but it does not have check if the channel is null or not.
41+
/// </summary>
42+
/// <param name="argument"></param>
43+
public virtual void InvokeUnsafe(T argument)
44+
{
45+
listeners?.Invoke(argument);
46+
}
47+
48+
49+
#if UNITY_EDITOR
50+
public virtual void OnPlayModeExit()
51+
{
52+
if (listeners == null) return;
53+
54+
if (_checkForLeaksOnPlayModeExit) {
55+
int count = listeners.GetInvocationList().Length;
56+
if (count > 0) Debug.LogWarning($"<color=red>Event Leak Detected!</color> '{name}' had {count} listeners after exiting play mode!", this);
57+
}
58+
59+
if (_clearEventsOnPlayModeExit) listeners = null;
60+
}
61+
#endif
62+
}
63+
}

‎Runtime/Events/BaseEventChannel.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Runtime/Events/BaseEventListener.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.Events;
4+
5+
namespace JUtils
6+
{
7+
/// <summary>
8+
/// The base class for event channel based listeners. Recommended for UI or if the GO only has one event listener, otherwise consider using the <see cref="BaseEventChannel{T}"/> directly
9+
/// </summary>
10+
/// <remarks>When inheriting this class, it is recommended to leave the body empty</remarks>
11+
public abstract class BaseEventListener<TListener, TArgument> : MonoBehaviour where TListener : BaseEventChannel<TArgument>
12+
{
13+
[SerializeField] private TListener _eventChannel;
14+
[SerializeField] private UnityEvent<TArgument> _event;
15+
16+
private event Action<TArgument> myListeners;
17+
18+
19+
/// <summary>
20+
/// Add a listener to the internal eventListener of this class
21+
/// </summary>
22+
public void AddListener(Action<TArgument> listener)
23+
{
24+
myListeners += listener;
25+
}
26+
27+
28+
/// <summary>
29+
/// Remove a listener to the internal eventListener of this class
30+
/// </summary>
31+
public void RemoveListener(Action<TArgument> listener)
32+
{
33+
myListeners -= listener;
34+
}
35+
36+
37+
private void OnEnable()
38+
{
39+
_eventChannel.AddListener(HandleChannelInvoked);
40+
}
41+
42+
43+
private void OnDisable()
44+
{
45+
_eventChannel.RemoveListener(HandleChannelInvoked);
46+
}
47+
48+
49+
private void HandleChannelInvoked(TArgument argument)
50+
{
51+
myListeners?.Invoke(argument);
52+
_event.Invoke(argument);
53+
}
54+
}
55+
}

‎Runtime/Events/BaseEventListener.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Runtime/Events/BoolEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/Bool Event Channel")]
6-
public sealed class BoolEventChannel : EventChannel<bool> { }
6+
public sealed class BoolEventChannel : BaseEventChannel<bool> { }
77
}

‎Runtime/Events/BoolEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class BoolEventListener : EventListener<BoolEventChannel, bool> { }
3+
public sealed class BoolEventListener : BaseEventListener<BoolEventChannel, bool> { }
44
}

‎Runtime/Events/EmptyEventChannel.cs

-33
This file was deleted.

‎Runtime/Events/EmptyEventChannel.cs.meta

-3
This file was deleted.

‎Runtime/Events/EmptyEventListener.cs

-27
This file was deleted.

‎Runtime/Events/EmptyEventListener.cs.meta

-3
This file was deleted.

‎Runtime/Events/EventChannel.cs

+10-40
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,33 @@
1-
using System;
1+
using System;
22
using UnityEngine;
33

44
namespace JUtils
55
{
6-
/// <summary>
7-
/// The base class for an simple event channel
8-
///
9-
/// Event channels are scriptable objects that can dynamically be assigned
10-
/// </summary>
11-
/// <remarks>When inheriting this class, it is recommended to leave the body empty</remarks>
12-
[ResourcePlayModeChangeCallbackReceiver("Events")]
13-
public abstract class EventChannel<T> : ScriptableObject
6+
[CreateAssetMenu(menuName = "JUtils/Events/Event Channel")]
7+
public sealed class EventChannel : BaseEventChannel<EventChannel.Empty>
148
{
15-
[SerializeField] private bool _clearEventsOnPlayModeExit = true;
16-
[SerializeField] private bool _checkForLeaksOnPlayModeExit = true;
9+
private event Action listeners;
1710

18-
private event Action<T> listeners;
1911

20-
21-
/// <summary>
22-
/// Add a listener to this event channel
23-
/// </summary>
24-
public void AddListener(Action<T> listener)
12+
public void AddListener(Action listener)
2513
{
2614
listeners += listener;
2715
}
2816

2917

30-
/// <summary>
31-
/// Remove a listener from this event channel
32-
/// </summary>
33-
public void RemoveListener(Action<T> listener)
18+
public void RemoveListener(Action listener)
3419
{
3520
listeners -= listener;
3621
}
3722

3823

39-
/// <summary>
40-
/// Unsafe version of the <see cref="EventChannelExtensions.Invoke"/> function. This is directly on the class, but it does not have check if the channel is null or not.
41-
/// </summary>
42-
/// <param name="argument"></param>
43-
public virtual void InvokeUnsafe(T argument)
24+
public override void InvokeUnsafe(Empty argument)
4425
{
45-
listeners?.Invoke(argument);
26+
listeners?.Invoke();
27+
base.InvokeUnsafe(argument);
4628
}
4729

4830

49-
#if UNITY_EDITOR
50-
public virtual void OnPlayModeExit()
51-
{
52-
if (listeners == null) return;
53-
54-
if (_checkForLeaksOnPlayModeExit) {
55-
int count = listeners.GetInvocationList().Length;
56-
if (count > 0) Debug.LogWarning($"<color=red>Event Leak Detected!</color> '{name}' had {count} listeners after exiting play mode!", this);
57-
}
58-
59-
if (_clearEventsOnPlayModeExit) listeners = null;
60-
}
61-
#endif
31+
public struct Empty { }
6232
}
6333
}

‎Runtime/Events/EventChannel.cs.meta

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Runtime/Events/EventChannelExtensions.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
namespace JUtils
44
{
55
/// <summary>
6-
/// Useful extensions for dealing with <see cref="EventChannel{T}"/>>
6+
/// Useful extensions for dealing with <see cref="BaseEventChannel{T}"/>>
77
/// </summary>
88
public static class EventChannelExtensions
99
{
1010
/// <summary>
11-
/// Raise an event on an <see cref="EventChannel{T}"/>.
11+
/// Raise an event on an <see cref="BaseEventChannel{T}"/>.
1212
/// </summary>
1313
/// <remarks>This automatically checks if the channel is null</remarks>
14-
public static void Invoke<T>([CanBeNull] this EventChannel<T> eventChannel, T argument)
14+
public static void Invoke<T>([CanBeNull] this BaseEventChannel<T> eventChannel, T argument)
1515
{
1616
if (eventChannel == null) return;
1717
eventChannel.InvokeUnsafe(argument);
1818
}
1919

2020
/// <summary>
21-
/// Raise an event on an <see cref="EmptyEventChannel"/>>.
21+
/// Raise an event on an <see cref="EventChannel"/>>.
2222
/// </summary>
2323
/// <remarks>This automatically checks if the channel is null</remarks>
24-
public static void Invoke([CanBeNull] this EmptyEventChannel eventChannel)
24+
public static void Invoke([CanBeNull] this EventChannel eventChannel)
2525
{
2626
if (eventChannel == null) return;
2727
eventChannel.InvokeUnsafe(default);

‎Runtime/Events/EventListener.cs

+7-35
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,27 @@
1-
using System;
2-
using UnityEngine;
3-
using UnityEngine.Events;
1+
using System;
42

53
namespace JUtils
64
{
7-
/// <summary>
8-
/// The base class for event channel based listeners. Recommended for UI or if the GO only has one event listener, otherwise consider using the <see cref="EventChannel{T}"/> directly
9-
/// </summary>
10-
/// <remarks>When inheriting this class, it is recommended to leave the body empty</remarks>
11-
public abstract class EventListener<TListener, TArgument> : MonoBehaviour where TListener : EventChannel<TArgument>
5+
public sealed class EventListener : BaseEventListener<EventChannel, EventChannel.Empty>
126
{
13-
[SerializeField] private TListener _eventChannel;
14-
[SerializeField] private UnityEvent<TArgument> _event;
7+
private event Action myListeners;
158

16-
private event Action<TArgument> myListeners;
179

18-
19-
/// <summary>
20-
/// Add a listener to the internal eventListener of this class
21-
/// </summary>
22-
public void AddListener(Action<TArgument> listener)
10+
public void AddListener(Action listener)
2311
{
2412
myListeners += listener;
2513
}
2614

2715

28-
/// <summary>
29-
/// Remove a listener to the internal eventListener of this class
30-
/// </summary>
31-
public void RemoveListener(Action<TArgument> listener)
16+
public void RemoveListener(Action listener)
3217
{
3318
myListeners -= listener;
3419
}
3520

3621

37-
private void OnEnable()
38-
{
39-
_eventChannel.AddListener(HandleChannelInvoked);
40-
}
41-
42-
43-
private void OnDisable()
44-
{
45-
_eventChannel.RemoveListener(HandleChannelInvoked);
46-
}
47-
48-
49-
private void HandleChannelInvoked(TArgument argument)
22+
private void Awake()
5023
{
51-
myListeners?.Invoke(argument);
52-
_event.Invoke(argument);
24+
AddListener(() => myListeners?.Invoke());
5325
}
5426
}
5527
}

‎Runtime/Events/EventListener.cs.meta

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Runtime/Events/FloatEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/Float Event Channel")]
6-
public sealed class FloatEventChannel : EventChannel<float> { }
6+
public sealed class FloatEventChannel : BaseEventChannel<float> { }
77
}

‎Runtime/Events/FloatEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class FloatEventListener : EventListener<FloatEventChannel, float> { }
3+
public sealed class FloatEventListener : BaseEventListener<FloatEventChannel, float> { }
44
}

‎Runtime/Events/IntEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/Int Event Channel")]
6-
public sealed class IntEventChannel : EventChannel<int> { }
6+
public sealed class IntEventChannel : BaseEventChannel<int> { }
77
}

‎Runtime/Events/IntEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class IntEventListener : EventListener<IntEventChannel, int> { }
3+
public sealed class IntEventListener : BaseEventListener<IntEventChannel, int> { }
44
}

‎Runtime/Events/LongEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/Long Event Channel")]
6-
public sealed class LongEventChannel : EventChannel<long> { }
6+
public sealed class LongEventChannel : BaseEventChannel<long> { }
77
}

‎Runtime/Events/LongEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class LongEventListener : EventListener<LongEventChannel, long> { }
3+
public sealed class LongEventListener : BaseEventListener<LongEventChannel, long> { }
44
}

‎Runtime/Events/UIntEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/UInt Event Channel")]
6-
public sealed class UIntEventChannel : EventChannel<uint> { }
6+
public sealed class UIntEventChannel : BaseEventChannel<uint> { }
77
}

‎Runtime/Events/UIntEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class UIntEventListener : EventListener<UIntEventChannel, uint> { }
3+
public sealed class UIntEventListener : BaseEventListener<UIntEventChannel, uint> { }
44
}

‎Runtime/Events/ULongEventChannel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
namespace JUtils
44
{
55
[CreateAssetMenu(menuName = "JUtils/Events/ULong Event Channel")]
6-
public sealed class ULongEventChannel : EventChannel<ulong> { }
6+
public sealed class ULongEventChannel : BaseEventChannel<ulong> { }
77
}

‎Runtime/Events/ULongEventListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
namespace JUtils
22
{
3-
public sealed class ULongEventListener : EventListener<ULongEventChannel, ulong> { }
3+
public sealed class ULongEventListener : BaseEventListener<ULongEventChannel, ulong> { }
44
}

0 commit comments

Comments
 (0)
Please sign in to comment.