본문 바로가기
유니티/코드

어드레서블 에셋 씬 로드 구현

by fore4022 2024. 9. 11.

 게임을 만들면서 addressable asset으로 scene을 관리하는 기능이 없다는 것을 알고, 추가하였다.

어드레서블 에셋 로드 구현

 

어드레서블 에셋 로드 구현

항상 어드레서블 에셋 사용이 필요할 때마다 일일이 구현하는 번거로움을 줄이고자 LoadToPath, LoadToLable을 만들게 되었다.본인이 이해한 내용을 바탕으로 만들었기에 성능 최적화나, 모든 경우에

fore4022.tistory.com

using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
public class Util
{
    public static async Task<AsyncOperationHandle<SceneInstance>> LoadingScene(string path)
    {
        AsyncOperationHandle<SceneInstance> handle = Addressables.LoadSceneAsync(path);

        await handle.Task;

        return handle;
    }
}// 비동기로 scene을 불러오고, 해제를 위해서 handle을 반환한다.
using System;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
public class Scene_Manager
{
    public AsyncOperationHandle<SceneInstance>? sceneHandle;

    public Action loadScene = null;

    public async void LoadScene(string path)
    {
        if(sceneHandle != null)
        {
            loadScene.Invoke();

            Addressables.Release(sceneHandle);
        }

        sceneHandle = await Util.LoadingScene(path);
    }
}// handle을 받아와서 저장한 이후, 새로운 Scene으로의 변경이 생길 경우에 호출되는 action의 호출과 동시에, 이전 Scene 정보를 언로드 해준다.
using UnityEngine;
public class Test : MonoBehaviour
{
    private string scenePath = "test";

    private void Start()
    {
        Managers.Scene.LoadScene(scenePath);
    }
}// test라는 주소를 가진 Scene이 불러와진다.

아직 많이 부족한 코드이니, 틀린 점이나 개선 여지가 있는 것을 지적해주세요.

 오늘도 방문해 주셔서 감사합니다.

 

 최근 수정 날짜 : 2024.9.11

'유니티 > 코드' 카테고리의 다른 글

Tweening 구현(1)  (0) 2025.04.29
ScriptableObject 배열 길이 제한하기  (0) 2025.03.18
Non-MonoBehaviour Class에서 Coroutine 실행  (0) 2024.12.31
GetComponentsInChildren 구현  (0) 2024.10.12
Input Action 관리  (0) 2024.08.27