Space Ad Mobile 9.5.1
Space Ad Mobile makes ads easy to add into your mobile game: one unified workflow for LevelPlay, AdMob, Yandex.Ads across banners, interstitials and rewarded.
Loading...
Searching...
No Matches
UndestroyableUtility.cs
1using System.Collections.Generic;
2using UnityEngine;
3
4namespace SCG.SpaceAdMobile
5{
12 [DisallowMultipleComponent]
13 [DefaultExecutionOrder(-1)]
15 {
16 private static readonly Dictionary<string, Transform> instances = new();
17
18 [Tooltip("Enable to preserve this object across scene changes.")]
19 [SerializeField]
20 private new bool enabled = true;
21
26 public bool IsDestroyed { get; private set; }
27
31 protected virtual void Awake()
32 {
33 if (!enabled)
34 return;
35
36 if (instances.TryGetValue(Id, out var existing))
37 {
38 // A null result indicates the other object was destroyed for some reason
39 if (existing != null)
40 {
41 if (ReferenceEquals(transform, existing))
42 return;
43 Destroy(transform.gameObject);
44 // Return to skip the following registration code
45 return;
46 }
47 }
48
49 // The following code registers this Transform regardless of whether it's new or replacing
50 instances[Id] = transform;
51 if (transform != transform.root)
52 {
53 transform.SetParent(null);
54 }
55 DontDestroyOnLoad(transform);
56 }
57
61 protected virtual void OnDisable() => IsDestroyed = gameObject.activeInHierarchy;
62
66 protected virtual void OnApplicationQuit() => instances.Clear();
67 }
68}
virtual string Id
The unique identifier for the object instance. It is serialized and can be viewed and modified in the...
Provides a base class for objects requiring a unique identifier, using MonoBehaviour as its foundatio...
virtual void Awake()
Initializes the component during Unity's Awake callback.
virtual void OnApplicationQuit()
Handles application shutdown.
virtual void OnDisable()
Handles the Unity disable callback.
bool IsDestroyed
Indicates whether the object has been destroyed. It becomes true if the object is explicitly destroye...
A utility component to make an object persist across different scenes in a Unity project....