简介

Blueprint 组成部分

Blueprint 系统 主要由4个类组成, 详细介绍可见 UE4 Blueprint 架构分析

  • UBlueprint: 包含蓝图所有的信息类(包括了函数,变量,宏,蓝图连线的等一切蓝图信息)
  • UEdGraphSchema: 蓝图工具类,提供蓝图节点操作所需的所有操作方法
  • UEdGraph: 蓝图容器类,包含了蓝图上的所有节点信息
  • UEdGraphNode: 蓝图节点,定义各类功能的蓝图节点

FEdGraphSchemaAction

FEdGraphSchemaAction 是创建所有Node的工厂类的父类

根据不同的Node类型,UE4提供了不同的FEdGraphSchemaAction模板子类来创建Node

FEdGraphSchemaAction 分为几大类

FEdGraphSchemaAction_

FAISchemaAction_

FMaterialGraphSchemaAction_

FNiagaraSchemaAction_

FSoundCueGraphSchemaAction_

FSoundSubmixGraphSchemaAction_

创建 Node 实例

UAnimGraphNode_StateMachine

//创建节点 模板
UAnimGraphNode_StateMachine* UAnimGraphNode_StateMachineTemplate = NewObject<UAnimGraphNode_StateMachine>();
//设置模板,StateMachine 不需要设置模板

//使用对应的FEdGraphSchemaAction创建节点,并返回节点
UAnimGraphNode_StateMachine* RootStateMachineNode = FEdGraphSchemaAction_NewStateNode::SpawnNodeFromTemplate(AnimationGraph, UAnimGraphNode_StateMachineTemplate, FVector2D(-300.0f, 0.0f));

UAnimStateNode

//创建AnimStateNode节点
const auto AnimStateNode = FEdGraphSchemaAction_NewStateNode::SpawnNodeFromTemplate( AnimationStateMachineGraph, NewObject<UAnimStateNode>(), FVector2D(300.0f, 100.0f * i));
//使用工具类FBlueprintEditorUtils,对节点惊醒重命名
FBlueprintEditorUtils::RenameGraph(AnimStateNode->GetBoundGraph(), TEXT("NewStateName"));

UAnimGraphNode_SequencePlayer

//加载资源路径上的资源
UObject* AnimSequenceObject = LoadObject<UObject>(nullptr, TEXT("AssetPath"));
UAnimGraphNode_SequencePlayer* AnimSequence = FEdGraphSchemaAction_NewNode::SpawnNodeFromTemplate( AnimationStateGraph, NewObject<UAnimGraphNode_SequencePlayer>(), FVector2D(-300.0f, 0));
if (const auto Asset = Cast<UAnimationAsset>(AnimSequenceObject))
{
	AnimSequence->SetAnimationAsset(Asset);
}

UK2Node_VariableGet

//通过UE4 反射机制获取属性
FProperty* Property = UNextAnimStrategyUtilities::GetProperty(NextAnimNormalAnimInstance->GetClass(), "IsIdle");
							
if (Property)
{
	auto VariableGetTemplate = NewObject<UK2Node_VariableGet>();
	//将属性和StaticClass 设置到UK2Node_VariableGet 模板中
	VariableGetTemplate->SetFromProperty(Property, true, NextAnimNormalAnimInstance->GetClass());
	const auto VariableGet = FEdGraphSchemaAction_NewNode::SpawnNodeFromTemplate(Graph, VariableGetTemplate, FVector2D(-300.0f, 0));
}

FProperty* UAnimStrategyUtilities::GetProperty(UClass* StaticClass, FString PropertyName)
{
	FProperty* Property = nullptr;
	TFieldIterator<FProperty> PropIt(StaticClass);
	for (; PropIt; ++PropIt)
	{
		auto Name = PropIt->GetName();
		if (PropIt->GetName().Equals(PropertyName))
		{
			Property = *PropIt;
		}
	}

	return Property;
}

UK2Node_CallFunction

UK2Node_CallFunction

蓝图中的取反节点,它其实是一个函数节点,使用SetExternalMember设置函数方法,其实也是通过UE4的反射机制,将字符串类型名映射到CallFunctionTemp中

auto CallFunctionTemp = NewObject<UK2Node_CallFunction>();
//
CallFunctionTemp->FunctionReference.SetExternalMember( "Not_PreBool", UKismetMathLibrary::StaticClass());
const auto CallFunction = FEdGraphSchemaAction_NewNode::SpawnNodeFromTemplate(Graph, CallFunctionTemp, FVector2D(-300.0f, -100));

总结

  1. 不同的Graph对应不同Schema
  2. Schema下各不相同的Node 又对应着各个不同FEdGraphSchemaAction模板方法
  3. 如果需要自定义蓝图系统,也应该遵循这个规则