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

Commit 9e73d32

Browse files
authored
Inspector improvements (#15)
* Remove game event null checks on enable and disable. Expose listener enumerable. Draw a list of references to event listeners on events. Implement editor for all built-in event types. * Update changelog * Add tests for expose Listeners field * Update readme * Fix changelog order * Fixup changelog, copy over readme * Make resetting more flexible, update version * Update package doc * Update docs * Flip assert args
1 parent 51c1e8a commit 9e73d32

32 files changed

+480
-147
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ 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+
## [0.7.0] - 2020-10-10
8+
9+
### Added
10+
- List of references to event listeners on game events will be drawn during play mode.
11+
- Exposed listener enumerable.
12+
- Editors for all built-in game event types.
13+
- More tests!
14+
15+
### Changed
16+
- `Persisting` flag was changed to `ResetType` enum which allows more customization.
17+
18+
### Removed
19+
- Game event null checks on enable and disable.
20+
721
## [0.6.0] - 2020-10-07
822

923
### Changed

Documentation~/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ Available mutable objects:
4141
- `MutableVector2` - encapsulates a `Vector2` value.
4242
- `MutableVector3` - encapsulates a `Vector3` value.
4343

44-
If a mutable object value should be saved between scene loads, check the `Persisting` flag on the `MutableObject` asset.
44+
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.
46+
- `ActiveSceneChange` - when the active (focused) scene changes.
47+
- `SceneUnloaded` - when the current scene gets unloaded.
48+
- `SceneLoaded` - when the scene is loaded.
4549

4650
### Custom game events
4751
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.
@@ -70,6 +74,21 @@ public class CustomGameEventListener : ArgumentGameEventListener<CustomGameEvent
7074
}
7175
```
7276

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.
78+
```cs
79+
[CustomEditor(typeof(CustomGameEvent))]
80+
public class GameObjectGameEventEditor : ArgumentGameEventEditor<CustomGameEvent, Custom>
81+
{
82+
protected override Custom DrawArgumentField(Custom value)
83+
{
84+
var fieldValue = EditorGUILayout
85+
.ObjectField(value, typeof(Custom), true);
86+
87+
return fieldValue as Custom;
88+
}
89+
}
90+
```
91+
7392
### Custom mutable objects
7493
In some cases, littering the script code with loads of `MutableObject` references can be inconvenient. To avoid this, a single object can be used which encompasses multiple fields.
7594

@@ -95,7 +114,6 @@ public class MutableCustom : MutableObject
95114

96115
// This will set property values when mutable object is enabled or if the values change in the
97116
// inspector.
98-
// This is also called when MutableObjectExtensions.ResetMutatedObjects() is invoked.
99117
public override void ResetValues()
100118
{
101119
Health = health;

Editor/GameEvents/Game/GameEventEditor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public override void OnInspectorGUI()
2424
GUILayout.Space(GroupSpacingPixels);
2525

2626
DrawRaise(gameEvent);
27+
28+
if (!Application.isPlaying)
29+
{
30+
return;
31+
}
32+
33+
GUILayout.Space(GroupSpacingPixels);
34+
DrawListeners(gameEvent);
2735
}
2836

2937
private static void DrawRaise(IGameEvent gameEvent)
@@ -34,5 +42,17 @@ private static void DrawRaise(IGameEvent gameEvent)
3442
gameEvent.RaiseGameEvent();
3543
}
3644
}
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+
}
3757
}
3858
}

Editor/GameEvents/GameObject.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using GameEvents.Generic;
2+
using UnityEditor;
3+
4+
namespace GameEvents.GameObject
5+
{
6+
[CustomEditor(typeof(GameObjectGameEvent))]
7+
public class GameObjectGameEventEditor
8+
: ArgumentGameEventEditor<GameObjectGameEvent, UnityEngine.GameObject>
9+
{
10+
protected override UnityEngine.GameObject DrawArgumentField(UnityEngine.GameObject value)
11+
{
12+
var fieldValue = EditorGUILayout
13+
.ObjectField(value, typeof(UnityEngine.GameObject), true);
14+
15+
return fieldValue as UnityEngine.GameObject;
16+
}
17+
}
18+
}

Editor/GameEvents/GameObject/GameObjectGameEventEditor.cs.meta

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

Editor/GameEvents/Generic/ArgumentGameEventEditor.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public override void OnInspectorGUI()
2424
GUILayout.Space(GroupSpacingPixels);
2525

2626
DrawRaise(gameEvent);
27+
28+
if (!Application.isPlaying)
29+
{
30+
return;
31+
}
32+
33+
GUILayout.Space(GroupSpacingPixels);
34+
DrawListeners(gameEvent);
2735
}
2836

2937
private void DrawRaise(TGameEvent gameEvent)
@@ -40,6 +48,18 @@ private void DrawRaise(TGameEvent gameEvent)
4048
GUILayout.EndHorizontal();
4149
}
4250

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+
4363
/// <returns>
4464
/// Value that is entered in the argument field.
4565
/// </returns>

Editor/GameEvents/Transform.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.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using GameEvents.Generic;
2+
using UnityEditor;
3+
4+
namespace GameEvents.Transform
5+
{
6+
[CustomEditor(typeof(TransformGameEvent))]
7+
public class TransformGameEventEditor
8+
: ArgumentGameEventEditor<TransformGameEvent, UnityEngine.Transform>
9+
{
10+
protected override UnityEngine.Transform DrawArgumentField(UnityEngine.Transform value)
11+
{
12+
var fieldValue = EditorGUILayout
13+
.ObjectField(value, typeof(UnityEngine.Transform), true);
14+
15+
return fieldValue as UnityEngine.Transform;
16+
}
17+
}
18+
}

Editor/GameEvents/Transform/GameObjectGameEventEditor.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.

0 commit comments

Comments
 (0)