배움 저장소

[UE4] Asset Manger and Soft Reference 본문

Dev/Unreal Engine4

[UE4] Asset Manger and Soft Reference

시옷지읏 2021. 11. 7. 10:04
더보기

이 글은 동영상을 보고 공부하여 정리한 글입니다. 틀린 내용이 있다면 알려주세요.

1. 왜 Asset Manger가 필요한가?


1) GameLevel이 자주 바뀌면 Asset Manager가 필요하지 않다. Asset을 당장 쓰지 않더라도 Reference를 가지고 있으면 Memory를 잡아먹는다. 이러한 Asset이 많고 Level이 바뀌지 않는 RPG 게임은 Asset이 Memory를 계속해서 잡아먹을 수 밖에 없다. 이때 Asset Manager를 유용하게 사용할 수 있다.

 

2) Blue Print Class는 C++ Class와 상호작용 하기 위하여 복잡한 작업을 해야한다. Asset Manager는 이 일을 쉽게 해결해줄 수 있다.

 

2. Asset Manger 구성요소


Asset Manager는 Map 형태로 Asset의 경로를 Registry에 저장한다. 이떄 Identifier 식별자는 고유한 이름을 가지고 있어야 한다. 그러니 Content 폴더에 들어있는 Asset은 고유한 이름을 가지도록 하자.

 

 

 

1. LoadPrimaryAsset()으로 등록되어있으면 Level이 바뀌어도 파괴되지 않기 때문에 UnloadPrimaryAsset()을 사용해주어야 한다.

 

2.  PreloadPrimaryAssets()은 Handle과 함께쓰고 Handle이 파괴될 때 함께 파괴됨.

 

3. Soft Reference 활용하기

 

[UE4] Programming KickStart

Programming Kickstart - Unreal Engine In this course, you’ll learn about programming in Unreal Engine along with tips and tricks for creating and developing C++ projects. www.unrealengine.com 강의..

hoplite.tistory.com

 위 글에서 C++ 코드가 Asset을 어떻게 가져와야하는지 아주 간략하게 다루었다. 이번 글에서는 Asset을 바로 불러오지 않고 Asset의 Path만 저장해두었다가 필요할 때 부르는 방법을 확인해본다.

더보기

Unreal Engine 4 provides a number of mechanisms to control how an asset is referenced and by extension loaded into memory. You can think of references in two ways: a hard reference where object A refers to object B and causes object B to be loaded when object A is loaded; and a soft reference where object A refers to object B via an indirect mechanism such as the string form of the path to the object

 언리얼은 코드에서 두 가지 방법으로 객체를 참조할 수 있다. Hard Reference와 SoftReference이다.

 Hard Reference에서 객체 A가 객체 B를 참조하고 있다면, 객체 A를 불러올 때 객체 B도 함께 불러온다. 캐릭터를 불러올 때 캐릭터가 사용할 수 있는 모든 장비를 함께 불러온다고 생각하는 경우가 아닐까. 사용하지 않는 객체를 잔뜩 불러와 메모리를 잡아먹고 있는 상황은 최적화에 도움될게 없다.

 Soft Reference는 객체 A가 객체 B를 참조할 때, 객체 B를 불러오지 않는다. 그 대신에 객체 B의 Path를 String 형태로 가지고 있다가, 불러오기 코드가 실행될 때 객체 B를 가져온다.

 이때 사용하는 것이 TSoftObjectPtr 타입이다. String 형태로 데이터를 가지고 있으며, 템플릿 코드가 객체 B를 메모리에 가져왔는지 아닌지를 확인할 수 있다. IsPending()을 사용하면 된다. 주의할 점은 필요할 때 직접 불러오기 코드를 실행시켜줘야 한다.  불러올 수 있는 방법은 3가지가 있다.

1. LoadObject<Type>()

2. StaticLoadObject()

3. FStreamingManager 활용

 첫 번째와 두 번째 방법은 Synchronous라서 Memory 여유가 없다면 렉이 걸릴 수 있다. 세 번째 방법을 쓴다면 Asynchronous라서 병렬적으로 처리할 수 있다.

 필요할 때 객체를 불러오는 방법은 다음 코드와 같다.

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category=Building)
TSoftObjectPtr<UStaticMesh> BaseMesh;

UStaticMesh* GetLazyLoadedMesh()
{
    if (BaseMesh.IsPending())
    {
        const FSoftObjectPath& AssetRef = BaseMesh.ToStringReference();
        BaseMesh = Cast< UStaticMesh>(Streamable.SynchronousLoad(AssetRef));
    }
    return BaseMesh.Get();
}

 

참고

https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/Assets/ReferencingAssets/

'Dev > Unreal Engine4' 카테고리의 다른 글

[UE4] Introduction to C++ Programming in UE4  (0) 2021.11.08
[UE4] Unreal Property System (Reflection)  (0) 2021.11.02
[UE4] Profiling and Optimization  (0) 2021.10.31
[UE4] UI 최적화  (0) 2021.10.30
[UE4] Programming KickStart  (0) 2021.10.28
Comments