04c++呵呵老师【组件概念、基本继承关系和UProperties】

A开头的都可以放在场景中,U开头的都是组件,只能依附于其他组件,不能直接放入场景。

组件和actor的关系:组件只能依附于actor上,不能直接放在场景。actor好比是人,组件好比是衣服。

(1)RootComponent组件:

每一个actor都有一个默认的组件,来存储基础信息。是AActor的一个成员,用来存放组件树的顶级组件。

以下图为例。获取actor的位置,其实是获取了跟组件的位置,actor本身不存储信息。

 创建一个类,继承于actor

头文件

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

//在场景中设置Actor基础位置的基础场景组件
class USceneComponent;
//网格体组件
class UStaticMeshComponent;
//例子组件
class UParticleSystemComponent;
//音频组件
class UAudioComponent;
//碰撞盒子组件
class UBoxComponent;

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

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

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

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

	UPROPERTY(EditAnywhere, Category = "MyBox")
		UBoxComponent* MyBox;

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

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

};

源文件

#include "MyComponent.h"
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "Components/AudioComponent.h"
#include "Components/BoxComponent.h"
// Sets default values
AMyComponent::AMyComponent()
{
 	// 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(RootComponent);

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

	MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyAudio"));
	MyAudio->SetupAttachment(RootComponent);

	MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBox"));
	MyBox->SetupAttachment(RootComponent);

}

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

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

}

编译

得到蓝图子类

拖入蓝图

在蓝图里可以看到这些组件

点击组件,可以设置

测试成功 

=============================================================

声明UProperties时使用的关键词,以指定属性与引擎和编辑器诸多方面的相处方式。

1.UPROPERTY 用途广泛。它允许变量被复制、被序列化,让编辑器和蓝图访问。

2.可以让垃圾回收机制自动回收。

详情请看:

https://docs.unrealengine.com/4.26/zh-CN/ProgrammingAndScripting/GameplayArchitecture/Properties/Specifiers/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无情的阅读机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值