111 lines
4.1 KiB
C#
111 lines
4.1 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using UnityEngine.UI;
|
|
|
|
namespace TKS.OutGame
|
|
{
|
|
/// <summary>
|
|
/// 씬 바꿈 페이드(PD 2026-11-16 「타이틀 화면은 별도의 씬으로 구성」) — 씬이 바뀌어도 남는 화면 덮개(오버레이 캔버스 · 씬 카메라와 무관)로
|
|
/// 페이드 아웃 → 씬 불러오기 → 검정 유지 → 페이드 인 → 스스로 사라짐. 이미 검정이면(FromBlack) 페이드 아웃 없이 바로 불러옴.
|
|
/// 시계 = LoadingScreen.Now(확인 캡처 때 게임 시간과 같음).
|
|
/// </summary>
|
|
public class SceneSwitch : MonoBehaviour
|
|
{
|
|
public static SceneSwitch Current { get; private set; }
|
|
|
|
public Image Img { get; private set; }
|
|
public string Target { get; private set; }
|
|
/// <summary>새 씬을 불러왔는가(페이드 인 시작 전 true).</summary>
|
|
public bool Loaded { get; private set; }
|
|
|
|
float fOut;
|
|
float fHold;
|
|
float fIn;
|
|
|
|
/// <summary>지금 화면에서 페이드 아웃 → scene → 페이드 인.</summary>
|
|
public static SceneSwitch Go(string scene, float fadeOut, float hold, float fadeIn, Color color, int order)
|
|
{
|
|
return Make(scene, fadeOut, hold, fadeIn, color, order, 0f);
|
|
}
|
|
|
|
/// <summary>이미 같은 색으로 덮인 화면(앞 페이드가 끝난 상태)에서 → scene → 페이드 인.</summary>
|
|
public static SceneSwitch FromBlack(string scene, float hold, float fadeIn, Color color, int order)
|
|
{
|
|
return Make(scene, 0f, hold, fadeIn, color, order, 1f);
|
|
}
|
|
|
|
static SceneSwitch Make(string scene, float fadeOut, float hold, float fadeIn, Color color, int order, float startAlpha)
|
|
{
|
|
if (Current != null) Destroy(Current.gameObject);
|
|
var go = new GameObject("SceneSwitch");
|
|
DontDestroyOnLoad(go);
|
|
var s = go.AddComponent<SceneSwitch>();
|
|
Current = s;
|
|
var canvas = go.AddComponent<Canvas>();
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
canvas.sortingOrder = order;
|
|
go.AddComponent<GraphicRaycaster>(); // 바뀌는 동안 입력 막음
|
|
s.Img = UiKit.Fill("Fade", go.transform).gameObject.AddComponent<Image>();
|
|
s.Img.color = new Color(color.r, color.g, color.b, startAlpha);
|
|
s.Img.raycastTarget = true;
|
|
s.Target = scene;
|
|
s.fOut = fadeOut;
|
|
s.fHold = hold;
|
|
s.fIn = fadeIn;
|
|
s.StartCoroutine(s.Run());
|
|
return s;
|
|
}
|
|
|
|
public float Alpha
|
|
{
|
|
get { return Img != null ? Img.color.a : 0f; }
|
|
}
|
|
|
|
void SetAlpha(float a)
|
|
{
|
|
var c = Img.color;
|
|
c.a = Mathf.Clamp01(a);
|
|
Img.color = c;
|
|
}
|
|
|
|
IEnumerator FadeTo(float to, float sec)
|
|
{
|
|
float from = Img.color.a, t = 0f, last = LoadingScreen.Now;
|
|
while (t < sec)
|
|
{
|
|
yield return null;
|
|
float now = LoadingScreen.Now;
|
|
t += Mathf.Max(0f, now - last);
|
|
last = now;
|
|
SetAlpha(Mathf.Lerp(from, to, sec > 0f ? Mathf.Clamp01(t / sec) : 1f));
|
|
}
|
|
SetAlpha(to);
|
|
}
|
|
|
|
IEnumerator Run()
|
|
{
|
|
if (Img.color.a < 1f) yield return FadeTo(1f, fOut);
|
|
var op = SceneManager.LoadSceneAsync(Target, LoadSceneMode.Single);
|
|
while (op != null && !op.isDone) yield return null;
|
|
Loaded = true;
|
|
yield return null; // 새 씬 Awake·Start 한 프레임
|
|
for (float w = 0f, last = LoadingScreen.Now; w < fHold;)
|
|
{
|
|
yield return null;
|
|
float now = LoadingScreen.Now;
|
|
w += Mathf.Max(0f, now - last);
|
|
last = now;
|
|
}
|
|
yield return FadeTo(0f, fIn);
|
|
if (Current == this) Current = null;
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
if (Current == this) Current = null;
|
|
}
|
|
}
|
|
}
|