UI 관련 스크립트 추가
All checks were successful
Code Review / code-review (pull_request) Successful in 9s

This commit is contained in:
SullyunShin
2025-04-30 17:55:20 +09:00
parent 860e0a5294
commit c26d94636d
5 changed files with 147 additions and 67 deletions

View File

@@ -1,105 +1,102 @@
using UnityEngine; using UnityEngine;
using System.Collections; 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; get
private static object _lock = new object();
protected static bool applicationIsQuitting = false;
public static bool IsApplicationQuitting
{ {
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." +
Debug.LogWarning("[Singleton] Instance '" + typeof(T) + " Won't create again - returning null.");
"' already destroyed on application quit." + return null;
" 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;
}
} }
}
void Awake()
{
lock (_lock) lock (_lock)
{ {
if (instance == null) if (instance == null)
{ {
instance = this as T; instance = FindFirstObjectByType(typeof(T)) as T;
if (instance == null)
} {
else if (instance != this) GameObject singleton = new GameObject(typeof(T).Name);
{ instance = singleton.AddComponent<T>();
Debug.LogError(this.name + " Destroy !!!"); instance.Init();
Destroy(this.gameObject); instance.name = typeof(T).ToString();
return; 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 #if UNITY_EDITOR
#else #else
applicationIsQuitting = true; applicationIsQuitting = true;
#endif #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(); public static T I
protected ISingleton() {
get
{ {
} lock (padlock)
public static T I
{
get
{ {
lock (padlock) if (instance == null)
{ {
if (instance == null) instance = new T();
{ (instance as ISingleton<T>).Init();
instance = new T();
(instance as ISingleton<T>).Init();
}
return instance;
} }
return instance;
} }
} }
public virtual void Init() { }
} }
public virtual void Init() { }
} }

View 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;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1ae5ce061e3746f4d841bab193d90076

View 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);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c83beb55043dab346a0cf9439add61ec