프로젝트 Common 스크립트 추가

This commit is contained in:
aube.lee
2025-01-21 22:53:52 +09:00
parent 8c2341ff7e
commit 3bb9913d16
38 changed files with 1672 additions and 121 deletions

View File

@@ -0,0 +1,51 @@
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace TON
{
/// <summary> Main을 타고 실행한 것처러므 씬이 동작되도록 도와주는 도우미 클래스 </summary>
public class BootStrapper
{
private const string BootStrapperMenuPath = "Game One/BootStrapper/Activate Ingame System";
private static bool IsActivateBootStrapper
{
get => UnityEditor.EditorPrefs.GetBool(BootStrapperMenuPath, false);
set
{
UnityEditor.EditorPrefs.SetBool(BootStrapperMenuPath, value);
UnityEditor.Menu.SetChecked(BootStrapperMenuPath, value);
}
}
[UnityEditor.MenuItem(BootStrapperMenuPath, false)]
private static void ToggleActivateBootStrapper()
{
IsActivateBootStrapper = !IsActivateBootStrapper;
UnityEditor.Menu.SetChecked(BootStrapperMenuPath, IsActivateBootStrapper);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void SystemBoot()
{
Scene activeScene = EditorSceneManager.GetActiveScene();
if (IsActivateBootStrapper && false == activeScene.name.Equals("Main"))
{
InternalBoot();
}
}
private static void InternalBoot()
{
Main.Singleton.Initialize();
// TODO : Custom Order After System Load
// UIManager.Show<IngameUI>(UIList.IngameUI);
// UIManager.Show<LogUI>(UIList.LogUI);
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3a8bac5825e1be40a3aa098116c4728
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public class InputSystem : MonoBehaviour
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 94c58e7fc0b907b43bbe208a18ca6ded
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,120 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace TON
{
public enum SceneType
{
None,
Empty,
Title,
Ingame,
}
public class Main : SingletonBase<Main>
{
private bool isIniaialized = false;
public void Initialize()
{
if (isIniaialized)
return;
// 게임에 필요한 필수 시스템 초기화
UIManager.Singleton.Initalize();
// TODO : GameDataModel.Singleton.Initalize();
isIniaialized = true;
}
private void Start()
{
Initialize();
#if UNITY_EDITOR
Scene activeScene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();
if (activeScene.name.Equals("Main"))
{
ChangeScene(SceneType.Title);
}
#else
ChangeScene(SceneType.Title);
#endif
}
bool isSceneChangeProgress = false;
SceneBase currentSceneController = null;
SceneType currentSceneType = SceneType.None;
public void ChangeScene(SceneType sceneType, System.Action onSceneChangeCompletedCallback = null)
{
if (isSceneChangeProgress)
return;
if (currentSceneType == sceneType)
return;
currentSceneType = sceneType;
switch (sceneType)
{
case SceneType.Title:
StartCoroutine(ChangeScene<TitleScene>(onSceneChangeCompletedCallback));
break;
case SceneType.Ingame:
StartCoroutine(ChangeScene<IngameScene>(onSceneChangeCompletedCallback));
break;
}
}
private IEnumerator ChangeScene<T>(System.Action onSceneChangeCompletedCallback = null) where T : SceneBase
{
UIManager.Show<LoadingUI>(UIList.LoadingUI);
yield return new WaitForSeconds(3f);
isSceneChangeProgress = true;
// 기존에 불러두었던 씬 컨트롤러(Scene Base)가 있다면, OnEnd를 호출해주고 삭제한다.
if (currentSceneController != null)
{
yield return StartCoroutine(currentSceneController?.OnEnd());
Destroy(currentSceneController.gameObject);
currentSceneController = null;
}
// Empty 씬으로 전환을 먼저 한다
AsyncOperation emptySceneLoad = SceneManager.LoadSceneAsync("Empty", LoadSceneMode.Single);
while (!emptySceneLoad.isDone)
{
yield return null;
}
// 새로운 씬 컨트롤러를 생성한다.
GameObject newSceneController = new GameObject(typeof(T).Name);
newSceneController.transform.SetParent(transform);
currentSceneController = newSceneController.AddComponent<T>();
yield return StartCoroutine(currentSceneController.OnStart());
// 씬 전환을 종료했다고 플래그 값을 변경한다.
isSceneChangeProgress = false;
// 씬 전환 후 - 콜백 함수를 호출해준다.
onSceneChangeCompletedCallback?.Invoke();
UIManager.Hide<LoadingUI>(UIList.LoadingUI);
}
internal void SystemQuit()
{
// TODO: 게임 종료 전 처리할 것들을 추가
// 게임 종료
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 364865c88d1f27e45b5697a007bcba78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public abstract class SceneBase : MonoBehaviour
{
public abstract IEnumerator OnStart();
public abstract IEnumerator OnEnd();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c6b8a25fe16551241a01e82fdd6b1c24
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e60be2f565d8334ca8bde4d14b67ec4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public class UIBase : MonoBehaviour
{
public virtual void Show()
{
gameObject.SetActive(true);
}
public virtual void Hide()
{
gameObject.SetActive(false);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d8bca0d4dac93b34e8249704224536c3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,25 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public enum UIList
{
PANEL_START,
TitleUI,
IngameUI,
LogUI,
PANEL_END,
POPUP_START,
PausePopupUI,
LoadingUI,
POPUP_END,
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e7771782ad5d4d14e9f71884356ae993
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,108 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace TON
{
public class UIManager : SingletonBase<UIManager>
{
public static T Show<T>(UIList uiName) where T : UIBase
{
var targetUI = Singleton.GetUI<T>(uiName);
if (targetUI == null)
{
return null;
}
targetUI.Show();
return targetUI;
}
public static T Hide<T>(UIList uiName) where T : UIBase
{
var targetUI = Singleton.GetUI<T>(uiName);
if (targetUI == null)
{
return null;
}
targetUI.Hide();
return targetUI;
}
private Dictionary<UIList, UIBase> panels = new Dictionary<UIList, UIBase>();
private Dictionary<UIList, UIBase> popups = new Dictionary<UIList, UIBase>();
private Transform panelRoot;
private Transform popupRoot;
private const string UI_PATH = "UI/Prefabs/";
public void Initalize()
{
if (panelRoot == null)
{
GameObject panelGo = new GameObject("Panel Root");
panelRoot = panelGo.transform;
panelRoot.SetParent(transform);
}
if (popupRoot == null)
{
GameObject popupGo = new GameObject("Popup Root");
popupRoot = popupGo.transform;
popupRoot.SetParent(transform);
}
for (int i = (int)UIList.PANEL_START + 1; i < (int)UIList.PANEL_END; i++)
{
panels.Add((UIList)i, null);
}
for (int i = (int)UIList.POPUP_START + 1; i < (int)UIList.POPUP_END; i++)
{
popups.Add((UIList)i, null);
}
}
public T GetUI<T>(UIList uiName, bool isReload = false) where T : UIBase
{
Dictionary<UIList, UIBase> container = null;
if (uiName > UIList.PANEL_START && uiName < UIList.PANEL_END)
{
container = panels;
}
else
{
container = popups;
}
if (!container.ContainsKey(uiName))
{
return null;
}
if (isReload && container[uiName])
{
Destroy(container[uiName].gameObject);
container[uiName] = null;
}
if (!container[uiName])
{
string path = UI_PATH + $"UI.{uiName}";
T result = Resources.Load<UIBase>(path) as T;
if (result)
{
container[uiName] = Instantiate(result, container == panels ? panelRoot : popupRoot);
container[uiName].gameObject.SetActive(false);
}
}
return container[uiName] as T;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fed807c49e259cf45b2b4b591f941822
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: