VR中的UMG
2024年11月12日 2025年4月20日
引入
- 分四步走
[4/4]
- PC模式下,能使用键鼠操作
- PC模式下,能使用手柄操作
- VR模式下,能使用手柄射线操作
- VR模式下,能使用手柄摇杆操作
- PC模式下,能使用键鼠操作
使能OpenXR插件
虚幻5.1开始,只支持OpenXR。
用 OpenXR 帮助 XR 行业成长——高通和神木是怎么做的
未激活
-
入口
-
使能插件
使能后
验证说明
-
UMG
-
WBP_Button
按钮基类 -
WBP_Menu
菜单页面,一列按钮。 -
WBP_Main
主页面,布局为一排按钮,可以通过键鼠/Gamepad导航。
-
-
蓝图Actor派生类
BP_WidgetActor-
创建时初始化的bVR指示当前模式
-
负责UMG的创建与销毁:Main和Menu
-
提供两种显示UMG的方式:
- 添加到视口
- 设置给WidgetComponent,也就是VR中能够看到的UMG
- 添加到视口
-
-
Gameplay
类 名称 Pawn VRPawn PlayerController PC_UMG GameModeBase GM_UMG -
在GM_UMG中使用VRPawn和PC_UMG
-
新建关卡,设置GameMode为GM_UMG
-
在PC_UMG中创建BP_WidgetActor;含标志位bVR,表示当前运行模式,并设置给WidgetActor
-
VRPawn含VR需要的交互组件
-
UMG
按钮基类
-
按钮Normal/Hovered/Pressed样式修改
-
文本样式
-
文本内容初始化
-
按钮点击回调
菜单
为按钮实现功能
主页面
为按钮实现功能
蓝图Actor
bVR为true,在VR模式下运行;在PC模式下运行
Widget Component
创建主页面
创建和移除菜单
Gameplay
-
GameMode
-
关卡
-
PlayerController
-
Pawn
使用鼠标操作
使用键盘操作
-
确认键
键位 空格键 <SPC> 回车键 <Enter> -
导航键
键位 方向键 <Left> <Right> <Up> <Down> Tab键 <Tab>
问题:
- 使用方向键导航前,需先使用Tab键修改聚焦
- 聚焦按钮显示蓝框,不明显
设置按钮Keyboard Focus样式
-
WBP_Focus
Keyboard Focus
时,是按钮控件Keyboard Focus
-
设置按钮样式
Button > Style > Normal > Tint > Specified Color成员 结构体 Style ButtonStyle Normal SlateBrush Tint SlateColor Specified Color LinearColor
设置页面默认Keyboard Focus按钮
-
为按钮添加设置
Keyboard Focus
事件 -
主页面
-
菜单
关闭菜单时,主页面菜单按钮Keyboard Focus
-
主页面添加菜单聚焦事件
-
移除菜单页面时,更新主页面
Keyboard Focus
取消蓝框
Project Settings
效果
使用手柄操作
同键盘方向键操作
键位 | |
---|---|
左侧摇杆 | 上下左右移动 |
Face Button Bottom | 确认 |
Face Button Right | 后退 |
VR模式下使用手柄射线
PC_UMG设置bVR为true
VRPawn
MotionController
为左右两个手柄各添加一个MotionController,以Motion Source区分是哪个
MotionController | Motion Source |
---|---|
MotionControllerLeft | Left |
MotionControllerRight | Right |
WidgetInteraction
在左右两个MotionController下挂载WidgetInteraction,使得可以与Widget交互
通过Pointer Index区分左右
WidgetInteraction | Pointer Index |
---|---|
WidgetInteractionLeft | 0 |
WidgetInteractionRight | 1 |
Sphere
指示MotionController位置
为手柄Trigger添加输入
-
添加键位映射
-
按下Trigger时,模拟鼠标左键点击
效果
如有需要,在关卡中添加地板和出生点
注意到,从菜单页面返回时,聚焦到了第一个按钮
VR模式下使用手柄摇杆
键盘方向键实现导航功能,由虚幻底层提供。
如果要添加VR手柄导航功能,需覆写NavigationConfig。该操作在PC中完成。
-
创建C++类:派生自PlayerController
-
实现蓝图函数:更新NavigationConfig
键位 左侧摇杆 上下左右移动 A 确认 B 后退 1#include "Framework/Application/NavigationConfig.h" 2 3UFUNCTION(BlueprintCallable) 4void UpdateNavCustomPlayerController() 5{ 6 auto InNavConf = FSlateApplication::Get().GetNavigationConfig(); 7 8 InNavConf.Get().AnalogHorizontalKey = EKeys::OculusTouch_Left_Thumbstick_X; 9 InNavConf.Get().AnalogVerticalKey = EKeys::OculusTouch_Left_Thumbstick_Y; 10 11 InNavConf.Get().KeyActionRules.Emplace(EKeys::OculusTouch_Right_A_Click, EUINavigationAction::Accept); 12 InNavConf.Get().KeyActionRules.Emplace(EKeys::OculusTouch_Right_B_Click, EUINavigationAction::Back); 13 14 FSlateApplication::Get().SetNavigationConfig(InNavConf); 15}
-
使能Slate和SlateCore
1PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
-
修改PC_UMG基类,调用UpdateNavCustomPlayerController
效果