LoginSignup
0

More than 1 year has passed since last update.

C++でUserWidgetを非同期ロードして生成する

Last updated at Posted at 2022-05-01

概要

UAssetManager::Get()->GetStreamableManager()->RequestAsyncLoadを使用した非同期ロードで、UserWidgetを読み込んでインスタンスを作成する方法のメモ

SoftObjectPathの作成

Content Browserでアセットを選択し右クリックからCopy Referenceをしたパスを使って、SoftObjectPathを作成する。
_Cを付けることに注意
例 FSoftObjectPath("/Game/UMG/UMG_HUD.UMG_HUD_C")

RequestAsyncLoad呼び出し

handle = UAssetManager::Get().GetStreamableManager().RequestAsyncLoad(FSoftObjectPath("/Game/UMG/UMG_HUD.UMG_HUD_C"), [this]()
	{
		TSubclassOf<class UUserWidget> WidgetClass;
		WidgetClass = TSoftClassPtr<UUserWidget>(handle->GetLoadedAsset()).Get();
		auto w = CreateWidget(GetWorld(), WidgetClass);
		w->AddToViewport(0);		
		handle->ReleaseHandle();
		handle = nullptr;
	});

コールバック内でWidgetをインスタンス化

TSoftClassPtrにGetLoadedAsset()のUObjectを渡すことで、Get()から正しいUClassが取得できる。
ブループリントで行っているのと同じ流れで、CreateWidgetとAddToViewportを呼び出す。

不要になったハンドルを解放

handle->ReleaseHandle()で解放。
一応nullptrを入れる。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0