UE4中组件概念和四大继承关系

文章目录

  • 1.组件的概念
    • TestActor.h
    • TestActor.cpp
  • 继承关系
  • 小作业

1.组件的概念

以A开头的,都是可以放置到场景中的,以U开头的,都是组件,可以依附于Actor,但是不能单独放置到场景中。

例如,我们在自动控制摄像机那一节,给一个Actor添加一个摄像机,这就是组件,同样的,也可以用类似方法添加光源、声源这些组件。

当然,有的东西可以既是Actor,也是组件

组件是用来丰富Actor的。

下面我们可以从代码里面看看Actor与组件的关系。同时有很多常用组件。

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"

class USceneComponent;
class UParticleSystemComponent;
class UStaticMeshComponent;
class UAudioComponent;
class UBoxComponent;

UCLASS()
class QUICKSTART_API ATestActor : public AActor//这是一个Actor,组件依附于此
{
	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;

	//场景组件,记录基本信息,旋转、位置、缩放等。
	USceneComponent *MyScene;//U开头,是组件
	
	UPROPERTY(EditAnywhere, Category = "MyMesh")
		UStaticMeshComponent *MyMesh;//U开头,是组件

	UPROPERTY(EditAnywhere, Category = "MyParticle")
		UParticleSystemComponent *MyParticle;

	UPROPERTY(EditAnywhere, Category = "MyAudio")
		UAudioComponent *MyAudio;

	UPROPERTY(EditAnywhere, Category = "Box")
		UBoxComponent* MyBoxComponent;
};

TestActor.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "TestActor.h"
#include "Components/StaticMeshComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.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;
	
	//添加根组件,记录基本信息,旋转、位置、缩放等
	MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MyScene"));
	RootComponent = MyScene;//RootComponent是AActor中的一个成员,存储物体的基本信息,是AActor组件树中的顶级组件
	
	//添加静态网格体,用于显示物体形状
	MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
	MyMesh->SetupAttachment(MyScene);//MyMesh添加到组件树的根节点上

	//添加粒子组件,显示例子特效
	MyParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyParticle"));
	MyParticle->SetupAttachment(MyMesh);//添加到组件树的MyMesh节点上

	//添加声音组件
	MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyAudio"));
	MyAudio->SetupAttachment(MyMesh);//添加到组件树的MyMesh节点上

	//添加盒体组件,碰撞检测用,和物体发生碰撞的范围
	MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBox"));
	MyBoxComponent->SetupAttachment(MyMesh);//添加到组件树的MyMesh节点上
}

// Called when the game starts or when spawned
void ATestActor::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

当然,这份代码的层级关系如下:
UE4中组件概念和四大继承关系_第1张图片

继承关系

各类继承关系如下
UE4中组件概念和四大继承关系_第2张图片

小作业

UE4中组件概念和四大继承关系_第3张图片
作业代码,依次是.h, .cpp

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "HomeworkActor.generated.h"

class USceneComponnet;
class UStaticMeshComponent;
class UParticleSystemComponent;

UCLASS()
class QUICKSTART_API AHomeworkActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AHomeworkActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	USceneComponent* MyScene;

	UPROPERTY(EditAnywhere, Category = "MyMesh")
		UStaticMeshComponent* MyMesh;

	UPROPERTY(EditAnywhere, Category = "MyParticle")
		UParticleSystemComponent* MyParticle;

};
// Fill out your copyright notice in the Description page of Project Settings.


#include "HomeworkActor.h"
#include "Components/StaticMeshComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.h"


// Sets default values
AHomeworkActor::AHomeworkActor()
{
 	// 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;

	MyScene = CreateDefaultSubobject<USceneComponent>(TEXT("MyScene"));
		RootComponent = MyScene;

		MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
		MyMesh->SetupAttachment(MyScene);

		MyParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyParticle"));
		MyParticle->SetupAttachment(MyMesh);
}

// Called when the game starts or when spawned
void AHomeworkActor::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}


你可能感兴趣的:(虚幻引擎4,c++,ue4,学习)