UE4 在Actor Component中给Actor动态创建Component

版本UE4.20

创建C++ Class,继承自Actor Component,以动态创建SceneCaptureComponent2D为例。

.h

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

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "MyActorComponent.generated.h"

class USceneCaptureComponent2D;
class UTextureRenderTarget2D;
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UMyActorComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UMyActorComponent();

	UFUNCTION(BlueprintCallable, Category = "Actor Com")
		bool  Init2DCaptureComponent( USceneComponent* Parent, UTextureRenderTarget2D*  RenderTargets);
protected:
	// Called when the game starts
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

		
	
};

.cpp

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

#include "MyActorComponent.h"
#include "Components/SceneCaptureComponent2D.h"
#include "Engine/TextureRenderTarget2D.h"
#include "UObject/UObjectGlobals.h"
#include "GameFramework/Actor.h"

// Sets default values for this component's properties
UMyActorComponent::UMyActorComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UMyActorComponent::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}


// Called every frame
void UMyActorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

bool UMyActorComponent:: Init2DCaptureComponent(USceneComponent* Parent, UTextureRenderTarget2D*  RenderTargets)
{
	if (Parent == nullptr || RenderTargets == nullptr)
	{
		return false;
	}
		USceneCaptureComponent2D* New2DCamera = NewObject<USceneCaptureComponent2D>(GetOwner());
		New2DCamera->AttachToComponent(Parent, FAttachmentTransformRules::KeepRelativeTransform);
		New2DCamera->RegisterComponent();  //注意必不可少,否则创建的组件不起作用
		New2DCamera->TextureTarget = RenderTargets;
		New2DCamera->CaptureSource = ESceneCaptureSource::SCS_FinalColorLDR;
		New2DCamera->PrimitiveRenderMode = ESceneCapturePrimitiveRenderMode::PRM_RenderScenePrimitives;
		New2DCamera->SetRelativeRotation(FRotator(0.0, 90.0, 0.0));

	return New2DCamera->IsRegistered();
}

创建一个Actor蓝图

运行

结果:

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: b'ue4 actor component'是Unreal Engine 4的一个组件,可以附加到Actor上,并添加自定义功能,例如碰撞检测、动画控制和游戏逻辑。这样,多个Actor可以共享同一个组件,从而简化代码和提高效率。 ### 回答2: UE4 Actor Component是一个非常重要的功能模块,它可以让我们为UE4引擎的游戏角色添加自定义的行为和功能,而不需要修改原有的游戏代码。使用Actor Component,我们可以将游戏角色所拥有的行为和功能拆分成一个个独立的模块,并可以独立开发、测试、优化和修改,从而提高游戏的可维护性和开发效率。以下是Actor Component的主要特性: 1. 重用性:可以将Actor Component应用到不同的游戏角色,从而将相同的行为和功能重用。 2. 灵活性:可以根据需要添加或移除Actor Component,从而增强游戏角色的行为和功能。 3. 可拆分性:可以将复杂的游戏角色拆分成更小、更简单的组件,从而降低代码复杂度和维护成本。 4. 可扩展性:通过继承Actor Component类,我们可以创建自己的定制组件,从而增强游戏的定制化程度。 使用Actor Component开发游戏可以大大简化代码开发过程,加快开发时间,减少错误和bug,提高代码的可读性和可维护性。Actor ComponentUE4引擎的一个重要特性,也是开发高质量游戏的不可或缺的工具之一。 ### 回答3: UE4(Unreal Engine 4)是一款功能强大的游戏开发引擎,其ActorComponent模块是构建游戏对象的重要组成部分之一。Actor是在游戏场景的一个物体,而ComponentActor对象的一部分,经常用于添加特定功能和属性。UE4Actor Component能够帮助游戏开发人员在游戏实现更多的交互和动态效果。 UE4 Actor Component的设计思路是将不同的组件Actor划分为不同的模块,使其更加简单易用和灵活。通过UE4 Actor Component,开发人员可以根据自己的需求将多个组件组合在一个Actor,从而实现更复杂的游戏特效等功能。 UE4 Actor Component提供了很多常见的组件,如动画组件、碰撞器组件、模型组件、声音组件等,以及多种自定义组件。其,动画组件是游戏开发最常用的组件之一,可以使游戏的角色动作更加真实,使动画更加流畅。碰撞器组件则可以增加游戏的碰撞检测效果,模型组件可以快速地在场景添加物体,声音组件则可以为游戏增加更多音效。 UE4 Actor Component的另一个优势是可以通过连接多个组件来创建更为复杂的组件。通过将一个组件附加到另一个组件上,可以将两个组件的功能融合在一起,从而实现更多不同的游戏特效。例如,一个具有紫外线视觉效果的球体可以通过将光线源附加到它的子组件上来实现,这样就可以使球体在场景发出紫外线光线。 总之,UE4Actor Component是游戏开发非常重要的一部分,它能够提供丰富多彩的特效和功能,游戏开发人员可以通过组合不同的组件来实现自己的游戏开发需求。因此,学习UE4 Actor Component对于游戏开发人员来说是非常有必要的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值