ue 5 c++ 控制播放动画实践

目录

[蓝图中调用c++ actor](#蓝图中调用c++ actor)

关卡蓝图调用PlayTalkAnim

关卡蓝图,新建工具类,继承Actor,

[MyActor 设置播放速度 播放时间](#MyActor 设置播放速度 播放时间)


蓝图中调用c++ actor

打开 Content Drawer

找到你这个 C++ 类:

MyActor

右键 MyActor → Create Blueprint Class Based on MyActor

命名:BP_MyActor

然后把BP_MyActor 拖进关卡,

关卡蓝图调用PlayTalkAnim

关卡蓝图,新建工具类,继承Actor,

类里面自己加载动画。

MyActor 设置播放速度 播放时间

MetahumancharacterHeiXi\MyActor.h

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Animation/AnimationAsset.h"
#include "Components/SkeletalMeshComponent.h"
#include "MyActor.generated.h"

UCLASS()
class METAHUMANCHARACTERHEIXI_API AMyActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor();

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UAnimationAsset* TalkAnim;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UAnimSequence* TalkSeq;
	UFUNCTION(BlueprintCallable, Category = "Talk")
	void PlayTalkAnim(USkeletalMeshComponent* TargetMesh);

	UFUNCTION(BlueprintCallable, Category = "Talk")
	void PlayAnimSegment(
		USkeletalMeshComponent* TargetMesh,
		float StartTime,
		float EndTime
	);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

MetahumancharacterHeiXi\MyActor.cpp

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "MyActor.h"

#include "UObject/ConstructorHelpers.h"
// Sets default values
AMyActor::AMyActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
    static ConstructorHelpers::FObjectFinder<UAnimationAsset> AnimObj(
        TEXT("/Game/anim_new/talk03.talk03")
    );

    UE_LOG(LogTemp, Error, TEXT("load Game/anim_new/talk03.talk03 ok"));

    if (AnimObj.Succeeded())
    {
        TalkAnim = AnimObj.Object;
    }
	
	static ConstructorHelpers::FObjectFinder<UAnimSequence> AnimSeg(
		TEXT("/Game/anim_new/talk03.talk03")
	);

	if (AnimSeg.Succeeded())
	{
		TalkSeq = AnimSeg.Object;
	}
}

void AMyActor::PlayTalkAnim(USkeletalMeshComponent* targetMesh)
{

    UE_LOG(LogTemp, Error, TEXT("PlayTalkAnim 111"));
    if (!targetMesh || !TalkAnim)
    {
        UE_LOG(LogTemp, Warning, TEXT("Body or TalkAnim is null"));
        return;
    }
    UE_LOG(LogTemp, Error, TEXT("PlayTalkAnim 222"));
    // 对应蓝图:Set Global Anim Rate Scale
    targetMesh->GlobalAnimRateScale = 1.f;

    // 必须切换到 Single Node
    targetMesh->SetAnimationMode(EAnimationMode::AnimationSingleNode);
    UE_LOG(LogTemp, Error, TEXT("PlayTalkAnim 333"));
    // 对应蓝图:Play Animation
    targetMesh->PlayAnimation(TalkAnim, true); // true = Looping
}

void AMyActor::PlayAnimSegment(
	USkeletalMeshComponent* TargetMesh,
	float StartTime,
	float EndTime
)
{
	if (!TargetMesh || !TalkSeq) return;
	if (EndTime <= StartTime) return;

	// 用单节点动画模式(非常重要)
	TargetMesh->SetAnimationMode(EAnimationMode::AnimationSingleNode);

	TargetMesh->PlayAnimation(TalkSeq, false);
	TargetMesh->SetPosition(StartTime, false);

	const float PlayLength = EndTime - StartTime;

	// 定时在 EndTime 停止
	FTimerHandle StopHandle;
	GetWorld()->GetTimerManager().SetTimer(
		StopHandle,
		[TargetMesh]()
		{
			if (TargetMesh)
			{
				TargetMesh->Stop();
			}
		},
		PlayLength,
		false
	);
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void AMyActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
相关推荐
1204157137 肖哥1 天前
UE5.7 Procedural Vegetation分析
ue5
半天法师2 天前
Bug 记录:UE 结构体转 JSON 时 Key 字段大小写异常 (Editor 与打包后表现不一致)
ai·ue5·json·bug
邪修king2 天前
UE5 零基础入门第四弹:UMG UI 系统入门,从静态界面到逻辑联动
c++·ui·ue5
HAPPY酷3 天前
UE5 开发工具链配置清单
ue5
晴夏。3 天前
UE5第三人称模板实现及相关引擎源码分析
unity·ue5·游戏引擎·ue
HAPPY酷4 天前
UE5 C++ 避坑指南:暴力移除 Electronic Nodes 插件,回归纯净开发
开发语言·c++·ue5
晴夏。4 天前
UE原生第三人称相机源码分析
游戏·ue5·ue4·相机·ue·3c
郑寿昌5 天前
UE5中FBX材质丢失终极修复指南
ue5·材质
郑寿昌6 天前
AI生成的FBX格式导入UE5模型的兼容性优化指南
ue5
2601_957190907 天前
超元力悬浮玻璃剧场:当光影踏空而来,重构沉浸式文旅新体验
ue5·旅游