| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class UpdateEventHandler : IUpdateEventHandler |
| | 10 | | { |
| | 11 | | private UpdateDispatcher dispatcher; |
| | 12 | | private const string UPDATE_DISPATCHER = "_UpdateDispatcher"; |
| | 13 | |
|
| 478 | 14 | | public UpdateEventHandler () |
| | 15 | | { |
| 478 | 16 | | var go = new GameObject(); |
| | 17 | | #if UNITY_EDITOR |
| 478 | 18 | | go.name = UPDATE_DISPATCHER; |
| | 19 | | #endif |
| 478 | 20 | | dispatcher = go.AddComponent<UpdateDispatcher>(); |
| 478 | 21 | | } |
| | 22 | |
|
| | 23 | | public void AddListener( IUpdateEventHandler.EventType eventType, Action action ) |
| | 24 | | { |
| 2176 | 25 | | Assert.IsTrue( dispatcher != null, $"Dispatcher is null! Listener for event {eventType} couldn't be added!") |
| 2176 | 26 | | dispatcher.eventCollections[eventType].Add(action); |
| 2176 | 27 | | } |
| | 28 | |
|
| | 29 | | public void RemoveListener( IUpdateEventHandler.EventType eventType, Action action ) |
| | 30 | | { |
| 1899 | 31 | | if ( dispatcher == null ) |
| 1700 | 32 | | return; |
| | 33 | |
|
| 199 | 34 | | dispatcher.eventCollections[eventType].Remove(action); |
| 199 | 35 | | } |
| | 36 | |
|
| | 37 | | public void Dispose() |
| | 38 | | { |
| 478 | 39 | | if ( dispatcher != null ) |
| | 40 | | { |
| 478 | 41 | | UnityEngine.Object.Destroy( dispatcher.gameObject ); |
| 478 | 42 | | dispatcher = null; |
| | 43 | | } |
| 478 | 44 | | } |
| | 45 | |
|
| | 46 | | public void Initialize() |
| | 47 | | { |
| 478 | 48 | | } |
| | 49 | | } |
| | 50 | | } |