본문 바로가기
프로그래밍/Unity

Unity 어드레서블 에셋

by neive 2021. 8. 9.
728x90

http://docs.unity3d.com/Packages/com.unity.addressables@1.18/manual/index.html

 

Unity Addressable Asset system | Addressables | 1.18.15

Unity Addressable Asset system The Addressable Asset system provides an easy way to load assets by “address”. It handles asset management overhead by simplifying content pack creation and deployment. The Addressable Asset system uses asynchronous loadi

docs.unity3d.com

 

튜토리얼

 

1. 새로운 프로젝트를 생성

 

2. Sprite Packer 확인 (Edit/Project Settings/Editor 에서 Sprite Packer/Mode Always Enabled)

 

3. 에디터의 Window/Package Manager 로 들어가서 Addressables 를 설치

 

4. Window/Asset Management/Addressables/Group 실행 후 Create Addressables Settings 클릭

 

5.  아틀라스 생성

저는 이미지 폴더안에 아이콘들만 모아둘 폴더를 생성했습니다
Icon 폴더랑 같은 위치에 Create 로 Sprite Atlas 를 생성
표시한 부분을 잘 보고 값을 입력하시고.. 아래 폴더째로 넣은 것에 주의.. 폴더를 드래그 해서 올리면 됩니다

 

6. 확인하기 위해 UGUI 생성

그냥 UI 에서 이미지를 하나 생성 합니다

7. Image 에 아래 소스로 컴포넌트를 부착

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AddressableAssets;

public class Test_AddresAtlas : MonoBehaviour
{
    [SerializeField] protected Image m_Image;

    // Start is called before the first frame update
    IEnumerator Start()
    {
        yield return Addressables.InitializeAsync();

        string atlasName = "Atlas_Icon";
        string imageName = "emoticon_001";
        var handle = Addressables.LoadAssetAsync<Sprite>($"{atlasName}[{imageName}]"); // Atlas_Icon[emoticon_001]

        yield return handle;

        m_Image.sprite = handle.Result;
        
        Addressables.Release(handle);
    }
}

 

 

8. 이런.. 애러가 발생하는군요.. 스프라이트를 찾지 못하고 있어요 -_- 아마

 도 이 게시물에 온 이유중 가장 많은 이유가 이거겠죠..

Exception encountered in operation Resource<Sprite>(Atlas_Icon.spriteatlas[emoticon_001]), status=Failed, result= : Sprite failed to load for location Atlas_Icon[emoticon_001].
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase`1/<>c__DisplayClass55_0<System.Collections.Generic.IList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle>>:<add_CompletedTypeless>b__0 (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<System.Collections.Generic.IList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle>>)
DelegateList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<System.Collections.Generic.IList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle>>>:Invoke (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<System.Collections.Generic.IList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle>>) (at Library/PackageCache/com.unity.addressables@1.16.19/Runtime/ResourceManager/Util/DelegateList.cs:69)
UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationBase`1/<>c__DisplayClass55_0<object>:<add_CompletedTypeless>b__0 (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<object>)
DelegateList`1<UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<object>>:Invoke (UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationHandle`1<object>) (at Library/PackageCache/com.unity.addressables@1.16.19/Runtime/ResourceManager/Util/DelegateList.cs:69)
UnityEngine.ResourceManagement.Util.DelayedActionManager:LateUpdate () (at Library/PackageCache/com.unity.addressables@1.16.19/Runtime/ResourceManager/Util/DelayedActionManager.cs:159)

 

 

9. 애러 해결

최초 올렸던 이미지의 속성으로 가서 Texture 타입을 봅시다 -_-;;; 스프라이트로 안되어있으니 스프라이트 아틀라스로 들어가지 않았고 들어가지 않았으니 못찾는게 당연한;;

10. 결과

잘 나오는군요

 

 

11. 기타

using UnityEngine.AddressableAssets;

Addressables.LoadAssetAsync<GameObject>("AssetAddress");

Addressables.InstantiateAsync("AssetAddress");


void UseCompleted()
{
    AsyncOperationHandle<Texture2D> textureHandle = Addressables.LoadAsset<Texture2D>("mytexture");
    textureHandle.Completed += TextureHandle_Completed;
}

private void TextureHandle_Completed(AsyncOperationHandle<Texture2D> handle)
{
    if (handle.Status == AsyncOperationStatus.Succeeded) {
        Texture2D result = handle.Result;
        // The texture is ready for use.
    }
}

IEnumerator UseHandle()
{
    AsyncOperationHandle<Texture2D> handle = Addressables.LoadAssetAsync<Texture2D>("mytexture");

    //if the handle is done, the yield return will still wait a frame, but we can skip that with an IsDone check
    if(!handle.IsDone)
        yield return handle;

    if (handle.Status == AsyncOperationStatus.Succeeded)
    {
        Texture2D texture = handle.Result;
        // The texture is ready for use.
        // ...
        // Release the asset after its use:
        Addressables.Release(handle);
    }
}

void UseSimple()
{
        Addressables.InstantiateAsync(key, transform.position, transform.rotation).Completed 
            += delegate(AsyncOperationHandle<GameObject> handle) { Destroy(handle.Result, 2.0f); };
}

async UseAwait()
{
    AsyncOperationHandle<Texture2D> handle = Addressables.LoadAssetAsync<Texture2D>("mytexture");

    await handle.Task;

    // The task is complete. Be sure to check the Status is successful before storing the Result.

    // dont destory
    Addressables.ResourceManager.Acquire(handle);
}


void TakeLow()
{

	//자산의 모든 하위 오브젝트를 로드하려면 다음 예제 구문을 사용할 수 있습니다.
	Addressables.LoadAssetAsync<IList<Sprite>>("MySpriteSheetAddress");

	//자산의 단일 하위 오브젝트를 로드하려면 다음을 수행할 수 있습니다. 
	Addressables.LoadAssetAsync<Sprite>("MySpriteSheetAddress[MySpriteName]");
    
    // 위의 하위 오브젝트는 모르겠고 폴더를 라벨로 잡고 그 폴더의 내용을 전부 땡겨오는건 이 방법
        var handle = Addressables.LoadAssetsAsync<GameObject>("Label", obj =>
        {
            //Gets called for every loaded asset
            Debug.Log(obj.name);
            
        });
        yield return handle;
        IList<GameObject> singleKeyResult = handle.Result;
}


void AssetRef()
{
	AssetRefMember.LoadAssetAsync<GameObject>();
	AssetRefMember.InstantiateAsync(pos, rot);
}
728x90

'프로그래밍 > Unity' 카테고리의 다른 글

txt 파일 생성  (0) 2021.09.09
Unity EditorTool 을 활용한 Component 값 제어  (5) 2021.08.24
CinemachineVirtualCamera Aim 속성  (0) 2021.07.30
Simple Controller  (0) 2021.05.29
UGUI Particle 파티클 / Effect 이펙트  (0) 2021.05.12

댓글