Project_WL/Assets/Script/Main/MainToTitle.cs

208 lines
7.2 KiB
C#
Raw Normal View History

2024-12-15 01:39:39 +00:00
using System.Collections;
using System.Collections.Generic;
using TMPro;
2024-12-22 01:07:25 +00:00
using UnityEditor;
2024-12-15 01:39:39 +00:00
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
public class MainToTitle : MonoBehaviour
{
public GameObject go_splash, go_popup;
public TextMeshProUGUI label_popup, label_slider;
public Slider slider_download;
public List<AssetLabelReference> addr_Labels; // Addressables 레이블 참조
bool startDownload = false, downloadFail;
private int retryLimit = 3; // 재시도 횟수 제한
2024-12-22 01:07:25 +00:00
#if UNITY_EDITOR
[InitializeOnLoadMethod] private static void ClearLogFileOnGameStart() { DSUtil.ClearLogFile(); }
#endif
2024-12-15 01:39:39 +00:00
private IEnumerator Start()
{
CcdManager.EnvironmentName = "dev"; // "production"; // 개발, 라이브
slider_download.gameObject.SetActive(false);
slider_download.value = 0f;
yield return new WaitForSeconds(1f);
go_splash.SetActive(false);
StartCoroutine(Co_CheckDownload());
}
IEnumerator Co_CheckDownload()
{
label_slider.text = "";
long totalDownloadSize = 0;
long currentDownloadedSize = 0; // 현재까지 다운로드된 용량
2024-12-15 01:39:39 +00:00
int totalLabels = addr_Labels.Count;
var labels = new List<string>();
foreach (var labelRef in addr_Labels)
{
labels.Add(labelRef.labelString);
}
// 다운로드 크기 계산 (실패 시 재시도)
bool calcSuccess = false;
for (int retry = 0; retry < retryLimit; retry++)
{
totalDownloadSize = 0;
2024-12-15 01:39:39 +00:00
calcSuccess = true;
foreach (var label in labels)
{
var handle = Addressables.GetDownloadSizeAsync(label);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
totalDownloadSize += handle.Result;
2024-12-15 01:39:39 +00:00
}
else
{
calcSuccess = false;
Debug.LogError($"Failed to get download size for label: {label}, Retry: {retry + 1}");
break;
}
Addressables.Release(handle);
}
if (calcSuccess)
break;
else
yield return new WaitForSeconds(1f); // 재시도 대기
}
if (!calcSuccess)
{
label_popup.text = "리소스 계산 중 에러가 발생했습니다.\n다시 시도하시겠습니까?";
go_popup.SetActive(true);
downloadFail = true;
yield break; // 다운로드 크기 계산 실패로 종료
}
// 다운로드 크기 출력
if (totalDownloadSize > 0)
2024-12-15 01:39:39 +00:00
{
label_popup.text = $"다운로드 안내\n\n최신 패치 파일을 받아야 합니다.\n총 {FormatSize(totalDownloadSize)}를 다운로드 합니다.\n\nWifi로 다운로드 하는 것을 권장합니다.";
2024-12-15 01:39:39 +00:00
go_popup.SetActive(true);
while (!startDownload) yield return null; // 팝업에서 OK 누를 때까지 대기
slider_download.gameObject.SetActive(true);
// 다운로드 시작
foreach (var label in labels)
{
var handle = Addressables.GetDownloadSizeAsync(label);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
if (handle.Result > 0)
{
var downHandle = Addressables.DownloadDependenciesAsync(label);
while (!downHandle.IsDone)
{
float progress = (float)currentDownloadedSize / totalDownloadSize;
slider_download.value = progress;
label_slider.text = $"{progress * 100f:N2}%";
2024-12-15 01:39:39 +00:00
yield return null;
}
if (downHandle.Status == AsyncOperationStatus.Succeeded)
{
// 다운로드 완료 후 currentDownloadedSize에 추가
currentDownloadedSize += handle.Result;
2024-12-15 01:39:39 +00:00
}
else
{
Debug.LogError($"다운로드 실패: {label}");
downloadFail = true;
break;
}
Addressables.Release(downHandle);
}
}
Addressables.Release(handle);
}
// 다운로드 실패 처리
if (downloadFail)
{
label_popup.text = "리소스 다운로드 중 에러가 발생했습니다.\n다시 시도하시겠습니까?";
go_popup.SetActive(true);
}
else
StartCoroutine(Co_Loading(true));
}
else
StartCoroutine(Co_Loading(false));
}
IEnumerator Co_Loading(bool isUpdate)
{ // 필요한 애셋들 로딩하기
yield return null;
//// 폰트 로드하기
//AddrResourceMgr.Ins.LoadObject<TMP_FontAsset>("Assets/ThirdParty/TextMesh Pro/Addressables/Fonts & Materials/Font SDF.asset", handle =>
//{
// if (handle.Status == AsyncOperationStatus.Succeeded)
// {
// var fontAsset = handle.Result;
// //textMeshProComponent.font = fontAsset;
// }
//});
List<string> list_path = new List<string>
{
"Tables", "SoundInfo",
"UIAtlasMgr", "SortOrder_5", "BoxOpenUI", "GetItemUI", "ItemDescUI", "ActorInfo",
"TitleInfo", // 제일 뒤에 로드해서 시작한다.
};
for (int i = 0; i < list_path.Count; i++)
{
list_path[i] = $"Assets/ResWork/UIPrefabs/Title/{list_path[i]}.prefab";
AddrResourceMgr.Ins.LoadObject<GameObject>(list_path[i], handle =>
{
if (handle.IsDone && handle.Status == AsyncOperationStatus.Succeeded)
{
var go = DSUtil.Get_Clone(handle.Result);
if (go.name.Contains("TitleInfo"))
AddrResourceMgr.Ins.Set_AddressableReleaseSelf(go);
}
else
Debug.LogError($"로드 실패 : {list_path[i]}");
});
}
yield return null;
// 로딩 끝나면 이 오브젝트는 필요없음
gameObject.SetActive(false);
}
// 크기를 KB, MB, GB로 변환하여 출력
private string FormatSize(long bytes)
{
if (bytes >= 1L << 30) return $"{bytes / (1L << 30)} GB";
if (bytes >= 1L << 20) return $"{bytes / (1L << 20)} MB";
if (bytes >= 1L << 10) return $"{bytes / (1L << 10)} KB";
return $"{bytes} Bytes";
}
public void OnClick_OK()
{
if (downloadFail)
{
downloadFail = false;
StartCoroutine(Co_CheckDownload());
}
go_popup.SetActive(false);
startDownload = true;
}
}