U++ Slate小技巧 slate组件继承

1、.Padding(10.f)等于.Padding(10.f, 10.f, 10.f, 10.f)

2、LOCTEXT("键值", "输出或显示的TEXT"):注意,键值如果相同,例如:

LOCTEXT("1", "我") //返回的是“我”
LOCTEXT("1", "你") //返回的是“我”
LOCTEXT("2", "你") //返回的是“你”

因为1最开始注册的值是我,之后用键值为1的LOCTEXT,不管你第二个参数是多少,都是和第一次注册的值是一样的


SMyCompoundWidget.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Widgets/SCompoundWidget.h"
#include "Widgets/Input/SButton.h"

/**
 * 
 */
class SMyCompoundWidget : public SButton
{
public:
	SLATE_BEGIN_ARGS(SMyCompoundWidget)
	{}
	SLATE_END_ARGS()

	

	/** Constructs this widget with InArgs */
	void Construct(const FArguments& InArgs);
};

SMyCompoundWidget.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "SMyCompoundWidget.h"
#include "SlateOptMacros.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SMyCompoundWidget::Construct(const FArguments& InArgs)
{
	SButton::Construct(SButton::FArguments()
		.ContentPadding(FMargin(10, 5, 10, 5)));

	ChildSlot
	[
		//SNew(SButton)
		//Populate the widget
		SNew(SVerticalBox)
		+ SVerticalBox::Slot()
			.AutoHeight()
		[
			SNew(SButton)
		]
		+ SVerticalBox::Slot()
			.Padding(0, 10, 0, 0)
		[
			SNew(SButton)
		]
	];
	
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

Inherit.cpp:

// Copyright Epic Games, Inc. All Rights Reserved.

#include "Inherit.h"
#include "InheritStyle.h"
#include "InheritCommands.h"
#include "LevelEditor.h"
#include "Widgets/Docking/SDockTab.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Text/STextBlock.h"
#include "ToolMenus.h"
#include "SMyCompoundWidget.h"

static const FName InheritTabName("Inherit");

#define LOCTEXT_NAMESPACE "FInheritModule"

void FInheritModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
	
	FInheritStyle::Initialize();
	FInheritStyle::ReloadTextures();

	FInheritCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FInheritCommands::Get().OpenPluginWindow,
		FExecuteAction::CreateRaw(this, &FInheritModule::PluginButtonClicked),
		FCanExecuteAction());

	UToolMenus::RegisterStartupCallback(FSimpleMulticastDelegate::FDelegate::CreateRaw(this, &FInheritModule::RegisterMenus));
	
	FGlobalTabmanager::Get()->RegisterNomadTabSpawner(InheritTabName, FOnSpawnTab::CreateRaw(this, &FInheritModule::OnSpawnPluginTab))
		.SetDisplayName(LOCTEXT("FInheritTabTitle", "Inherit"))
		.SetMenuType(ETabSpawnerMenuType::Hidden);
}

void FInheritModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.

	UToolMenus::UnRegisterStartupCallback(this);

	UToolMenus::UnregisterOwner(this);

	FInheritStyle::Shutdown();

	FInheritCommands::Unregister();

	FGlobalTabmanager::Get()->UnregisterNomadTabSpawner(InheritTabName);
}

TSharedRef<SDockTab> FInheritModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{
	FText WidgetText = FText::Format(
		LOCTEXT("WindowWidgetText", "Add code to {0} in {1} to override this window's contents"),
		FText::FromString(TEXT("FInheritModule::OnSpawnPluginTab")),
		FText::FromString(TEXT("Inherit.cpp"))
		);

	return SNew(SDockTab)
		.TabRole(ETabRole::NomadTab)
		[
			// Put your tab content here!
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
			[
				SAssignNew(temp, SMyCompoundWidget)
				//SNew(SButton)
			]
		];
}

void FInheritModule::PluginButtonClicked()
{
	FGlobalTabmanager::Get()->TryInvokeTab(InheritTabName);
}

void FInheritModule::RegisterMenus()
{
	// Owner will be used for cleanup in call to UToolMenus::UnregisterOwner
	FToolMenuOwnerScoped OwnerScoped(this);

	{
		UToolMenu* Menu = UToolMenus::Get()->ExtendMenu("LevelEditor.MainMenu.Window");
		{
			FToolMenuSection& Section = Menu->FindOrAddSection("WindowLayout");
			Section.AddMenuEntryWithCommandList(FInheritCommands::Get().OpenPluginWindow, PluginCommands);
		}
	}

	{
		UToolMenu* ToolbarMenu = UToolMenus::Get()->ExtendMenu("LevelEditor.LevelEditorToolBar");
		{
			FToolMenuSection& Section = ToolbarMenu->FindOrAddSection("Settings");
			{
				FToolMenuEntry& Entry = Section.AddEntry(FToolMenuEntry::InitToolBarButton(FInheritCommands::Get().OpenPluginWindow));
				Entry.SetCommandList(PluginCommands);
			}
		}
	}
}

#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FInheritModule, Inherit)

尽量放一个插件里面 


效果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值