Tanoda
EnumEventTable.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
13
14namespace Leap.Unity {
15
16 [Serializable]
17 public class EnumEventTable : ISerializationCallbackReceiver {
18
19 [Serializable]
20 private class Entry {
21
22 #pragma warning disable 0649
23 [SerializeField]
24 public int enumValue;
25
26 [SerializeField]
27 public UnityEvent callback;
28 #pragma warning restore 0649
29 }
30
31 //The actual serialized list of entries
32 [SerializeField]
33 private List<Entry> _entries = new List<Entry>();
34
35 //Not serialized, is just populated after deserialization and used
36 //when calling Invoke for speed.
37 private Dictionary<int, UnityEvent> _entryMap = new Dictionary<int, UnityEvent>();
38
39 public bool HasUnityEvent(int enumValue) {
40 UnityEvent callback;
41 if (_entryMap.TryGetValue(enumValue, out callback)) {
42 return callback.GetPersistentEventCount() > 0;
43 }
44 else {
45 return false;
46 }
47 }
48
49 public void Invoke(int enumValue) {
50 UnityEvent callback;
51 if (_entryMap.TryGetValue(enumValue, out callback)) {
52 callback.Invoke();
53 }
54 }
55
56 public void OnBeforeSerialize() { }
57
58 public void OnAfterDeserialize() {
59 if (_entryMap == null) {
60 _entryMap = new Dictionary<int, UnityEvent>();
61 } else {
62 _entryMap.Clear();
63 }
64
65 foreach (var entry in _entries) {
66 _entryMap[entry.enumValue] = entry.callback;
67 }
68 }
69 }
70}
bool HasUnityEvent(int enumValue)
void Invoke(int enumValue)