AWS S3 Unity Addressable Bukkit

Yumin·2025년 2월 7일
0

Unity

목록 보기
6/18
post-thumbnail

권한 -> 버킷 정책 편집

파일 업로드 후 객체 URL 복사 후
파일 명 대신 [BuildTarget] 추가
Addressable Group Remote 설정

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingManager : MonoBehaviour
{
    public static string nextScene;
    public Slider loadingBar;

    void Start()
    {
        StartCoroutine(StartLoadingScene());
    }

    IEnumerator StartLoadingScene()
    {
        yield return null;

        AsyncOperation op = SceneManager.LoadSceneAsync(nextScene);
        op.allowSceneActivation = false;

        while (!op.isDone)
        {
            if (op.progress < 0.9f)
            {
                loadingBar.value = op.progress;
            }
            else
            {
                loadingBar.value = 1f;
                yield return new WaitForSeconds(2f);
                op.allowSceneActivation = true;
            }

            yield return null;
        }
    }

    public static void LoadScene(string sceneName)
    {
        nextScene = sceneName;
        SceneManager.LoadScene("Loading");
    }
}
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;

public class DownManager : MonoBehaviour
{
    [Header("UI")]
    public GameObject waitMessage;
    public GameObject downMessage;
    public Slider downSlider;
    public TextMeshProUGUI sizeInfoText;
    public TextMeshProUGUI downValText;

    [Header("Label")]
    public AssetLabelReference defaultLabel;
    public AssetLabelReference matLabel;

    private long patchSize;
    private Dictionary<string, long> patchMap = new Dictionary<string, long>();

    void Start()
    {
        waitMessage.SetActive(true);
        downMessage.SetActive(false);

        StartCoroutine(InitAddressable());
        StartCoroutine(CheckUpdateFiles());
    }

    IEnumerator InitAddressable()
    {
        var init = Addressables.InitializeAsync();
        yield return init;
    }

    #region Chek Down

    IEnumerator CheckUpdateFiles()
    {
        var labels = new List<string>() { defaultLabel.labelString, matLabel.labelString };

        patchSize = default;

        foreach (var label in labels)
        {
            Debug.Log($"Checking download size for label: {label}");
            var handle = Addressables.GetDownloadSizeAsync(label);

            yield return handle;

            if (handle.Status == AsyncOperationStatus.Failed)
            {
                Debug.LogError($"Failed to get download size for label: {label}");
                continue;
            }

            patchSize += handle.Result;
        }

        if (patchSize > decimal.Zero)
        {
            //Down
            waitMessage.SetActive(false);
            downMessage.SetActive(true);

            sizeInfoText.text = GetFileSize(patchSize);
        }
        else
        {
            downValText.text = " 100 % ";
            downSlider.value = 1f;
            yield return new WaitForSeconds(2f);
            LoadingManager.LoadScene("Main");
        }
    }

    private string GetFileSize(long byteCnt)
    {
        string size = "0 Bytes";

        if (byteCnt >= 1073741824.0)
        {
            size = string.Format("{0:##.##}", byteCnt / 1073741824.0) + " GB";
        }
        else if (byteCnt >= 1048576.0)
        {
            size = string.Format("{0:##.##}", byteCnt / 1048576.0) + " MB";
        }
        else if (byteCnt >= 1024.0)
        {
            size = string.Format("{0:##.##}", byteCnt / 1024.0) + " KB";
        }
        else if (byteCnt > 0 && byteCnt < 1024.0)
        {
            size = byteCnt.ToString() + " Bytes";
        }

        return size;
    }

    #endregion

    #region DownLoad

    public void Button_DownLoad()
    {
        StartCoroutine(PatchFiles());
    }

    IEnumerator PatchFiles()
    {
        var labels = new List<string>() { defaultLabel.labelString, matLabel.labelString };

        foreach (var label in labels)
        {
            Debug.Log($"Starting download for label: {label}");
            var handle = Addressables.GetDownloadSizeAsync(label);

            yield return handle;

            if (handle.Result != decimal.Zero)
            {
                StartCoroutine(DownLoadLabel(label));
            }
        }

        yield return CheckDownLoad();
    }

    IEnumerator DownLoadLabel(string label)
    {
        patchMap.Add(label, 0);

        var handle = Addressables.DownloadDependenciesAsync(label, false);

        while (!handle.IsDone)
        {
            patchMap[label] = handle.GetDownloadStatus().DownloadedBytes;
            yield return new WaitForEndOfFrame();
        }

        patchMap[label] = handle.GetDownloadStatus().TotalBytes;
        Addressables.Release(handle);
    }

    IEnumerator CheckDownLoad()
    {
        var total = 0f;
        downValText.text = "0 %";

        while (true)
        {
            total += patchMap.Sum(tmp => tmp.Value);

            downSlider.value = total / patchSize;
            downValText.text = (int)(downSlider.value * 100) + " %";

            if (total == patchSize)
            {
                LoadingManager.LoadScene("Main");
                break;
            }

            total = 0f;
            yield return new WaitForEndOfFrame();
        }
    }

    #endregion
}
profile
クリエイティブを楽しむ開発者

0개의 댓글