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

}
相关推荐
phltxy9 小时前
Redis 持久化机制
java·redis·git
学习,学习,在学习9 小时前
Q工控仪器程序框架设计详解(工控)
c++·qt·架构·qt5
j7~9 小时前
【Linux系统】基础IO(文件描述)(1)
linux·服务器·c++·文件·基础io
Gerardisite10 小时前
企业微信客户管理系统实战:标签、分层与自动化流程搭建
java·python·机器人·自动化·企业微信
计算机安禾10 小时前
【c++面向对象编程】第20篇:override与final关键字:现代C++对继承的控制
开发语言·c++
ch.ju10 小时前
Java程序设计(第3版)第三章——数组的定义方式
java·开发语言
郝学胜-神的一滴10 小时前
Qt 高级开发 004: 三大窗口类深度解析
开发语言·c++·qt·程序人生·系统架构
王老师青少年编程10 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串综合】:[NOIP 2004 普及组] FBI 树
c++·字符串·csp·高频考点·信奥赛·字符串综合·fbi树
楼田莉子10 小时前
Linux网络:多路转接IO
服务器·c++·后端·软件构建
Chloeis Syntax10 小时前
JavaEE学习日记(2)---文件操作和IO
java·笔记·学习·java-ee