ThreeKingdomsSlingshot/Assets/Scripts/OutGame/Boot/SplashBoot.cs

112 lines
4.1 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace TKS.OutGame
{
/// <summary>
/// Splash 씬 — 회사 CI 노출(페이드 인 → 유지 → 페이드 아웃) 동안 CDN 확인을 함께 돌리고,
/// 받을 것이 없으면 Title 씬으로 · 있으면 로딩 화면 Download(BootFlow) → 다 받으면 페이드 → Title 씬.
/// </summary>
public class SplashBoot : MonoBehaviour
{
public enum Step
{
Logo,
Check,
Download,
Switch
}
public static SplashBoot Current { get; private set; }
public Step State { get; private set; }
public IResourceDownloader Downloader { get; private set; }
public Image Ci { get; private set; }
/// <summary>CI 가 가장 밝았던 알파(테스트 · 노출 확인).</summary>
public float CiPeak { get; private set; }
Camera cam;
void Awake()
{
Current = this;
}
void OnDestroy()
{
if (Current == this) Current = null;
}
IEnumerator Start()
{
var r = BootScenes.SplashRow;
Color bg = r != null ? r.color : Color.black;
cam = BootScenes.Cam(bg);
var go = new GameObject("SplashUI");
go.transform.SetParent(transform, false);
// P41: CI 알파가 바뀌는 동안만 = Frame · 요소 2개(바탕 · CI)
UiLayer.Root(go, cam, r != null && r.size > 0 ? r.size : 100, 1f, UiLayer.Reference, UiRate.Frame, false);
var back = UiKit.Fill("Bg", go.transform).gameObject.AddComponent<Image>();
back.color = bg;
back.raycastTarget = false;
var ciRt = UiKit.Rect("CI", go.transform, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), Vector2.zero,
new Vector2(r != null && r.w > 0f ? r.w : 560f, r != null && r.h > 0f ? r.h : 560f));
Ci = ciRt.gameObject.AddComponent<Image>();
Ci.sprite = r != null ? LoadingTable.Sprite(r.sprite) : null;
Ci.preserveAspect = true;
Ci.raycastTarget = false;
Ci.color = new Color(1f, 1f, 1f, 0f);
// CDN 확인은 CI 가 보이는 동안 함께(기다림 줄임)
Downloader = BootFlow.OverrideDownloader ?? BootFlow.FromTable();
Downloader.Begin();
float last = LoadingScreen.Now;
System.Func<float> dt = () =>
{
float now = LoadingScreen.Now;
float d = Mathf.Max(0f, now - last);
last = now;
Downloader.Tick(d);
return d;
};
State = Step.Logo;
float delay = r != null ? r.phase : 0.2f, fIn = r != null ? r.a : 0.4f, hold = r != null ? r.b : 1.2f, fOut = r != null ? r.c : 0.4f;
for (float t = 0f; t < delay;)
{
yield return null;
t += dt();
}
for (float t = 0f; t < fIn + hold + fOut;)
{
yield return null;
t += dt();
float a = t < fIn ? t / Mathf.Max(0.01f, fIn) : t < fIn + hold ? 1f : 1f - (t - fIn - hold) / Mathf.Max(0.01f, fOut);
a = Mathf.Clamp01(a);
Ci.color = new Color(1f, 1f, 1f, a);
CiPeak = Mathf.Max(CiPeak, a);
}
Ci.color = new Color(1f, 1f, 1f, 0f);
State = Step.Check;
while (!Downloader.Checked)
{
yield return null;
dt();
}
if (!Downloader.Needed)
{
State = Step.Switch;
BootScenes.GoTo(BootScenes.Title);
yield break;
}
// 받기 = 로딩 화면 Download(BootFlow · 진행률·용량 · 실패 = retry) → 페이드 아웃 → Title 씬(검정에서 이어 페이드 인)
State = Step.Download;
BootFlow.BeginThen(cam, () =>
{
State = Step.Switch;
BootScenes.FromBlack(BootScenes.Title);
}, Downloader);
}
}
}