目录

设置动画播放速度:

蓝图中调用c++ actor

关卡蓝图调用PlayTalkAnim

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

MyActor 设置播放速度 播放时间


设置动画播放速度:

	//BodyMesh->GlobalAnimRateScale = 0.6f;
	BodyMesh->SetPlayRate(0.7f);

蓝图中调用c++ actor


打开 Content Drawer

找到你这个 C++ 类:

MyActor

右键 MyActor → Create Blueprint Class Based on MyActor

命名:BP_MyActor

然后把BP_MyActor 拖进关卡,

关卡蓝图调用PlayTalkAnim

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

类里面自己加载动画。

MyActor 设置播放速度 播放时间

MetahumancharacterHeiXi\MyActor.h

// 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

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

}

更多推荐