一、Actor组件(场景组件调用)




.h头文件
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/SceneComponent.h"
#include "MySceneComponent_fan.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class TEST818_API UMySceneComponent_fan : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UMySceneComponent_fan();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="MyTest")
int abd=0;
float speed ;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MyTest")
FString str ;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="MyTest")
AActor* TargetActorFan;
void ModifyActorByTransform(AActor* TargetActor);
UFUNCTION(BlueprintCallable)
void TestFan(int number);
};
.cpp源文件
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MySceneComponent_fan.h"
// Sets default values for this component's properties
UMySceneComponent_fan::UMySceneComponent_fan()
{
PrimaryComponentTick.bCanEverTick = true;
str = TEXT("hahahh哈");
speed = 3.2f;
abd = 9;
// 加上这行测试:
UE_LOG(LogTemp, Warning, TEXT("====== 构造函数执行了,代码已更新! ======"));
}
// Called when the game starts
void UMySceneComponent_fan::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Warning, TEXT("999"));
UE_LOG(LogTemp, Warning, TEXT("你好啊,%s,我知道你的速度是 %f"), * str, speed);
// ...
UE_LOG(LogTemp, Warning, TEXT("Hello from MySceneComponent_fan!"));
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Hello from MySceneComponent_fan!"));
}
if (TargetActorFan)
{
ModifyActorByTransform(TargetActorFan);
}
//// 举例:获取当前组件所属的Actor(自己的父Actor)
//AActor* MyOwner = GetOwner();
}
// Called every frame
void UMySceneComponent_fan::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (GEngine) {
abd++;
if (abd > 15) {
}
else
{
// ...
TestFan(abd);
}
}
}
void UMySceneComponent_fan::ModifyActorByTransform(AActor* TargetActor)
{
if (!IsValid(TargetActor)) return;
FTransform NewTrans;
NewTrans.SetLocation(FVector(100, 0, 50)); // 位置
NewTrans.SetRotation(FRotator(0, 90, 0).Quaternion()); // 旋转:Yaw90度
NewTrans.SetScale3D(FVector(1.5, 1.5, 1.5)); // 缩放
TargetActor->SetActorTransform(NewTrans);
}
void UMySceneComponent_fan::TestFan(int number)
{
//UE_LOG(LogTemp, Display, TEXT("abd=%d"),number);
if (GEngine) {
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Blue, TEXT("fanhahahhah"));
}
}
二、Actor(动态对象)调用


此处以创建好的 BP_MyActorFan 为例


cpp
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActorFan.generated.h"
UCLASS()
class TEST818_API AMyActorFan : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActorFan();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable,Category="fan")
void SpawnActorFan();
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
TSubclassOf<AActor> actorTarget;
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
FTransform tran;
UPROPERTY(EditAnywhere,BlueprintReadWrite,Category="fan")
FString str;
};
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActorFan.h"
// Sets default values
AMyActorFan::AMyActorFan()
{
// 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;
}
// Called when the game starts or when spawned
void AMyActorFan::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActorFan::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMyActorFan::SpawnActorFan()
{
UE_LOG(LogTemp, Display, TEXT("开始生成%s"), *str);
FActorSpawnParameters param;
param.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
GetWorld()->SpawnActor<AActor>(actorTarget, tran, param);
}