-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathPersistentMonoSingleton.cs
33 lines (29 loc) · 1.04 KB
/
PersistentMonoSingleton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using UnityEngine;
namespace UnityCommunity.UnitySingleton
{
/// <summary>
/// This singleton is persistent across scenes by calling <see cref="UnityEngine.Object.DontDestroyOnLoad(Object)"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class PersistentMonoSingleton<T> : MonoSingleton<T> where T : MonoSingleton<T>
{
/// <summary>
/// if this is true, this singleton will auto detach if it finds itself parented on awake
/// </summary>
[Tooltip("if this is true, this singleton will auto detach if it finds itself parented on awake")]
[SerializeField] private bool UnparentOnAwake = true;
#region Protected Methods
protected override void OnInitializing()
{
if (UnparentOnAwake) {
transform.SetParent(null);
}
base.OnInitializing();
if (Application.isPlaying)
{
DontDestroyOnLoad(gameObject);
}
}
#endregion
}
}