0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【UE5】UMG ViewModelプラグインを使ってListViewに文字列のリストを表示する

Last updated at Posted at 2025-04-13

はじめに

UMG ViewModel(MVVM)プラグインで、単体テキストや体力バーの表示は紹介されていたのですが、リストビューへの反映の手順が見当たらなかったので調べました。

公式ドキュメント

導入

Edit > Plugins から UMG ViewModelを有効にする(要再起動)
image.png

やりたいこと

  • ViewModelの仕組みを使って ListView に文字列のリストを表示する

構成

名前 種類 親クラス 内容
VM_TextEntry ViewModel MVVMViewModelBase 各行の表示テキストを保持
VM_TextList ViewModel MVVMViewModelBase 複数の EntryViewModel を保持
WBP_TextEntry ウィジェット UserWidget + UserObjectListEntry テキスト1行の UI
WBP_MainView ウィジェット UserWidget ListView を配置するメイン UI

手順

(1)各行のViewModelの作成(行ごとのテキストを表示)

  1. Blueprint クラス作成:親 = MVVMViewModelBase
    image.png
     名前:VM_TextEntry
  2. 変数を追加
     名前:Text / 型:Text
     Field Notify(ベルのアイコン)にチェックを入れる
    image.png

(2)メインViewModelの作成(テキストのリスト)

  1. Blueprint クラス作成:親 = MVVMViewModelBase
     名前:VM_TextList
  2. 変数を追加
     名前:TextEntries / 型:VM_TextEntryの配列
     Field Notify(ベルのアイコン)にチェックを入れる
    image.png

(3)Entryウィジェットの作成と設定:WBP_TextEntry

A. 作成

  1. Blueprint Class → UserWidget → 名前:WBP_TextEntry

B. インターフェース追加(ここ重要!)

  1. クラス設定 → インターフェース → UserObjectListEntry を追加

C. UIデザイン

  1. TextBlock を1つ追加(表示用)
    image.png

D. MVVM Binding

  1. BindingsTextBlock.Text を ViewModel の Text にバインド
    image.png
    image.png

E. イベント実装(OnListItemObjectSet

追加したインターフェースでViewModelを設定する

Event OnListItemObjectSet(InObject)
→ Cast to VM_TextEntry
→ Set ViewModel ノードで ViewModel を設定(MVVM用のViewModel変数へ)

image.png

このステップが「ListView の表示行ごとに ViewModel をバインド」する要になります。

(4)メインウィジェットの作成:WBP_MainView

  1. Blueprint Class → UserWidget → 名前:WBP_MainView
  2. UI に ListView を配置
    image.png

A. MVVM Binding

  • ViewModel を指定:VM_TextList
    image.png

B. ListView 設定

  • Entry Widget Class: WBP_TextEntry
  • Entry ViewModel Class: VM_TextEntry
    image.png
  • Items → バインド → ViewModel の TextEntries
    image.png
    image.png
    image.png

C. Creation Typeの設定

Creation TypeCreate Instance に設定します。
image.png

表示するテキストデータを追加

今回は WBP_MainView の Event Construct で行いました。

  1. "Apple", "Orange", "Lemmon", "Banana", "Strawberry"のText Arrayを作成
  2. それぞれ Construct Object from Class に VM_TextEntry を指定してViewModel を作成して配列に保存
  3. VM_TextList の TextEntries に設定(SET w/ Broadcast)する
    image.png

リストビューを表示

今回は Level Blueprint に実装しました。

  1. Level Blueprintを開く
  2. Create Widget で WBP_MainView を作成
  3. Add to Viewport で表示
    image.png

実行

プレイするとリストビューにテキストが表示されました。
image.png

まとめ

使い勝手が良くなりそうなプラグインだと思って使い始めました。
リストビューを使った資料がなくて調べたのでまとめました。
参考になれば幸いです。

参考

https://qiita.com/HSKKOU/items/abc562a9decf7409322e
https://qiita.com/HSKKOU/items/e31e393cdac2309b4d1e
https://www.youtube.com/watch?v=1PNpRdVP_68&t=1737s&ab_channel=LifeEXE

0
0
0

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
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?