Project_WL/Assets/Script/UI/ETC/LoadingUI.cs

107 lines
3.5 KiB
C#

using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class LoadingUI : MyCoroutine
{
public static bool isIns;
public static LoadingUI Ins;
public GameObject go;
public TextMeshProUGUI[] texts; // 0 팁, 1 로딩 진행 상황
public Slider slider_loading;
public RawImage loadingimg;
AsyncOperationHandle<Texture> currentHandle;
List<bool> list_loadingComplete = new List<bool> { false, false };
public int MaxImage = 1;
int m_index = 1;
// #800 세로 화면 대응 --------------------------------------------------------------
// 배경은 16:9 원본을 세로 화면 높이에 맞춰 띄우므로 가로가 크게 잘린다.
// 잘릴 때 "화면 중앙에 둘 원본의 가로 위치"는 그림마다 다르다(주인공이 좌/우 어디에 있느냐).
// 그래서 이미지 번호별 초점을 인스펙터 데이터로 들고 다닌다. 값의 SOT 는 프리팹이다.
[Header("#800 세로 대응")]
[Tooltip("배경 RawImage 의 WLBackgroundFit. 비어 있으면 loadingimg 에서 자동으로 찾는다.")]
public WLBackgroundFit bgFit;
[Tooltip("Loading1~MaxImage 각 장의 초점 x (0 = 왼쪽 끝, 0.5 = 정중앙, 1 = 오른쪽 끝). 비면 전부 0.5.")]
public float[] focusX_perImage;
void Awake()
{
Ins = this; isIns = true;
if (bgFit == null && loadingimg != null) bgFit = loadingimg.GetComponent<WLBackgroundFit>();
}
private void Start() { Load_Img(); }
void Load_Img()
{
m_index = Random.Range(1, MaxImage + 1);
AddrResourceMgr.Ins.LoadObject<Texture>($"Assets/Res_Addr/Loading/Loading{m_index}.jpg", handle =>
{
if (currentHandle.IsValid())
{
Addressables.Release(currentHandle);
loadingimg.texture = null; // 기존 Texture 참조 해제
}
currentHandle = handle;
loadingimg.texture = handle.Result;
Apply_Focus(m_index); // 그림이 바뀌면 크롭 기준점도 같이 바뀐다
});
}
/// 이미지 번호(1-base)에 맞는 초점을 배경에 적용한다. 표가 없거나 범위를 벗어나면 정중앙.
void Apply_Focus(int index)
{
if (bgFit == null) return;
int i = index - 1;
float focus = (focusX_perImage != null && i >= 0 && i < focusX_perImage.Length)
? focusX_perImage[i] : 0.5f;
bgFit.focusX = focus;
bgFit.Apply();
}
public void Start_Loading()
{
go.SetActive(true);
texts[0].text = "팁 입니다.";
slider_loading.value = 0f;
texts[1].text = "0%";
for (int i = 0; i < list_loadingComplete.Count; i++)
list_loadingComplete[i] = false;
}
public void Set_LoadingSlider(float proc)
{
slider_loading.value = proc;
texts[1].text = $"{proc * 100f:N0}%";
}
public void End_Loading(int index)
{
if (go.activeInHierarchy)
{
list_loadingComplete[index] = true;
bool endloading = true;
for (int i = 0; i < list_loadingComplete.Count; i++)
{
endloading = list_loadingComplete[i];
if (!endloading) break;
}
if (endloading)
{
Load_Img();
go.SetActive(false);
}
}
}
}