ue5 自定义 actor ac++ actor 用法实战

目录

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

c++创建类:

MetahumancharacterHeiXi\MyActor.h

MetahumancharacterHeiXi\MyActor.cpp


蓝图中调用c++ actor

  • 打开 Content Drawer

  • 找到你这个 C++ 类:

    MyActor

  • 右键 MyActor → Create Blueprint Class Based on MyActor

  • 命名:BP_MyActor

然后把BP_MyActor 拖进关卡,

然后:

c++创建类:

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)
	USkeletalMeshComponent* Body_comp;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	UAnimationAsset* TalkAnim;

	UFUNCTION(BlueprintCallable, Category = "Talk")
	void PlayTalkAnim(USkeletalMeshComponent* TargetMesh);

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;
    }
    if (!Body_comp)
    {
        // 找到蓝图里第一个 SkeletalMeshComponent
        Body_comp = FindComponentByClass<USkeletalMeshComponent>();
        if (!Body_comp)
        {
            UE_LOG(LogTemp, Warning, TEXT("找不到 BodyMesh!"));
        }
    }
    UE_LOG(LogTemp, Error, TEXT("BodyMesh ok"));

}

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
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}
相关推荐
NE_STOP4 小时前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
后端AI实验室9 小时前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai
程序员清风11 小时前
小红书二面:Spring Boot的单例模式是如何实现的?
java·后端·面试
belhomme11 小时前
(面试题)Redis实现 IP 维度滑动窗口限流实践
java·面试
Be_Better11 小时前
学会与虚拟机对话---ASM
java
开源之眼14 小时前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
樱木Plus14 小时前
深拷贝(Deep Copy)和浅拷贝(Shallow Copy)
c++
Maori31615 小时前
放弃 SDKMAN!在 Garuda Linux + Fish 环境下的优雅 Java 管理指南
java
用户9083246027315 小时前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
小王和八蛋15 小时前
DecimalFormat 与 BigDecimal
java·后端