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

}
相关推荐
zhangzhangkeji3 天前
UE5 C++(71):文件是否存在,文件夹是否存在,FPaths :: FileExists( const FString & InPath) ;
ue5
妙为3 天前
UE5角色穿过石头穿模
ue5·unreal engine5·角色穿越石头·穿模
技术策划Boring4 天前
2025年工作复盘:开放世界3A项目配置管线与性能监控的探索
游戏·ue5·虚幻·p4·perforce
zhangzhangkeji6 天前
UE5 C++(70-2):定义成员函数 getCleanDirectory(..) 和枚举类 EFileDirectoryType,来获得目录
ue5
avi91117 天前
UE4-UE5虚幻引擎-前置学习三,优化,基础CPP
ue5·ue4·游戏开发·虚幻·游戏优化·游戏代码
zhangzhangkeji7 天前
UE5线程进阶(3-2):任务图的相关源码整理。 FGraphEvent 与 TGraphTask 的区别和联系
ue5
zhangzhangkeji9 天前
UE5线程进阶(3-1):
ue5
zhangzhangkeji9 天前
UE5线程进阶(2-3):enum ENamedThreads命名空间 :: Type : int32 { RHIThread = 0 } 是渲染硬件接口线程
ue5
zhangzhangkeji10 天前
UE5线程进阶(2-1):枚举类EAsyncExecution,作业类TAsyncRunnable、TAsyncQueuedWork,及全局线程函数 Async(..),及线程调用的 4 种方法总结
ue5
zhangzhangkeji11 天前
UE5线程进阶(1):
ue5