| | 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 | | { |
| 1912 | 11 | | public HashSet<Action> eventHashset = new HashSet<Action>(); |
| 1912 | 12 | | public List<Action> eventList = new List<Action>(); |
| | 13 | |
|
| | 14 | | public void Add(Action action) |
| | 15 | | { |
| 2176 | 16 | | if (Contains(action)) |
| 0 | 17 | | return; |
| | 18 | |
|
| 2176 | 19 | | eventList.Add(action); |
| 2176 | 20 | | eventHashset.Add(action); |
| 2176 | 21 | | } |
| | 22 | |
|
| | 23 | | public void Remove(Action action) |
| | 24 | | { |
| 199 | 25 | | int count = eventList.Count; |
| 1086 | 26 | | for (int i = 0; i < count; i++) |
| | 27 | | { |
| 543 | 28 | | if (eventList[i] == action) |
| | 29 | | { |
| 199 | 30 | | eventList.RemoveAt(i); |
| 199 | 31 | | eventHashset.Remove(action); |
| 199 | 32 | | return; |
| | 33 | | } |
| | 34 | | } |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public bool Contains(Action action) |
| | 38 | | { |
| 2176 | 39 | | int count = eventList.Count; |
| 9246 | 40 | | for (int i = 0; i < count; i++) |
| | 41 | | { |
| 2447 | 42 | | if (eventList[i] == action) |
| | 43 | | { |
| 0 | 44 | | return true; |
| | 45 | | } |
| | 46 | | } |
| 2176 | 47 | | return false; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | } |
| | 51 | |
|
| 478 | 52 | | public Dictionary<IUpdateEventHandler.EventType, UpdateEventCollection> eventCollections = new Dictionary<IUpdat |
| | 53 | |
|
| | 54 | | void Awake() |
| | 55 | | { |
| 478 | 56 | | EnsureEventType(IUpdateEventHandler.EventType.Update); |
| 478 | 57 | | EnsureEventType(IUpdateEventHandler.EventType.LateUpdate); |
| 478 | 58 | | EnsureEventType(IUpdateEventHandler.EventType.FixedUpdate); |
| 478 | 59 | | EnsureEventType(IUpdateEventHandler.EventType.OnDestroy); |
| 478 | 60 | | } |
| | 61 | |
|
| | 62 | | void EnsureEventType(IUpdateEventHandler.EventType eventType) |
| | 63 | | { |
| 1912 | 64 | | if (!ContainsEventType(eventType)) |
| | 65 | | { |
| 1912 | 66 | | eventCollections.Add(eventType, new UpdateEventCollection()); |
| | 67 | | } |
| 1912 | 68 | | } |
| | 69 | |
|
| | 70 | | private bool ContainsEventType(IUpdateEventHandler.EventType eventType) |
| | 71 | | { |
| 9560 | 72 | | foreach (var key in eventCollections.Keys) |
| | 73 | | { |
| 2868 | 74 | | if (key == eventType) |
| | 75 | | { |
| 0 | 76 | | return true; |
| | 77 | | } |
| | 78 | | } |
| 1912 | 79 | | return false; |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | void Dispatch( IUpdateEventHandler.EventType eventType ) |
| | 83 | | { |
| 42012 | 84 | | var list = eventCollections[eventType].eventList; |
| 42012 | 85 | | int count = eventCollections[eventType].eventList.Count; |
| | 86 | |
|
| 236404 | 87 | | for ( int i = 0; i < count; i++ ) |
| | 88 | | { |
| 76190 | 89 | | list[i].Invoke(); |
| | 90 | | } |
| 42012 | 91 | | } |
| | 92 | |
|
| | 93 | | void Update() |
| | 94 | | { |
| 19366 | 95 | | Dispatch(IUpdateEventHandler.EventType.Update); |
| 19366 | 96 | | } |
| | 97 | |
|
| | 98 | | void LateUpdate() |
| | 99 | | { |
| 19366 | 100 | | Dispatch(IUpdateEventHandler.EventType.LateUpdate); |
| 19366 | 101 | | } |
| | 102 | |
|
| | 103 | | void FixedUpdate() |
| | 104 | | { |
| 2802 | 105 | | Dispatch(IUpdateEventHandler.EventType.FixedUpdate); |
| 2802 | 106 | | } |
| | 107 | |
|
| | 108 | | private void OnDestroy() |
| | 109 | | { |
| 478 | 110 | | Dispatch(IUpdateEventHandler.EventType.OnDestroy); |
| 478 | 111 | | } |
| | 112 | | } |
| | 113 | | } |