8
8
using LootLocker . LootLockerEnums ;
9
9
using UnityEditor ;
10
10
using LootLocker . Requests ;
11
+ using Object = UnityEngine . Object ;
11
12
#if UNITY_EDITOR
12
13
using UnityEditorInternal ;
13
14
#endif
@@ -26,6 +27,7 @@ public class LootLockerServerApi : MonoBehaviour
26
27
private static int _instanceId = 0 ;
27
28
private const int MaxRetries = 3 ;
28
29
private int _tries ;
30
+ public GameObject HostingGameObject = null ;
29
31
30
32
public static void Instantiate ( )
31
33
{
@@ -37,8 +39,9 @@ public static void Instantiate()
37
39
gameObject . tag = "LootLockerServerApiGameObject" ;
38
40
}
39
41
40
- _instanceId = gameObject . GetInstanceID ( ) ;
41
42
_instance = gameObject . AddComponent < LootLockerServerApi > ( ) ;
43
+ _instanceId = _instance . GetInstanceID ( ) ;
44
+ _instance . HostingGameObject = gameObject ;
42
45
_instance . StartCoroutine ( CleanUpOldInstances ( ) ) ;
43
46
if ( Application . isPlaying )
44
47
DontDestroyOnLoad ( _instance . gameObject ) ;
@@ -47,17 +50,13 @@ public static void Instantiate()
47
50
48
51
public static IEnumerator CleanUpOldInstances ( )
49
52
{
50
- if ( ! _bTaggedGameObjects )
53
+ LootLockerServerApi [ ] serverApis = GameObject . FindObjectsByType < LootLockerServerApi > ( FindObjectsInactive . Include , FindObjectsSortMode . None ) ;
54
+ foreach ( LootLockerServerApi serverApi in serverApis )
51
55
{
52
- yield break ;
53
- }
54
- GameObject [ ] gameObjects = GameObject . FindGameObjectsWithTag ( "LootLockerServerApiGameObject" ) ;
55
- foreach ( GameObject gameObject in gameObjects )
56
- {
57
- if ( _instanceId != gameObject . GetInstanceID ( ) )
56
+ if ( serverApi != null && _instanceId != serverApi . GetInstanceID ( ) && serverApi . HostingGameObject != null )
58
57
{
59
58
#if UNITY_EDITOR
60
- DestroyImmediate ( gameObject ) ;
59
+ DestroyImmediate ( serverApi . HostingGameObject ) ;
61
60
#else
62
61
Destroy ( gameObject ) ;
63
62
#endif
@@ -84,20 +83,6 @@ private static void OnEnterPlaymodeInEditor(EnterPlayModeOptions options)
84
83
{
85
84
ResetInstance ( ) ;
86
85
}
87
-
88
- [ InitializeOnLoadMethod ]
89
- private static void CreateTag ( )
90
- {
91
- if ( InternalEditorUtility . tags . Contains ( "LootLockerServerApiGameObject" ) )
92
- {
93
- _bTaggedGameObjects = true ;
94
- }
95
- else
96
- {
97
- InternalEditorUtility . AddTag ( "LootLockerServerApiGameObject" ) ;
98
- _bTaggedGameObjects = true ;
99
- }
100
- }
101
86
#endif
102
87
103
88
public static void SendRequest ( LootLockerServerRequest request , Action < LootLockerResponse > OnServerResponse = null )
0 commit comments