|  | 
| 1 |  | -Documentation | 
|  | 1 | +# Documentation | 
|  | 2 | + | 
|  | 3 | +## Game Events | 
|  | 4 | +The library provides two types of game events: | 
|  | 5 | +- `GameEvent` without arguments. | 
|  | 6 | +- `ArgumentGameEvent` which accept arguments. | 
|  | 7 | + | 
|  | 8 | +### Game event assets | 
|  | 9 | +The core of events is game event `ScriptableObject` assets. They store game event listeners and notify them when an event is raised. | 
|  | 10 | + | 
|  | 11 | +To use game events, create an appropriate game event type asset (where _"Argument Name"_ is type of the argument that you are going to be passing to your events): | 
|  | 12 | +- `GameEvent` - _Right Click -> Create -> Game Events -> Game Event_ | 
|  | 13 | +- `ArgumentGameEvent` - _Right Click -> Create -> Game Events -> "Argument Name" Game Event_ | 
|  | 14 | + | 
|  | 15 | +Event assets can be referenced and raised in scripts directly or used in `UnityEvent` fields: | 
|  | 16 | +```cs | 
|  | 17 | +public class SceneManager : MonoBehaviour | 
|  | 18 | +{ | 
|  | 19 | + // Reference GameEvent directly. | 
|  | 20 | + [SerializedField] | 
|  | 21 | + private GameEvent startSceneGameEvent = default; | 
|  | 22 | + | 
|  | 23 | + // Or inside UnityEvent. | 
|  | 24 | + [SerializedField] | 
|  | 25 | + private UnityEvent onStartScene = default; | 
|  | 26 | + | 
|  | 27 | + private void Start() | 
|  | 28 | + { | 
|  | 29 | + if (startSceneGameEvent != null) | 
|  | 30 | + { | 
|  | 31 | + startSceneGameEvent.RaiseGameEvent(); | 
|  | 32 | + } | 
|  | 33 | + | 
|  | 34 | + onStartScene.Invoke(); | 
|  | 35 | + } | 
|  | 36 | +} | 
|  | 37 | +``` | 
|  | 38 | + | 
|  | 39 | +### Custom game events | 
|  | 40 | +If you need to define a game event which accepts custom arguments, extend `GameEvents.Generic.ArgumentGameEvent` class: | 
|  | 41 | +```cs | 
|  | 42 | +[CreateAssetMenu(fileName = "CustomGameEvent", menuName = "Game Events/Custom Game Event")] | 
|  | 43 | +public class CustomGameEvent : ArgumentGameEvent<Custom> | 
|  | 44 | +{ | 
|  | 45 | +} | 
|  | 46 | +``` | 
|  | 47 | + | 
|  | 48 | +In order to enable raising of custom game events from the inspector, create a `CustomEditor` script where the argument fields are going to be drawn. To do so, define an editor script, extend `GameEvents.Generic.ArgumentGameEventEditor` and override `DrawArgumentField(Custom)` . Make sure to place this script under the `Editor` folder: | 
|  | 49 | +```cs | 
|  | 50 | +[CustomEditor(typeof(CustomGameEvent))] | 
|  | 51 | +public class CustomGameEventEditor : ArgumentGameEventEditor<CustomGameEvent, Custom> | 
|  | 52 | +{ | 
|  | 53 | + protected override Custom DrawArgumentField(Custom value) | 
|  | 54 | + { | 
|  | 55 | + // Draw Custom value input fields here. | 
|  | 56 | + } | 
|  | 57 | +} | 
|  | 58 | +``` | 
|  | 59 | + | 
|  | 60 | +### Game event listeners | 
|  | 61 | +Game events can be listened to by listener components. To use listeners, you need to create an appropriate listener component: | 
|  | 62 | +- `GameEvent` - _Add Component -> Game Events -> Game Event Listener_ | 
|  | 63 | +- `ArgumentGameEvent` - _Add Component -> Game Events -> "Argument Name" Game Event Listener_ | 
|  | 64 | + | 
|  | 65 | +Once the component is added, slot in the appropriate `GameEvent` you want the listener to listen to and add your response methods to `onGameEvent` callback via the inspector. | 
|  | 66 | + | 
|  | 67 | +### Custom game event listeners | 
|  | 68 | +Custom game event listeners which accept different arguments can also be created. First of all, create a `UnityEvent` which would accept your `Custom` type: | 
|  | 69 | +```cs | 
|  | 70 | +[Serializable] | 
|  | 71 | +public class CustomEvent : UnityEvent<Custom> | 
|  | 72 | +{ | 
|  | 73 | +} | 
|  | 74 | +``` | 
|  | 75 | + | 
|  | 76 | +Then, create a custom game event listener which. This can be done by extending `GameEvents.Generic.ArgumentGameEventListener` class. | 
|  | 77 | +```cs | 
|  | 78 | +[AddComponentMenu("Game Events/Custom Game Event Listener")] | 
|  | 79 | +public class CustomGameEventListener : ArgumentGameEventListener<CustomGameEvent, CustomEvent, Custom> | 
|  | 80 | +{ | 
|  | 81 | +} | 
|  | 82 | +``` | 
|  | 83 | + | 
|  | 84 | +### Examples | 
|  | 85 | +Import the `GameEvents` samples which show how to use game events in various situations. | 
0 commit comments