This commit is contained in:
@@ -1,105 +1,102 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
namespace XED.Core
|
||||
public abstract class UnitySingleton<T> : UnityEngine.MonoBehaviour where T : UnitySingleton<T>
|
||||
{
|
||||
public abstract class UnitySingleton<T> : UnityEngine.MonoBehaviour where T : UnitySingleton<T>
|
||||
private static T instance;
|
||||
private static object _lock = new object();
|
||||
protected static bool applicationIsQuitting = false;
|
||||
public static bool IsApplicationQuitting
|
||||
{
|
||||
private static T instance;
|
||||
private static object _lock = new object();
|
||||
protected static bool applicationIsQuitting = false;
|
||||
public static bool IsApplicationQuitting
|
||||
get
|
||||
{
|
||||
get
|
||||
{
|
||||
return applicationIsQuitting;
|
||||
}
|
||||
return applicationIsQuitting;
|
||||
}
|
||||
}
|
||||
|
||||
public static T I
|
||||
public static T I
|
||||
{
|
||||
get
|
||||
{
|
||||
get
|
||||
if (applicationIsQuitting)
|
||||
{
|
||||
if (applicationIsQuitting)
|
||||
{
|
||||
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
|
||||
"' already destroyed on application quit." +
|
||||
" Won't create again - returning null.");
|
||||
return null;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = FindFirstObjectByType(typeof(T)) as T;
|
||||
if (instance == null)
|
||||
{
|
||||
GameObject singleton = new GameObject(typeof(T).Name);
|
||||
instance = singleton.AddComponent<T>();
|
||||
instance.Init();
|
||||
instance.name = typeof(T).ToString();
|
||||
DontDestroyOnLoad(instance.gameObject);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
|
||||
"' already destroyed on application quit." +
|
||||
" Won't create again - returning null.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this as T;
|
||||
|
||||
}
|
||||
else if (instance != this)
|
||||
{
|
||||
Debug.LogError(this.name + " Destroy !!!");
|
||||
Destroy(this.gameObject);
|
||||
return;
|
||||
instance = FindFirstObjectByType(typeof(T)) as T;
|
||||
if (instance == null)
|
||||
{
|
||||
GameObject singleton = new GameObject(typeof(T).Name);
|
||||
instance = singleton.AddComponent<T>();
|
||||
instance.Init();
|
||||
instance.name = typeof(T).ToString();
|
||||
DontDestroyOnLoad(instance.gameObject);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void Init()
|
||||
void Awake()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = this as T;
|
||||
|
||||
}
|
||||
else if (instance != this)
|
||||
{
|
||||
Debug.LogError(this.name + " Destroy !!!");
|
||||
Destroy(this.gameObject);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnApplicationQuit()
|
||||
{
|
||||
protected virtual void Init()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void OnApplicationQuit()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
#else
|
||||
applicationIsQuitting = true;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class ISingleton<T> where T : class, new()
|
||||
public abstract class ISingleton<T> where T : class, new()
|
||||
{
|
||||
static volatile T instance = default(T);
|
||||
static readonly object padlock = new object();
|
||||
protected ISingleton()
|
||||
{
|
||||
static volatile T instance = default(T);
|
||||
static readonly object padlock = new object();
|
||||
protected ISingleton()
|
||||
}
|
||||
public static T I
|
||||
{
|
||||
get
|
||||
{
|
||||
}
|
||||
public static T I
|
||||
{
|
||||
get
|
||||
lock (padlock)
|
||||
{
|
||||
lock (padlock)
|
||||
if (instance == null)
|
||||
{
|
||||
if (instance == null)
|
||||
{
|
||||
instance = new T();
|
||||
(instance as ISingleton<T>).Init();
|
||||
}
|
||||
return instance;
|
||||
instance = new T();
|
||||
(instance as ISingleton<T>).Init();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
public virtual void Init() { }
|
||||
}
|
||||
public virtual void Init() { }
|
||||
}
|
||||
26
Assets/Scripts/UI/TextPopup.cs
Normal file
26
Assets/Scripts/UI/TextPopup.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System.Collections;
|
||||
|
||||
public class TextPopup : UnitySingleton<TextPopup>
|
||||
{
|
||||
public GameObject uiObj;
|
||||
public TMP_Text log;
|
||||
public void Show(string text, float showTime)
|
||||
{
|
||||
log.text = text;
|
||||
uiObj.SetActive(true);
|
||||
StopAllCoroutines();
|
||||
StartCoroutine(ShowTextCoroutine(showTime));
|
||||
}
|
||||
IEnumerator ShowTextCoroutine(float time)
|
||||
{
|
||||
while (time >= 0)
|
||||
{
|
||||
time -= Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
uiObj.SetActive(false);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/TextPopup.cs.meta
Normal file
2
Assets/Scripts/UI/TextPopup.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1ae5ce061e3746f4d841bab193d90076
|
||||
53
Assets/Scripts/UI/UIDragSpawner.cs
Normal file
53
Assets/Scripts/UI/UIDragSpawner.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class UIDragSpawner : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public GameObject prefabToSpawn;
|
||||
public Canvas canvas;
|
||||
private RectTransform rectTransform;
|
||||
private CanvasGroup canvasGroup;
|
||||
private GameObject dragPreview;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
rectTransform = GetComponent<RectTransform>();
|
||||
//canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
canvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
if (canvasGroup == null)
|
||||
canvasGroup = gameObject.AddComponent<CanvasGroup>();
|
||||
canvasGroup.alpha = 0.6f;
|
||||
canvasGroup.blocksRaycasts = false;
|
||||
|
||||
dragPreview = Instantiate(gameObject, canvas.transform);
|
||||
dragPreview.GetComponent<CanvasGroup>().blocksRaycasts = false;
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (dragPreview)
|
||||
{
|
||||
dragPreview.GetComponent<RectTransform>().position = eventData.position;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
canvasGroup.alpha = 1f;
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
|
||||
if (dragPreview)
|
||||
Destroy(dragPreview);
|
||||
|
||||
Ray ray = Camera.main.ScreenPointToRay(eventData.position);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
Instantiate(prefabToSpawn, hit.point, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/UIDragSpawner.cs.meta
Normal file
2
Assets/Scripts/UI/UIDragSpawner.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c83beb55043dab346a0cf9439add61ec
|
||||
Reference in New Issue
Block a user