-
Notifications
You must be signed in to change notification settings - Fork 114
Events
Events also exist as ScriptableObjects in your project. They can contain any single parameter when raising or none. Events are also fully debuggable through the editor using the stacktrace window and the raise button functionality, which also supports the ability to raise events with parameters. If a parameter is specified, a field will be present which allows you to select which value will be passed.
Sometimes you want to be able to pass along some data when raising an event like a float
or more comprehensive types like GameObject
or even types you've created yourself. To do so you simply need to use a GameEvent of said type, so if you want to pass along a float, you simply use the even called FloatGameEvent
.
If the type you want to pass along isn't included in the package, such as your own types, simply use the code generator to create the necessary script files for you! To do so, simply do the following:
- In the top of the Unity window, select the menu item called Window > SO Code Generation
- Tick off the field called "Game Event" 2a. (Optional) It's recommended that you also tick off the field called "Event Listener" so you can create event listeners for your custom event as well.
- In the Type Name field, type in the exact name of the type you wish to support - capitalization matters!
- In the Menu Name field, type in the name you want to appear in the context menu and Asset menu. This can be anything you'd like, and does support submenus using the '/' character
- Click "Generate" This will automatically create all the script files for you. You should now be able to create your custom event type the same way you would any other game event type.
If you chose to tick off event listener as described in Step 2a and are wondering how to use it properly, please feel free to read the documentation on even listeners - it describes how typed event listeners work.
Note that you must provide an Event Listener in order to subscribe to events
The base class differs depending on whether an argument is expected or not
void Raise(T)
void RegisterListener(IGameEventListener<T>)
void UnregisterListener(IGameEventListener<T>)
void Raise()
void RegisterListener(IGameEventListener)
void UnregisterListener(IGameEventListener)