- 在UE4中新建一个C++类:TestActor,继承子Actor ,UE4会自动在类的前面添加上大写字母A,最后生成的类是:ATestActor;
- 在UE4的内容面板中会出现新建Actor对象
在VS2019编辑器中,会出现对应的类:TestActor.h和TestActor.cpp
TestActor.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"
UCLASS()
class FIRSTPROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
TestActor.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestActor.h"
// Sets default values
ATestActor::ATestActor()
{
// 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 ATestActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ATestActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
- 在头文件中添加向量变量FVector,并且添加上蓝图属性
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
FVector initLocation = FVector(0.0, 0.0, 0.0);
注:
EditInstanceOnly:只有ATestActor实例化以后才可以编辑
BlueprintReadWrite:可以在蓝图中读写
Category:表示属性类别为"MyVar"
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"
UCLASS()
class FIRSTPROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestActor();
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
FVector initLocation = FVector(0.0, 0.0, 0.0);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
4.把新建的TestActor拖拽到场景中,在场景中什么也看不到(之所以看不到是因为没有添加组件),但是可以在世界面板中看到场景中已经有了一个ATestActor的对象实例。并且在细节面板中可以看到,新定义的变量类型“MyVars”
5.添加组件,并且设置组件是在蓝图中随处可见“VisibleAnywhere”,定义类别:“MyMess”
UPROPERTY(VisibleAnywhere, Category = "MyMess")
UStaticMeshComponent* mess = nullptr;
在构造函数中需要初始化一个对象
mess = CreateDefaultSubobject<UStaticMeshComponent>("TestMess");
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestActor.generated.h"
UCLASS()
class FIRSTPROJECT_API ATestActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATestActor();
UPROPERTY(VisibleAnywhere, Category = "MyMess")
UStaticMeshComponent* mess = nullptr;
UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Category="MyVars")
FVector initLocation = FVector(0.0, 0.0, 0.0);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "TestActor.h"
// Sets default values
ATestActor::ATestActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
mess = CreateDefaultSubobject<UStaticMeshComponent>("TestMess");
}
// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
Super::BeginPlay();
SetActorLocation(initLocation);//开始运行的时候,位置调整到当前位置
}
// Called every frame
void ATestActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
回到UE4Editor中,把内容面板中中TestActor拖动到场景中,可以在细节面板中可以看到对应的组件信息,并且可以选择不同的静态网格体。
aaa