UE4c++ Brush创建

  • 前言
    UE4常用的new FSlateBrush的方式与正常的SlateStyle的方式就不说了,这类的文章很多,这里主要仿照引擎源码加载图片的方式加加载

参考源码:FTestStyle

.h

class FMainStyle 
{
public:

	static TSharedRef< class ISlateStyle > Create();

	static const ISlateStyle& Get()
	{
		return *( Instance.Get() );
	}

	static void ResetToDefault()
	{
		SetStyle( FMainStyle::Create() );
	}

private:

	static void SetStyle( const TSharedRef< class ISlateStyle >& NewStyle )
	{
		if ( Instance.IsValid() )
		{
			FSlateStyleRegistry::UnRegisterSlateStyle( *Instance.Get() );
		}

		Instance = NewStyle;

		if ( Instance.IsValid() )
		{
			FSlateStyleRegistry::RegisterSlateStyle( *Instance.Get() );
		}
		else
		{
			ResetToDefault();
		}
	}

private:

	/** Singleton instances of this style. */
	static TSharedPtr< class ISlateStyle > Instance;
};

.cpp

#define IMAGE_BRUSH( RelativePath, ... ) FSlateImageBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define BOX_BRUSH( RelativePath, ... ) FSlateBoxBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define BORDER_BRUSH( RelativePath, ... ) FSlateBorderBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define DEFAULT_FONT(...) FCoreStyle::GetDefaultFontStyle(__VA_ARGS__)

TSharedPtr< ISlateStyle > FMainStyle::Instance = nullptr;

TSharedRef<ISlateStyle> FMainStyle::Create()
{	
	TSharedRef< FSlateStyleSet > Style = MakeShareable( new FSlateStyleSet("MainStyle") );
	Style->SetContentRoot( FPaths::ProjectContentDir() / TEXT("Textures") );
	FVector2D BackGroundSize(400, 400);
	Style->Set( "BackGround", new IMAGE_BRUSH( "CheckBox_Checked", BackGroundSize ) );
	
	return Style; 
}

添加模块:"Slate","SlateCore"

初始化:

FMainStyle::ResetToDefault();

获取Brush:

SAssignNew(BackGroundImage, SImage)
					.Image(FMainStyle::Get().GetBrush("BackGround"))

图片资源放在自己定义的路径下面,比如我这是://Style->SetContentRoot( FPaths::ProjectContentDir() / TEXT("Textures") );

注意:这里不需要把图片导入引擎,直接放在对应路径使用的,所以,如果用这种方式,打包一定要包含路径,记得规范开发流程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值