创建一个工程项目,创建一个K2Node类(生成节点),创建一个蓝图函数库(实现节点内功能)
项目.Build.cs内添加模块
// Copyright Epic Games, Inc. All Rights Reserved.
using UnrealBuildTool;
public class NewCoustNode : ModuleRules
{
public NewCoustNode(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { "BlueprintGraph","UnrealEd","GraphEditor","KismetCompiler" });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
K2Node.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "K2Node.h"
#include "MyNode.generated.h"
/**
*
*/
UCLASS()
class NEWCOUSTNODE_API UMyNode : public UK2Node
{
GENERATED_BODY()
public:
// //鼠标放到上面的说明/注释
// virtual FText GetTooltipText()const override;
// //节点名称
// virtual FText GetNodeTitle(ENodeTitleType::Type TitleType)const override;
// //将节点添加到蓝图视图
// virtual void GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegister)const override;
// //蓝图节点的标签
// virtual FText GetMenuCategory()const;
// //展开节点
// virtual void ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)override;
// //分配默认引脚
// virtual void AllocateDefaultPins()override;
// //引脚更改时会调用
// virtual void PinDefaultValueChanged(UEdGraphPin* ChangedPin)override;
// //连接情况更改以后
// virtual void NotifyPinConnectionListChanged(UEdGraphPin* Pin)override;
//
// //创建一个可视小部件来在图形编辑器或图形面板中表示这个节点。如果没有实现,则将使用默认的节点工厂
// virtual TSharedPtr<SGraphNode> CreateVisualWidget() { return TSharedPtr<SGraphNode>(); }
// // 为表示此节点的小部件创建背景图像
// virtual TSharedPtr<SWidget> CreateNodeImage() const { return TSharedPtr<SWidget>(); }
// //右键菜单, 比如添加RemovePin
// virtual void GetNodeContextMenuActions(class UToolMenu* Menu, class UGraphNodeContextMenuContext* Context) const override;
//鼠标放到上面的说明/注释
virtual FText GetTooltipText() const override { return FText::FromString(TEXT("This node is used for everyone to learn")); }
//节点名称
virtual FText GetNodeTitle(ENodeTitleType::Type TitleType) const override { return FText::FromString(TEXT("My Node")); }
//蓝图节点的标签
virtual FText GetMenuCategory() const { return FText::FromString(TEXT("This is My ClassMenu")); }
//重写UK2Node的GetMenuActions函数,使其可以在右键菜单里找到这个蓝图节点
virtual void GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const override;
//分配默认引脚
virtual void AllocateDefaultPins() override;
//节点调用函数
virtual void ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)override;
};
K2Node.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "NewCoustNode/Public/MyNode.h"
#include "BlueprintActionDatabaseRegistrar.h"
#include "BlueprintNodeSpawner.h"
#include "K2Node_CallFunction.h"
#include "KismetCompiler.h"
#include "MyBlueprintFunctionLibrary.h"
//通用,可以固定格式
void UMyNode::GetMenuActions(FBlueprintActionDatabaseRegistrar& ActionRegistrar) const
{
Super::GetMenuActions(ActionRegistrar);
UClass* ActionKey = GetClass();
//ActionRegistrar需要include
if (ActionRegistrar.IsOpenForRegistration(ActionKey))
{
//UBlueprintNodeSpawner需要include
UBlueprintNodeSpawner* NodeSpawner = UBlueprintNodeSpawner::Create(GetClass());
check(NodeSpawner != nullptr);
ActionRegistrar.AddBlueprintAction(ActionKey, NodeSpawner);
}
}
void UMyNode::AllocateDefaultPins()
{
Super::AllocateDefaultPins();
//创建输入引脚
CreatePin(EGPD_Input, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Execute);
//创建输出引脚01
UEdGraphPin* staring = CreatePin(EGPD_Output, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Then);
staring->PinFriendlyName = FText::FromString("staring");
//创建输出引脚02
UEdGraphPin* Finished = CreatePin(EGPD_Output, UEdGraphSchema_K2::PC_Exec, UEdGraphSchema_K2::PN_Then);
Finished->PinFriendlyName = FText::FromString("finished");
}
void UMyNode::ExpandNode(FKismetCompilerContext& CompilerContext, UEdGraph* SourceGraph)
{
Super::ExpandNode(CompilerContext, SourceGraph);
UEdGraphPin* ExecPin = GetExecPin();
UEdGraphPin* ThenPin = GetThenPin();
if (ExecPin && ThenPin) {
// 调用一个BlueprintFunctionLibrary节点,需要include "UMyBlueprintFunctionLibrary"
FName MyFunctionName = GET_FUNCTION_NAME_CHECKED(UMyBlueprintFunctionLibrary, starting);
//UK2Node_CallFunction需要include,CompilerContext需要include并且模块内要添加"BlueprintGraph","UnrealEd","GraphEditor","KismetCompiler"
UK2Node_CallFunction* CallFuncNode = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this,SourceGraph);
CallFuncNode->FunctionReference.SetExternalMember(MyFunctionName, UMyBlueprintFunctionLibrary::StaticClass());
CallFuncNode->AllocateDefaultPins();
CompilerContext.MovePinLinksToIntermediate(*ExecPin, *(CallFuncNode->GetExecPin()));
FName MyFunctionName01 = GET_FUNCTION_NAME_CHECKED(UMyBlueprintFunctionLibrary, Finished);
UK2Node_CallFunction* CallFuncNode01 = CompilerContext.SpawnIntermediateNode<UK2Node_CallFunction>(this,SourceGraph);
CallFuncNode01->FunctionReference.SetExternalMember(MyFunctionName01, UMyBlueprintFunctionLibrary::StaticClass());
CallFuncNode01->AllocateDefaultPins();
CompilerContext.MovePinLinksToIntermediate(*ExecPin, *(CallFuncNode01->GetExecPin()));
// move pins
CompilerContext.MovePinLinksToIntermediate(*ThenPin, *(CallFuncNode->GetThenPin()));
CompilerContext.MovePinLinksToIntermediate(*ThenPin, *(CallFuncNode01->GetThenPin()));
}
}
BlueprintFunctionLibrary.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"
/**
*
*/
UCLASS()
class NEWCOUSTNODE_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true"))
static void starting();
UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true"))
static void Finished();
};
BlueprintFunctionLibrary.cpp文章来源:https://www.toymoban.com/news/detail-604193.html
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyBlueprintFunctionLibrary.h"
void UMyBlueprintFunctionLibrary::starting()
{
if (GEngine) {
const int32 AlwaysAddKey = -1;
GEngine->AddOnScreenDebugMessage(AlwaysAddKey,
5.0f,
FColor::Purple,
TEXT("starting...")
);
}
}
void UMyBlueprintFunctionLibrary::Finished()
{
if (GEngine) {
const int32 AlwaysAddKey = -1;
GEngine->AddOnScreenDebugMessage(AlwaysAddKey,
5.0f,
FColor::Red,
TEXT("finished...")
);
}
}
这是简单定制蓝图节点的实现过程文章来源地址https://www.toymoban.com/news/detail-604193.html
到了这里,关于UE5 自定义蓝图节点(记录,备忘)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!