UE5.2自定义Slate控件步骤

1.创建空项目

2.继承Slate的SLeafWidget类

class UIDESIGNER_API SColumnChart :public SLeafWidget

3.用slate的宏声明自定义控件


	SLATE_DECLARE_WIDGET(SColumnChart, SLeafWidget)

4.用slate的宏创建构造函数

	SLATE_BEGIN_ARGS(SColumnChart) {}
	SLATE_END_ARGS()
	void Construct(const FArguments& InArgs);

5.重写必要函数

Construct:用于构造控件结构


void SColumnChart::Construct(const SColumnChart::FArguments InArgs)
{
	
	ChildSlot
		.VAlign(VAlign_Top)
		.HAlign(HAlign_Center)
		
		[
			SNew(SBox)
				.WidthOverride(100)
				.HeightOverride(100)
				[
					SNew(SButton)
						.HAlign(HAlign_Center)
						.VAlign(VAlign_Center)
						// 设置按钮文本
						.Text(FText::FromString("Click me!"))
				]
			
		];
}

PrivateRegisterAttributes::不知道有什么用但是必须实现

void SColumn::PrivateRegisterAttributes(FSlateAttributeInitializer& AttributeInitializer)
{
	
}

ComputeDesiredSize:不知道有什么用但是必须实现(可能是给父控件槽调整大小用的)

FVector2D SColumn::ComputeDesiredSize(float) const
{
	return FVector2D(0,0);
}

6.重写OnPaint函数

不需要自定义绘制可以可以不用重写

如果需要自定义绘制重写的OnPaint必须包含绘制子控件的代码(否则将不会显示Construct函数构建的内容)

int32 SColumnChart::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
{

	//获取子控件布局信息
	FArrangedChildren ArrangedChildren(EVisibility::Visible);
	{
		this->ArrangeChildren(AllottedGeometry, ArrangedChildren);
	}
	//存在子控件就绘制子控件
	if (ArrangedChildren.Num() != 0)
	{
		//获取父控件的可用性
		const bool bShouldBeEnable = ShouldBeEnabled(bParentEnabled);
		//SCompoundWidget只能有一个子控件
		check(ArrangedChildren.Num() == 1);
		FArrangedWidget& TheChild = ArrangedChildren[0];
		//控件格式
		FWidgetStyle CompoundWidgetStyle = FWidgetStyle(InWidgetStyle)
			.BlendColorAndOpacityTint(GetColorAndOpacity())
			.SetForegroundColor(bShouldBeEnable ? GetForegroundColor() : GetDisabledForegroundColor());

		{
#if WITH_VERY_VERBOSE_SLATE_STATS
			SCOPE_CYCLE_COUNTER(STAT_ChildPaint);
#endif
			LayerId = TheChild.Widget->Paint(Args, AllottedGeometry, MyCullingRect, OutDrawElements, LayerId, CompoundWidgetStyle, bShouldBeEnable);
		}
	}

	return LayerId;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值