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);

}
相关推荐
AI视觉网奇7 小时前
ue5 字典 字典动画 笔记
笔记·学习·ue5
速冻鱼Kiel1 天前
GASP笔记01
笔记·ue5·游戏引擎·虚幻
速冻鱼Kiel1 天前
GASP笔记02
笔记·ue5·游戏引擎·虚幻
曼巴UE51 天前
UE5 C++ 里创造 和 使用编辑GamePlayTag
c++·ue5·ue
XR101yqm12211 天前
2026年UE5 VS ChaosVantage实测对比,哪个更适合建筑可视化?
ue5·chaosvantage
zhangzhangkeji2 天前
UE5 C++(23-3):静态网络体与骨骼网络体的继承链。动态加载资源与类的源代码
ue5
曼巴UE52 天前
UE5 C++ GameInstanceSubsystem 在学习
c++·ue5·ue
zhangzhangkeji2 天前
UE5 C++(32):进度条 Progress 的实现,在蓝图里创建进度条,其数值绑定到 c++ 里的成员变量上
ue5
zhangzhangkeji2 天前
UE5 C++(29-2):描述碰撞响应方式的枚举类 enum ECollisionResponse。代码示例
ue5