You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UE5.2.1中如何在UMG派生类构造函数中正确初始化bIsFocusable

UE5.2.1中正确初始化UUserWidget的bIsFocusable属性

问题背景

在UE5.2.1版本中,若在运行时直接修改bIsFocusable会触发如下警告:

Direct access to bIsFocusable is deprecated. Please use the getter. Note that this property is only set at construction and is not modifiable at runtime. Please update your code to the new API before upgrading to the next release, otherwise your project will no longer compile.

尝试在构造函数初始化列表中直接初始化该属性时,无论是写bIsFocusable(true)还是UUserWidget::bIsFocusable(true),都会出现相同编译错误:

illegal member initialization: 'bIsFocusable' is not a base or member

正确解决方案

编译错误的原因是bIsFocusable已被封装,不再是子类可直接在初始化列表中访问的成员。正确做法是在构造函数体内调用UE官方提供的SetIsFocusable方法设置属性——警告明确说明该属性仅能在构造阶段设置,因此在构造函数中调用完全合规:

修改后的.cpp代码

#include "MyUserWidget.h"

UMyUserWidget::UMyUserWidget(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    // 在构造函数体内调用Setter设置焦点可交互性
    SetIsFocusable(true);
}

另外,也可以通过UPROPERTY元数据在头文件中直接指定默认值,无需编写构造函数:

头文件修改示例

UCLASS(meta=(IsFocusable=true))
class MUTIPLAYER_DEMO_API UMyUserWidget : public UUserWidget
{
    GENERATED_BODY()
};

补充说明

  • SetIsFocusable是UE替代直接访问bIsFocusable的官方API,仅在构造阶段调用有效,运行时调用不会生效(符合警告中的限制)。
  • 若使用蓝图继承该C类,也可直接在蓝图编辑器的细节面板中找到Is Focusable选项修改默认值,无需编写C代码。

内容的提问来源于stack exchange,提问作者KSroido

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 09:13:27