一.
1.創建插件 Editor Standalone Window
2.目錄下會生成如下文件
分別為Buttom/ButtomCommands/ButtomStyle Buttom.Build.cs 文件
3. 打開ButtomCommands.cpp
通過斷點,觀察 以下函數順序
void FButtomCommands::RegisterCommands()
ButtomCommands.cpp #include "ButtomCommands.h"
#define LOCTEXT_NAMESPACE "FButtomModule"
//斷點位置
void FButtomCommands::RegisterCommands() { UI_COMMAND(OpenPluginWindow, "Buttom", "Bring up Buttom window", EUserInterfaceActionType::Button, FInputGesture()); } #undef LOCTEXT_NAMESPACE
如下圖,會看到調用順序為
調用位置1 void FButtomModule::StartupModule() { // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module FButtomStyle::Initialize(); FButtomStyle::ReloadTextures(); FButtomCommands::Register();
調用位置2 commands.h void Register
template<typename CommandContextType> class TCommands : public FBindingContext { public: /** Use this method to register commands. Usually done in StartupModule(). */ FORCENOINLINE static void Register() { if ( !Instance.IsValid() ) { TSharedRef<CommandContextType> NewInstance = MakeShareable( new CommandContextType() );
Instance = NewInstance;
NewInstance->RegisterCommands();
由此我們可見,通過實例化 Commands 然后調用 NewInstance->RegisterCommands()
二.CommandList
回到StartupModule中, FUICommandList主要為綁定一些命令 ,
void FButtomModule::StartupModule() { // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module FButtomStyle::Initialize(); FButtomStyle::ReloadTextures(); FButtomCommands::Register(); PluginCommands = MakeShareable(new FUICommandList); PluginCommands->MapAction( FButtomCommands::Get().OpenPluginWindow, FExecuteAction::CreateRaw(this, &FButtomModule::PluginButtonClicked), FCanExecuteAction());
MapAction :將命令信息映射到由multibox或鼠標/鍵盤輸入執行的一系列委托
貼一段MapAction :
void MapAction( const TSharedPtr< const FUICommandInfo > InUICommandInfo, FExecuteAction ExecuteAction, EUIActionRepeatMode RepeatMode = EUIActionRepeatMode::RepeatDisabled ); /** * Maps a command info to a series of delegates that are executed by a multibox or mouse/keyboard input * * @param InUICommandInfo The command info to map * @param ExecuteAction The delegate to call when the command should be executed * @param CanExecuteAction The delegate to call to see if the command can be executed * @param RepeatMode Can this action can be repeated if the chord used to call it is held down? */
基本可以看到 InUICommandInfo :要映射的命令信息,ExecuteAction:什么時候應該執行命令
通過create_raw的方式:
PluginCommands->MapAction( FButtomCommands::Get().OpenPluginWindow, FExecuteAction::CreateRaw(this, &FButtomModule::PluginButtonClicked), FCanExecuteAction());
找到 void FButtomModule::PluginButtonClicked()
void FButtomModule::PluginButtonClicked() { //FGlobalTabmanager::Get()->TryInvokeTab(ButtomTabName); }
//可以進行一些自定義操作,我們想實現的功能