프로젝트 Common 스크립트 추가
This commit is contained in:
47
Gameton-06/Assets/Gameton/Scripts/Common/SingletonBase.cs
Normal file
47
Gameton-06/Assets/Gameton/Scripts/Common/SingletonBase.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace TON
|
||||
{
|
||||
public class SingletonBase<T> : MonoBehaviour where T : class
|
||||
{
|
||||
public static T Singleton
|
||||
{
|
||||
get
|
||||
{
|
||||
return _instance.Value;
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Lazy<T> _instance = new Lazy<T>(() =>
|
||||
{
|
||||
T instance = FindObjectOfType(typeof(T)) as T;
|
||||
if (instance == null)
|
||||
{
|
||||
GameObject obj = new GameObject(typeof(T).Name);
|
||||
instance = obj.AddComponent(typeof(T)) as T;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (EditorApplication.isPlaying)
|
||||
{
|
||||
DontDestroyOnLoad(obj);
|
||||
}
|
||||
|
||||
#else
|
||||
DontDestroyOnLoad(obj);
|
||||
#endif
|
||||
}
|
||||
|
||||
return instance;
|
||||
});
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user