Files
SAMKWANG/Assets/Scripts/BranchSelection/UI/Panel_BranchLoading.cs
2025-10-21 18:47:48 +09:00

77 lines
2.6 KiB
C#

using UnityEngine;
using WI;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
using TMPro;
namespace Samkwang
{
public class Panel_BranchLoading : MonoBehaviour
{
public TextMeshProUGUI Text_Branch;
public Image Image_BranchThumbnail;
public Slider Slider_LoadingProgress;
public TextMeshProUGUI Text_Loading;
public float progressSpeed;
public string nextSceneName;
public void Init()
{
var textDict = transform.GetChildComponentsByName<TextMeshProUGUI>();
Text_Branch = textDict.GetOrNull(nameof(Text_Branch));
var mask = transform.GetComponentInChildren<Mask>(true);
Image_BranchThumbnail = mask.transform.Find("Image_BranchThumbnail").GetComponent<Image>();
Slider_LoadingProgress = transform.GetComponentInChildren<Slider>(true);
Text_Loading = Slider_LoadingProgress.transform.parent.GetComponentInChildren<TextMeshProUGUI>();
}
public void LoadBranch(UI_BranchLocation branchLocation)
{
transform.SetAsLastSibling();
gameObject.SetActive(true);
Text_Branch.SetText(branchLocation.BranchName.text);
Image_BranchThumbnail.sprite = branchLocation.branchThumbnail;
nextSceneName = branchLocation.sceneName;
StopAllCoroutines();
StartCoroutine(WaitAndLoad());
}
private IEnumerator WaitAndLoad()
{
yield return null;
yield return StartCoroutine(LoadNextSceneAsync());
}
IEnumerator LoadNextSceneAsync()
{
AsyncOperation asyncOp = SceneManager.LoadSceneAsync(nextSceneName);
asyncOp.allowSceneActivation = false;
float displayedProgress = 0f;
while (!asyncOp.isDone)
{
float targetProgress = Mathf.Clamp01(asyncOp.progress / 0.9f);
displayedProgress = Mathf.MoveTowards(displayedProgress, targetProgress, Time.deltaTime * progressSpeed);
Slider_LoadingProgress.value = displayedProgress;
Text_Loading.SetText($"Loading..({(displayedProgress * 100f).ToString("F0")}%)");
if (asyncOp.progress >= 0.9f && displayedProgress >= 0.99f)
{
Slider_LoadingProgress.value = 1f;
Text_Loading.SetText($"Loading..(100%)");
yield return new WaitForSeconds(0.5f);
asyncOp.allowSceneActivation = true;
}
yield return null;
}
}
}
}