Post

KDT Unity 5주차 팀 프로젝트

2024-01-24 TIL

어드레서블을 사용하는 로딩씬 구현

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LodingManager : BaseScene
{

    private int _count;
    private int _totalCount;
    private string _key;
    private bool _resourcesLoaded = false;

    protected override bool Initialize()
    {
        if (!base.Initialize()) return false;
        Main.NextScene ??= "TitleScene";

        //  Main.Sound.PlayBGM("BGM");
        LoadResourcesAndScene();
        return true;
    }

    private void LoadResourcesAndScene()
    {
        LoadResources();
    }

    private void LoadResources()
    {
        string loadName;
        loadName = Main.NextScene == "TitleScene" ? Main.NextScene : "GameScene";
        Main.Resource.LoadAllAsync<Object>($"{loadName}", (key, count, totalCount) =>
        {
            _key = key;
            _count = count;
            _totalCount = totalCount;
            Debug.Log($"[{Main.NextScene}] Load asset {_key} ({_count}/{_totalCount})");
            if (_count != _totalCount) return;
            _resourcesLoaded = true; // 리소스 로딩 완료
            LoadNextSceneAsync(); // 리소스 로딩 완료 후 씬 로딩 시작
        });
    }

    private void LoadNextSceneAsync()
    {
        AsyncOperation sceneLoadOperation = SceneManager.LoadSceneAsync(Main.NextScene);
        sceneLoadOperation.allowSceneActivation = false;
        StartCoroutine(UpdateLoadingProgress(sceneLoadOperation));
    }

    private IEnumerator UpdateLoadingProgress(AsyncOperation sceneLoadOperation)
    {
        while (!_resourcesLoaded || sceneLoadOperation.progress < 0.9f) yield return null;
        while (!Input.anyKeyDown) yield return null;
        sceneLoadOperation.allowSceneActivation = true;
    }

    public override void Clear()
    {

    }
}



This post is licensed under CC BY 4.0 by the author.