| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class UpdateDispatcher : MonoBehaviour |
| | 8 | | { |
| | 9 | | public class UpdateEventCollection |
| | 10 | | { |
| 2692 | 11 | | public HashSet<Action> eventHashset = new HashSet<Action>(); |
| 2692 | 12 | | public List<Action> eventList = new List<Action>(); |
| | 13 | |
|
| | 14 | | public void Add(Action action) |
| | 15 | | { |
| 3204 | 16 | | if ( eventHashset.Contains(action)) |
| 0 | 17 | | return; |
| | 18 | |
|
| 3204 | 19 | | eventList.Add(action); |
| 3204 | 20 | | eventHashset.Add(action); |
| 3204 | 21 | | } |
| | 22 | |
|
| | 23 | | public void Remove(Action action) |
| | 24 | | { |
| 21 | 25 | | if ( !eventHashset.Contains(action)) |
| 3 | 26 | | return; |
| | 27 | |
|
| 18 | 28 | | eventList.Remove(action); |
| 18 | 29 | | eventHashset.Remove(action); |
| 18 | 30 | | } |
| | 31 | |
|
| | 32 | | public bool Contains(Action action) |
| | 33 | | { |
| 0 | 34 | | return eventHashset.Contains(action); |
| | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| 673 | 38 | | public Dictionary<IUpdateEventHandler.EventType, UpdateEventCollection> eventCollections = new Dictionary<IUpdat |
| | 39 | |
|
| | 40 | | void Awake() |
| | 41 | | { |
| 673 | 42 | | EnsureEventType(IUpdateEventHandler.EventType.Update); |
| 673 | 43 | | EnsureEventType(IUpdateEventHandler.EventType.LateUpdate); |
| 673 | 44 | | EnsureEventType(IUpdateEventHandler.EventType.FixedUpdate); |
| 673 | 45 | | EnsureEventType(IUpdateEventHandler.EventType.OnGui); |
| 673 | 46 | | } |
| | 47 | |
|
| | 48 | | void EnsureEventType( IUpdateEventHandler.EventType eventType ) |
| | 49 | | { |
| 2692 | 50 | | if ( !eventCollections.ContainsKey(eventType) ) |
| 2692 | 51 | | eventCollections.Add(eventType, new UpdateEventCollection()); |
| 2692 | 52 | | } |
| | 53 | |
|
| | 54 | | void Dispatch( IUpdateEventHandler.EventType eventType ) |
| | 55 | | { |
| 23130 | 56 | | var list = eventCollections[eventType].eventList; |
| 23130 | 57 | | int count = eventCollections[eventType].eventList.Count; |
| | 58 | |
|
| 117012 | 59 | | for ( int i = 0; i < count; i++ ) |
| | 60 | | { |
| 35376 | 61 | | list[i].Invoke(); |
| | 62 | | } |
| 23130 | 63 | | } |
| | 64 | |
|
| | 65 | | void Update() |
| | 66 | | { |
| 7622 | 67 | | Dispatch(IUpdateEventHandler.EventType.Update); |
| 7622 | 68 | | } |
| | 69 | |
|
| | 70 | | void LateUpdate() |
| | 71 | | { |
| 7622 | 72 | | Dispatch(IUpdateEventHandler.EventType.LateUpdate); |
| 7622 | 73 | | } |
| | 74 | |
|
| | 75 | | void FixedUpdate() |
| | 76 | | { |
| 4782 | 77 | | Dispatch(IUpdateEventHandler.EventType.FixedUpdate); |
| 4782 | 78 | | } |
| | 79 | |
|
| | 80 | | void OnGUI() |
| | 81 | | { |
| 3104 | 82 | | Dispatch(IUpdateEventHandler.EventType.OnGui); |
| 3104 | 83 | | } |
| | 84 | | } |
| | 85 | | } |