【发布时间】:2021-11-08 10:55:19
【问题描述】:
每次从小部件编辑器更改枚举变量属性时,我都必须更改子列表。
到目前为止,我尝试过的所有功能似乎都不起作用(virtual void OnEndEditByDesigner(); virtual void PostEditChangeProperty(struct FPropertyChangedEvent & PropertyChangedEvent))。
SynchronizeProperties 文档指出
编辑器也可以调用它来更新修改状态,因此请确保对小部件属性的所有初始化都在此处执行,否则属性和视觉状态可能会变得不同步。
所以我不完全确定它是使用/覆盖的正确函数(因为它只能“可以”被编辑器调用来更新修改状态,而不是在属性编辑器编辑时调用)
这里是一些示例代码,展示了我迄今为止一直在尝试做的事情。
示例代码:
SampleButton.h
/* Includes */
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "Components/Button.h"
#include "Components/Image.h"
#include "Components/CanvasPanel.h"
#include "Components/ButtonSlot.h"
#include "Widgets/SOverlay.h"
#include "Components/Overlay.h"
#include "Components/TextBlock.h"
#include "Styling/SlateBrush.h"
#include "UUIW_MasterButton.generated.h"
//
UENUM(BlueprintType)
enum class EContentType : uint8
{
WithIcon,
WithText,
}
class SAMPLE_API SampleButton : public UButton
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintSetter=SetType, meta = (MakeEditWidget = ""))
EContentType ContentType = EContentType::WithIcon;
UPROPERTY(meta = (EditCondition = "ContentType == EContentType::WithIcon || ContentType == EContentType::WithTextAndIcon", EditConditionHides))
UImage* Icon;
UPROPERTY(meta = (EditCondition = "ContentType == EContentType::WithText || ContentType == EContentType::WithTextAndIcon", EditConditionHides))
UTextBlock* TextBlock;
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
UOverlay* Overlay;
virtual void SynchronizeProperties();
}
SampleButton.cpp
#include "UI/SampleButton.h"
TSharedRef<SWidget> SampleButton::RebuildWidget()
{
Overlay = NewObject<UOverlay>();
auto ButtonRef = Super::RebuildWidget();
AddChild(Overlay);
Icon = NewObject<UImage>();
Icon->SetDisplayLabel(TEXT("Icon"));
TextBlock = NewObject<UTextBlock>();
TextBlock->SetDisplayLabel(TEXT("Text"));
switch (ContentType)
{
case EContentType::WithIcon:
Overlay->AddChild(Icon);
break;
case EContentType::WithText:
Overlay->AddChild(TextBlock);
break;
case EContentType::WithTextAndIcon:
Overlay->AddChild(Icon);
Overlay->AddChild(TextBlock);
break;
}
if ( GetChildrenCount() > 0 )
{
Cast<UButtonSlot>(GetContentSlot())->BuildSlot(MyButton.ToSharedRef());
}
return ButtonRef;
}
void UUUIW_MasterButton::SynchronizeProperties()
{
Super::SynchronizeProperties();
Overlay->ClearChildren();
switch (ContentType)
{
case EContentType::WithIcon:
Overlay->AddChild(Icon);
break;
case EContentType::WithText:
Overlay->AddChild(TextBlock);
break;
case EContentType::WithTextAndIcon:
Overlay->AddChild(Icon);
Overlay->AddChild(TextBlock);
break;
}
}
【问题讨论】:
标签: c++ widget unreal-engine4