본문 바로가기

Unity2020.1

[Unity::Addressable] Remote AssetBundle Update받는 방법

결과물

 https://hesterdiary.tistory.com/30

 

[Unity] Addressable을 사용하여 AssetBundle 빌드하기

https://blog.unity.com/kr/games/addressable-asset-system 어드레서블 에셋 시스템 | Unity Blog 어드레서블의 주된 기능은 로드할 대상이 되는 에셋과 에셋이 로드되는 위치 및 방식을 분리하는 것입니다. 씬,..

hesterdiary.tistory.com

위 포스트에 이어서 작업합니다.

작업순서

1. AddressableAssetSettings

2. AssetBundle Build For Remote

3. Code

 

1. AddressableAssetSettings

우선 자동으로 카탈로그를 업데이트하지 않도록

Assets/AddressableAssetData/AddressAssetSettings을 클릭한 후 inspector에서 Disable Catalog Update와 Build Remote Catalog를 체크해준다.

2. AssetBundle Build For Remote

repository에 저장할 에셋을 세팅한 후에 빌드를 한다.

참고로 Key는 추출을 위해 형식이 정해져 있는 것을 추천한다. 아래와같은 경우 "Key"로 시작하는 Label로 정하였다.

경로를 따로 커스텀하지 않았다면 RemoteBuildPath는

현재프로젝트\ServerData\현재플렛폼\

catalog_[PlayerVersionOverride].hash

catalog_[PlayerVersionOverride].json

과 번들 파일이 생성된 걸 확인할 수 있다.

만약 BuiltIn할 파일도 포함되어 있다면 빌드하기 전에 해당 Schema의 Include In Build를 해제했는지 확인해준다.

 

※ 현재까지 알아본 바로는 에셋번들을 빌드하면 카탈로그가 캐싱되어서 업데이트 체크를 못한다.

그렇기 때문에 hash값을 다르게 빌드를 하는 방법으로 우선 대체하였다.

 

해시를 다르게 하기 위해 key 지정만 해제했다.

 

3. Code

using UnityEngine.AddressableAssets;// Addressables
using UnityEngine.AddressableAssets.ResourceLocators;// IResourceLocator
using UnityEngine.ResourceManagement.AsyncOperations;// AsyncOperationHandle

 

먼저 InitializeAsync()를 호출하여 초기화를 해준다.

 

Addressables.InitializeAsync().Completed += op =>
{
	Addressable.Release(op);
};

 

업데이트 체크가 되었다면 checkOp.Result에는 "AddressablesMainContentCatalog"가 들어있다.

업데이트할 사항이 없다면 checkOp.Result.Count는 0이다.

"AddressablesMainContentCatalog"라는 이름은 빌드된 json파일의 상단에서 볼 수 있다.

 

Addressables.CheckForCatalogUpdates().Completed += checkOp =>
{
	if(checkOp.Result.Count > 0)
    {
    	Addressables.UpdateCatalogs().Complete += updateOp =>
        {
        	//updateOp에서 key추출
        }
    }
};

 

updateOp.Result에도 IResourceLocator가 하나 들어 있는 걸 확인할 수 있다.

resourceLocator.Keys의 내부를 살펴보면 Remote에 올린 Json파일의 m_Keys의 항목인 것을 확인할 수 있다.

 

"remoteassets_assets_all_50dffc876069b30ff3ac867a523a7187.bundle",

"Assets/Prefabs/PigModel.prefab",

"0f9b517307864db4cabe95601e1d3f16",

"KeyCharacter"

 

List<string> catalogsToUpdate = new List<string>();
var locators = updateOp.Result;
locators.Foreach(e=>
{
	foreach(var locatorKey in e.Keys)
	{
		string key = locatorKey.ToString();
		if(key.IndexOf("Key") == 0)// 지정한 key형식 조건에 맞는 string을 추가한다.
    	{
    		catalogsToUpdate.Add(key);
    	}
	}
});

 

위에서 얻은 catalogsToUpdate의 key를 이용하여

다운로드 크기 체크는 GetDownloadSizeAsync

 

Addressables.GetDownloadSizeAsync(key_);

 

다운로드는 DownloadDependenciesAsync로 할 수 있다.

 

Addressables.DownloadDependenciesAsync(key_);

 

참고 :

https://www.youtube.com/watch?v=EP3pvPAcHSo 

https://blog.naver.com/PostView.naver?blogId=cdw0424&logNo=221764918184&parentCategoryNo=&categoryNo=21&viewDate=&isShowPopularPosts=false&from=postList 

 

유니티 엔진 유니티(Unity) - Addressable(어드레서블) 사용법(9). 서버에서 다운로드하기 3편

실수로 전에 사용하던 예제를 지워버려서 아예 새로 만들어봤다. 위 예제는 스폰 버튼을 누르면 캐릭터를 ...

blog.naver.com