Skip to content
This repository was archived by the owner on Aug 10, 2021. It is now read-only.

Commit a2c41ff

Browse files
authored
Update game event editors (#21)
* Update game event editors * Use None as default reset type * Update changelog * Update usage readme
1 parent 7e5d0b7 commit a2c41ff

File tree

15 files changed

+117
-55
lines changed

15 files changed

+117
-55
lines changed

Assets/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.0] - 2020-10-13
8+
9+
### Changed
10+
- Made game event editors more user-friendly.
11+
- Naming in asset menus for game events.
12+
- Listeners are now exposed as an unmodifiable `ICollection`.
13+
714
## [1.0.0-rc.1] - 2020-10-11
815

916
## [0.7.0] - 2020-10-11

Assets/Documentation~/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Game events are scriptable objects (_Right Click -> Create -> Game Events -> ...
1717
Available game events:
1818
- `GameEvent` - simple event which doesn't accept any arguments.
1919
- `BoolGameEvent` - event with a `bool` argument.
20-
- `IntGameEvent` - event with a `int` argument.
20+
- `IntGameEvent` - event with an `int` argument.
2121
- `FloatGameEvent` - event with a `float` argument.
2222
- `StringGameEvent` - event with a `string` argument.
2323
- `Vector2GameEvent` - event with a `Vector2` argument.
@@ -35,20 +35,20 @@ Mutable objects are used for storing and editing data on `ScriptableObject` asse
3535

3636
Available mutable objects:
3737
- `MutableBool` - encapsulates a `bool` value.
38-
- `MutableInt` - encapsulates a `int` value.
38+
- `MutableInt` - encapsulates an `int` value.
3939
- `MutableFloat` - encapsulates a `float` value.
4040
- `MutableString` - encapsulates a `string` value.
4141
- `MutableVector2` - encapsulates a `Vector2` value.
4242
- `MutableVector3` - encapsulates a `Vector3` value.
4343

4444
Each mutable object has a `ResetType` property. This allows specifying when data in the mutable object should be reset. The following modes are available:
45-
- `None` - do not reset.
45+
- `None` - do not reset (default).
4646
- `ActiveSceneChange` - when the active (focused) scene changes.
4747
- `SceneUnloaded` - when the current scene gets unloaded.
4848
- `SceneLoaded` - when the scene is loaded.
4949

5050
### Custom game events
51-
In some situations, built-in game events might not suffice. For example if a custom type needs to be passed as an argument to the event. In this case, custom game event can be created which would carry all the necessary data.
51+
In some situations, built-in game events might not suffice. For example if a custom type needs to be passed as an argument to the event. In this case, a custom game event can be created which would carry all the necessary data.
5252

5353
To create a custom game event, first create a regular `UnityEvent`:
5454
```cs
@@ -74,7 +74,7 @@ public class CustomGameEventListener : ArgumentGameEventListener<CustomGameEvent
7474
}
7575
```
7676

77-
Optionally add a custom editor so that the event could be raised, and the listeners which reference the event get displayed in the inspector.
77+
, add a custom editor so that the event could be raised, and the listeners which reference the event get displayed in the inspector.
7878
```cs
7979
[CustomEditor(typeof(CustomGameEvent))]
8080
public class GameObjectGameEventEditor : ArgumentGameEventEditor<CustomGameEvent, Custom>

Assets/Editor/GameEvents/Game/GameEventEditor.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ namespace GameEvents.Game
88
[CustomEditor(typeof(GameEvent))]
99
public class GameEventEditor : Editor
1010
{
11-
private const int GroupSpacingPixels = 8;
12-
1311
public override void OnInspectorGUI()
1412
{
1513
base.OnInspectorGUI();
@@ -21,7 +19,7 @@ public override void OnInspectorGUI()
2119
}
2220

2321
GUI.enabled = Application.isPlaying;
24-
GUILayout.Space(GroupSpacingPixels);
22+
EditorGUILayout.Space();
2523

2624
DrawRaise(gameEvent);
2725

@@ -30,29 +28,17 @@ public override void OnInspectorGUI()
3028
return;
3129
}
3230

33-
GUILayout.Space(GroupSpacingPixels);
34-
DrawListeners(gameEvent);
31+
EditorGUILayout.Space();
32+
GameEventEditors.DrawReferences(gameEvent);
3533
}
3634

3735
private static void DrawRaise(IGameEvent gameEvent)
3836
{
39-
GUILayout.Label("Raise event (play mode only)");
37+
GameEventEditors.DrawPlaymodeLabel("Raise event");
4038
if (GUILayout.Button("Raise"))
4139
{
4240
gameEvent.RaiseGameEvent();
4341
}
4442
}
45-
46-
private static void DrawListeners(IGameEvent gameEvent)
47-
{
48-
GUILayout.Label("Listeners");
49-
foreach (var listener in gameEvent.Listeners)
50-
{
51-
if (listener is MonoBehaviour behaviour)
52-
{
53-
EditorGUILayout.ObjectField(behaviour, typeof(Object), true);
54-
}
55-
}
56-
}
5743
}
5844
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Collections.Generic;
2+
using GameEvents.Generic;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace GameEvents
7+
{
8+
public static class GameEventEditors
9+
{
10+
/// <summary>
11+
/// Draw a list of argument game event listener references.
12+
/// </summary>
13+
public static void DrawReferences<TArgument>(
14+
IArgumentGameEvent<TArgument> gameEvent
15+
)
16+
{
17+
DrawListeners(gameEvent.Listeners);
18+
}
19+
20+
/// <summary>
21+
/// Draw a list of game event listener references.
22+
/// </summary>
23+
public static void DrawReferences(IGameEvent gameEvent)
24+
{
25+
DrawListeners(gameEvent.Listeners);
26+
}
27+
28+
/// <summary>
29+
/// Draw a label which changes its suffix based on play mode state.
30+
/// </summary>
31+
public static void DrawPlaymodeLabel(string text)
32+
{
33+
var labelSuffix = Application.isPlaying ? "" : "(play mode only)";
34+
GUILayout.Label($"{text} {labelSuffix}");
35+
}
36+
37+
private static void DrawListeners<T>(ICollection<T> listeners)
38+
{
39+
DrawPlaymodeLabel("Listeners");
40+
if (Application.isPlaying)
41+
{
42+
if (listeners.Count == 0)
43+
{
44+
EditorGUILayout.HelpBox(
45+
"There are no listeners on this event",
46+
MessageType.Warning
47+
);
48+
49+
return;
50+
}
51+
52+
foreach (var listener in listeners)
53+
{
54+
if (listener is MonoBehaviour behaviour)
55+
{
56+
EditorGUILayout.ObjectField(behaviour, typeof(Object), true);
57+
}
58+
}
59+
}
60+
else
61+
{
62+
EditorGUILayout.HelpBox(
63+
"Registered listeners will be displayed here",
64+
MessageType.Info
65+
);
66+
}
67+
}
68+
}
69+
}

Assets/Editor/GameEvents/GameEventEditors.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor/GameEvents/Generic/ArgumentGameEventEditor.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ namespace GameEvents.Generic
77
public abstract class ArgumentGameEventEditor<TGameEvent, TArgument> : Editor
88
where TGameEvent : class, IArgumentGameEvent<TArgument>
99
{
10-
private const int GroupSpacingPixels = 8;
11-
1210
private TArgument argumentValue = default;
1311

1412
public override void OnInspectorGUI()
@@ -21,22 +19,17 @@ public override void OnInspectorGUI()
2119
}
2220

2321
GUI.enabled = Application.isPlaying;
24-
GUILayout.Space(GroupSpacingPixels);
22+
EditorGUILayout.Space();
2523

2624
DrawRaise(gameEvent);
2725

28-
if (!Application.isPlaying)
29-
{
30-
return;
31-
}
32-
33-
GUILayout.Space(GroupSpacingPixels);
34-
DrawListeners(gameEvent);
26+
EditorGUILayout.Space();
27+
GameEventEditors.DrawReferences(gameEvent);
3528
}
3629

3730
private void DrawRaise(TGameEvent gameEvent)
3831
{
39-
GUILayout.Label("Raise event (play mode only)");
32+
GameEventEditors.DrawPlaymodeLabel("Raise event");
4033
GUILayout.BeginHorizontal();
4134

4235
argumentValue = DrawArgumentField(argumentValue);
@@ -48,18 +41,6 @@ private void DrawRaise(TGameEvent gameEvent)
4841
GUILayout.EndHorizontal();
4942
}
5043

51-
private static void DrawListeners(TGameEvent gameEvent)
52-
{
53-
GUILayout.Label("Listeners");
54-
foreach (var listener in gameEvent.Listeners)
55-
{
56-
if (listener is MonoBehaviour behaviour)
57-
{
58-
EditorGUILayout.ObjectField(behaviour, typeof(Object), true);
59-
}
60-
}
61-
}
62-
6344
/// <returns>
6445
/// Value that is entered in the argument field.
6546
/// </returns>

Assets/Runtime/GameEvents/Game/GameEvent.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
23
using GameEvents.Generic;
34
using UnityEngine;
45

@@ -11,10 +12,17 @@ public class GameEvent : ScriptableObject, IGameEvent
1112
[Tooltip("Should debug messages be logged for this event")]
1213
private bool debug = false;
1314

15+
private readonly ReadOnlyCollection<IGameEventListener> readListeners;
16+
1417
private readonly List<IGameEventListener> listeners =
1518
new List<IGameEventListener>();
1619

17-
public IEnumerable<IGameEventListener> Listeners => listeners;
20+
public ICollection<IGameEventListener> Listeners => readListeners;
21+
22+
public GameEvent()
23+
{
24+
readListeners = listeners.AsReadOnly();
25+
}
1826

1927
public void RaiseGameEvent()
2028
{

Assets/Runtime/GameEvents/Generic/ArgumentGameEvent.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
23
using UnityEngine;
34

45
namespace GameEvents.Generic
@@ -10,10 +11,17 @@ public abstract class ArgumentGameEvent<TArgument>
1011
[Tooltip("Should debug messages be logged for this event")]
1112
private bool debug = false;
1213

14+
private readonly ReadOnlyCollection<IArgumentGameEventListener<TArgument>> readListeners;
15+
1316
private readonly List<IArgumentGameEventListener<TArgument>> listeners =
1417
new List<IArgumentGameEventListener<TArgument>>();
1518

16-
public IEnumerable<IArgumentGameEventListener<TArgument>> Listeners => listeners;
19+
public ICollection<IArgumentGameEventListener<TArgument>> Listeners => readListeners;
20+
21+
public ArgumentGameEvent()
22+
{
23+
readListeners = listeners.AsReadOnly();
24+
}
1725

1826
public void RaiseGameEvent(TArgument argument)
1927
{

Assets/Runtime/GameEvents/Generic/IArgumentGameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface IArgumentGameEvent<TArgument>
77
/// <summary>
88
/// Currently registered listeners.
99
/// </summary>
10-
IEnumerable<IArgumentGameEventListener<TArgument>> Listeners { get; }
10+
ICollection<IArgumentGameEventListener<TArgument>> Listeners { get; }
1111

1212
/// <summary>
1313
/// Raise this event with an argument.

Assets/Runtime/GameEvents/Generic/IGameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public interface IGameEvent
77
/// <summary>
88
/// Currently registered listeners.
99
/// </summary>
10-
IEnumerable<IGameEventListener> Listeners { get; }
10+
ICollection<IGameEventListener> Listeners { get; }
1111

1212
/// <summary>
1313
/// Raise this event.

Assets/Runtime/GameEvents/Int/IntGameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace GameEvents.Int
55
{
6-
[CreateAssetMenu(fileName = "IntEvent", menuName = "Game Events/Int Game Event")]
6+
[CreateAssetMenu(fileName = "IntGameEvent", menuName = "Game Events/Int Game Event")]
77
public class IntGameEvent : ArgumentGameEvent<int>
88
{
99
}

Assets/Runtime/GameEvents/String/StringGameEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace GameEvents.String
55
{
6-
[CreateAssetMenu(fileName = "StringEvent", menuName = "Game Events/String Game Event")]
6+
[CreateAssetMenu(fileName = "StringGameEvent", menuName = "Game Events/String Game Event")]
77
public class StringGameEvent : ArgumentGameEvent<string>
88
{
99
}

Assets/Runtime/GameEvents/Vector3/Vector3GameEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace GameEvents.Vector3
55
{
6-
[AddComponentMenu("Game Events/Vector 3 Game Event Listener")]
6+
[AddComponentMenu("Game Events/Vector3 Game Event Listener")]
77
public class Vector3GameEventListener
88
: ArgumentGameEventListener<Vector3GameEvent, Vector3Event, UnityEngine.Vector3>
99
{

Assets/Runtime/MutableObjects/Generic/MutableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class MutableObject : ScriptableObject, IMutableObject
66
{
77
[SerializeField]
88
[Tooltip("When reset should be called for this object")]
9-
private ResetType resetType = ResetType.ActiveSceneChange;
9+
private ResetType resetType = ResetType.None;
1010

1111
public ResetType ResetType => resetType;
1212

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Available mutable objects:
6565
- `MutableVector3` - encapsulates a `Vector3` value.
6666

6767
Each mutable object has a `ResetType` property. This allows specifying when data in the mutable object should be reset. The following modes are available:
68-
- `None` - do not reset.
68+
- `None` - do not reset (default).
6969
- `ActiveSceneChange` - when the active (focused) scene changes.
7070
- `SceneUnloaded` - when the current scene gets unloaded.
7171
- `SceneLoaded` - when the scene is loaded.

0 commit comments

Comments
 (0)