|
6 | 6 | #endif |
7 | 7 |
|
8 | 8 | namespace Luau { |
9 | | - public static class AirshipBehaviourRootV3 { |
10 | | - private static int _idGen = 10000000; |
11 | | - |
12 | | - private static readonly Dictionary<GameObject, int> Ids = new(); |
13 | | - private static readonly Dictionary<int, GameObject> IdToGameObject = new(); |
14 | | - private static readonly Dictionary<int, HashSet<int>> GameObjectComponentIds = new(); |
15 | | - |
16 | | - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] |
17 | | - private static void ResetOnLoad() { |
18 | | - _idGen = 10000000; |
19 | | - Ids.Clear(); |
20 | | - IdToGameObject.Clear(); |
21 | | - } |
22 | | - |
23 | | - public static int GetId(GameObject gameObject) { |
24 | | - if (Ids.TryGetValue(gameObject, out var id)) return id; |
25 | | - |
26 | | - id = ++_idGen; |
27 | | - Ids.Add(gameObject, id); |
28 | | - IdToGameObject.Add(id, gameObject); |
29 | | - |
30 | | - return id; |
31 | | - } |
32 | | - |
33 | | - public static int GetId(Component component) { |
34 | | - return GetId(component.gameObject); |
35 | | - } |
36 | | - |
37 | | - /// <summary> |
38 | | - /// This will ensure that the parent GameObject has a reference to the AirshipComponent |
39 | | - /// </summary> |
40 | | - internal static void LinkComponentToGameObject(AirshipComponentV2 component, out int gameObjectId) { |
41 | | - gameObjectId = GetId(component.gameObject); |
42 | | - var componentId = component.GetAirshipComponentId(); |
43 | | - |
44 | | - if (!GameObjectComponentIds.TryGetValue(gameObjectId, out var componentIds)) { |
45 | | - componentIds = new HashSet<int>(); |
46 | | - GameObjectComponentIds.Add(gameObjectId, componentIds); |
47 | | - } |
48 | | - |
49 | | - componentIds.Add(componentId); |
50 | | - } |
51 | | - |
52 | | - internal static void CleanIdOnDestroy(GameObject gameObject, AirshipComponentV2 component) { |
53 | | - if (!Ids.TryGetValue(gameObject, out var id)) return; |
54 | | - |
55 | | - var components = gameObject.GetComponents<AirshipComponentV2>().Where(c => c != component); |
56 | | - if (components.Any() && gameObject.activeInHierarchy) return; |
57 | | - |
58 | | - var componentIds = GameObjectComponentIds[id]; |
59 | | - |
60 | | - componentIds.Remove(component.GetAirshipComponentId()); |
61 | | - |
62 | | - if (componentIds.Count != 0) return; |
63 | | - |
64 | | - // If no more components, we'll remove Id <-> GameObject mappings, tyvm |
65 | | - Ids.Remove(gameObject); |
66 | | - IdToGameObject.Remove(id); |
67 | | - GameObjectComponentIds.Remove(id); |
68 | | - } |
69 | | - |
70 | | - public static bool HasId(GameObject gameObject) { |
71 | | - return Ids.ContainsKey(gameObject); |
72 | | - } |
73 | | - |
74 | | - public static GameObject GetGameObject(int objectId) { |
75 | | - return IdToGameObject.GetValueOrDefault(objectId); |
76 | | - } |
77 | | - |
78 | | - public static AirshipComponentV2 GetComponent(GameObject gameObject, int componentId) { |
79 | | - return gameObject != null |
80 | | - ? gameObject.GetComponents<AirshipComponentV2>() |
81 | | - .FirstOrDefault(f => f.GetAirshipComponentId() == componentId) |
82 | | - : null; |
83 | | - } |
84 | | - |
85 | | - public static AirshipComponentV2 GetComponent(int unityInstanceId, int componentId) { |
86 | | - return GetComponent(GetGameObject(unityInstanceId), componentId); |
87 | | - } |
88 | | - } |
89 | | - |
90 | 9 | public static class AirshipBehaviourRootV2 { |
91 | 10 | private static int _idGen; |
92 | 11 |
|
|
0 commit comments