mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-14 00:07:15 +00:00
make StartupSingleton work in edit mode
This commit is contained in:
parent
fe9f17f1ec
commit
c19b485a42
2 changed files with 56 additions and 0 deletions
|
@ -1036,6 +1036,18 @@ namespace SanAndreasUnity.Utilities
|
|||
}
|
||||
}
|
||||
|
||||
public static bool IsAppInEditTime
|
||||
{
|
||||
get
|
||||
{
|
||||
#if !UNITY_EDITOR
|
||||
return false;
|
||||
#else
|
||||
return !UnityEditor.EditorApplication.isPlaying && !UnityEditor.EditorApplication.isPaused;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ScreenHasHighDensity => Application.isMobilePlatform;
|
||||
|
||||
|
||||
|
|
|
@ -6,7 +6,39 @@ namespace SanAndreasUnity.Utilities
|
|||
public class StartupSingleton<T> : MonoBehaviour
|
||||
where T : StartupSingleton<T>
|
||||
{
|
||||
#if !UNITY_EDITOR
|
||||
public static T Singleton { get; private set; }
|
||||
#else
|
||||
private static T s_cachedSingleton;
|
||||
public static T Singleton
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!F.IsAppInEditTime)
|
||||
{
|
||||
return s_cachedSingleton;
|
||||
}
|
||||
|
||||
if (s_cachedSingleton != null)
|
||||
return s_cachedSingleton;
|
||||
|
||||
T[] objects = FindObjectsOfType<T>();
|
||||
|
||||
if (objects.Length == 0)
|
||||
return null;
|
||||
|
||||
if (objects.Length > 1)
|
||||
throw new Exception($"Found multiple singleton objects of type {typeof(T).Name}. Make sure there is only 1 singleton object created per type.");
|
||||
|
||||
s_cachedSingleton = objects[0];
|
||||
return s_cachedSingleton;
|
||||
}
|
||||
private set
|
||||
{
|
||||
s_cachedSingleton = value;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
|
@ -24,6 +56,18 @@ namespace SanAndreasUnity.Utilities
|
|||
{
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (Singleton != this)
|
||||
return;
|
||||
|
||||
this.OnSingletonDisable();
|
||||
}
|
||||
|
||||
protected virtual void OnSingletonDisable()
|
||||
{
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (this != Singleton)
|
||||
|
|
Loading…
Reference in a new issue