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

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

}

当然,这份代码的层级关系如下:
在这里插入图片描述

继承关系

各类继承关系如下
在这里插入图片描述

小作业

在这里插入图片描述
作业代码,依次是.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);

}


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值