如何在Unreal Engine C++中按需使用模块实现功能拆分挂载
Unreal Engine 无引擎依赖模块的功能拆分实现
1. 创建纯逻辑模块(无引擎依赖)
- 在项目
Source目录下手动新建模块文件夹(比如CoreMovement),添加三个核心文件:CoreMovement.build.cs:配置模块为无引擎依赖using UnrealBuildTool; public class CoreMovement : ModuleRules { public CoreMovement(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; // 仅依赖Unreal核心基础模块,不关联Engine/Gameplay等引擎模块 PublicDependencyModuleNames.AddRange(new string[] { "Core" }); PrivateDependencyModuleNames.AddRange(new string[] { }); } }CoreMovement.h:声明核心逻辑类#pragma once #include "CoreMinimal.h" class COREMOVEMENT_API FMovementLogic { public: // 纯底层移动计算逻辑,无任何Unreal引擎类依赖 void CalculateMovement(float DeltaTime, FVector& CurrentVelocity, const FVector& InputDirection, float MaxSpeed); };CoreMovement.cpp:实现逻辑,完全与引擎解耦#include "CoreMovement.h" void FMovementLogic::CalculateMovement(float DeltaTime, FVector& CurrentVelocity, const FVector& InputDirection, float MaxSpeed) { // 纯数值计算:加速度插值、速度限制等 FVector DesiredVelocity = InputDirection.GetSafeNormal() * MaxSpeed; CurrentVelocity = FMath::VInterpTo(CurrentVelocity, DesiredVelocity, DeltaTime, 5.f); }
- 编辑项目
.uproject文件,将该模块加入Modules列表:"Modules": [ ... { "Name": "CoreMovement", "Type": "Runtime", "LoadingPhase": "Default" } ]
2. 实现引擎桥接层
创建依赖引擎+纯逻辑模块的中间层(可以是独立模块GameplayMovement,也可直接在游戏模块内实现),用Unreal组件类桥接底层逻辑与引擎对象:
#pragma once #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "MovementComponent.generated.h" // 桥接组件:负责和Unreal引擎交互,调用底层纯逻辑 UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent)) class GAMEPLAYMOVEMENT_API UMovementComponent : public UActorComponent { GENERATED_BODY() public: UMovementComponent(); protected: virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; private: // 底层纯逻辑实例,无引擎依赖 class FMovementLogic MovementLogic; UPROPERTY(EditAnywhere, Category="Movement") float MaxMoveSpeed = 600.f; };
#include "MovementComponent.h" #include "CoreMovement.h" UMovementComponent::UMovementComponent() { PrimaryComponentTick.bCanEverTick = true; } void UMovementComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); if (AActor* Owner = GetOwner()) { // 从引擎获取输入数据(示例,可替换为实际玩家输入逻辑) FVector InputDirection = FVector(0.f, 0.f, 0.f); // 调用底层纯逻辑计算速度 FVector CurrentVelocity = Owner->GetVelocity(); MovementLogic.CalculateMovement(DeltaTime, CurrentVelocity, InputDirection, MaxMoveSpeed); // 将计算结果反馈给引擎对象 Owner->SetActorLocation(Owner->GetActorLocation() + CurrentVelocity * DeltaTime); } }
3. 挂载组件到目标类
- 蓝图方式:打开玩家角色蓝图,直接添加
UMovementComponent组件即可,无需在角色类内编写移动逻辑。 - C++方式:在角色类构造函数中创建组件实例:
APlayerCharacter::APlayerCharacter() { ... MovementComponent = CreateDefaultSubobject<UMovementComponent>(TEXT("MovementComponent")); }
核心注意事项
- 纯逻辑模块禁止引入
Engine.h、GameplayClasses.h等引擎相关头文件,仅允许依赖CoreMinimal.h或Core.h,确保完全与引擎解耦。 - 桥接层统一处理所有引擎交互:获取引擎数据、调用底层逻辑、将结果应用到引擎对象,实现底层逻辑的复用性与可移植性。
- 明确模块依赖关系:纯逻辑模块仅依赖
Core,桥接模块依赖Core+纯逻辑模块+Engine。
内容的提问来源于stack exchange,提问作者user22674097
相关产品推荐
相关产品推荐

