当前位置: 首页 > article >正文

UE4 C++ 静态加载类和资源

静态加载类和资源:指在编译时加载,并且只能在构造函数中编写代码
.h

//增加所需组件的头文件
#include "Components/SceneComponent.h"			//场景组件
#include "Components/StaticMeshComponent.h"		//静态网格体组件
#include "Components/BoxComponent.h"			//Box碰撞体组件
#include "Components/AudioComponent.h"			//音频组件
#include "Components/ChildActorComponent.h"		//子Actor组件
#include "Particles/ParticleSystemComponent.h"	//粒子组件

UCLASS()
class 工程名称_API AMyActor : public AActor
{
	//定义组件变量
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class USceneComponent* MyScene;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class UStaticMeshComponent* MyMesh;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class UParticleSystemComponent* MyParticle;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class UBoxComponent* MyBox;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class UAudioComponent* MyAudio;

	//定义ChildActor组件类型变量
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyComponent")
	class UChildActorComponent* MyChildActor;
}

.cpp

AMyActor::AMyActor()	//注意是在这里实现,不是BeginPlay、Tick
{
 	// 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"));
	MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
	MyParticle = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("MyParticle"));
	MyBox = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBox"));
	MyAudio = CreateDefaultSubobject<UAudioComponent>(TEXT("MyAudio"));
	MyChildActor = CreateDefaultSubobject<UChildActorComponent>(TEXT("MyChildActor"));

	//设置父子层级关系
	RootComponent = MyScene;
	MyMesh->SetupAttachment(MyScene);
	MyParticle->SetupAttachment(MyScene);
	MyBox->SetupAttachment(MyScene);
	MyAudio->SetupAttachment(MyBox);
	MyChildActor->SetupAttachment(MyScene);

	//静态加载资源:设置Component中的属性值
	static ConstructorHelpers::FObjectFinder<UStaticMesh>TempMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));	//引用一个StaticMesh资产,并存储到变量“TempStaticMesh”。FObjectFinder:查找Object
	MyMesh->SetStaticMesh(TempMesh.Object);	//调用“SetStaticMesh”的方法,设置MyMesh组件的StaticMesh属性值
	static ConstructorHelpers::FObjectFinder<UParticleSystem>TempParticle(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Fire.P_Fire'"));	
	MyParticle->SetTemplate(TempParticle.Object);
	static ConstructorHelpers::FObjectFinder<USoundWave>TempAudio(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Collapse01.Collapse01'"));	
	MyAudio->SetSound(TempAudio.Object);
	//静态加载资类:设置ChildActor组件中的ChildActorClass属性值
	static ConstructorHelpers::FClassFinder<AActor>TempActor(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'"));	//注意这里是FClassFinder:查找Class。注意被引用的Actor要加后缀”_C“
    MyChildActor->SetChildActorClass(TempActor.Class);	//注意这里是TempActor.Class
}

在这里插入图片描述


http://www.kler.cn/a/228176.html

相关文章:

  • 【机器学习】穷理至极,观微知著:微积分的哲思之旅与算法之道
  • 【python】四帧差法实现运动目标检测
  • 量子编程语言:Qiskit 与 Cirq
  • 32、【OS】【Nuttx】OSTest分析(1):stdio测试(二)
  • CSS 提示工具:优化网页设计,提升用户体验
  • 《Memory Barriers a Hardware View for Software Hackers》阅读笔记
  • 05 - python操作JSON
  • 安卓9宫格密码键盘
  • 装饰你的APP:使用Lottie-Android创建动画效果
  • 2024清洁能源、环境与智慧城市国际研讨会(ISCEESC2024)
  • 如何使用第三方API采集电商数据呢?
  • Jetson AGX Orin安装Anaconda,Cuda,Cudnn,pytorch,Tensorrt,ROS
  • 【爬虫实战】全过程详细讲解如何使用python获取抖音评论,包括二级评论
  • Java后端面试总结
  • 一文讲明Jetpack中的图片组件
  • openEuler 22.03 docker安装elasticsearch 7.17.17
  • 【云原生运维问题记录】kubesphere登录不跳转问题
  • docker- php7.4
  • 【Linux】EXT2文件系统 | 磁盘分区块组 | inode
  • 基于springboot智慧养老平台源码和论文
  • 1.0 Hadoop 教程
  • 深度学习在智能交互中的应用:人与机器的和谐共生
  • 【下一代分布式追踪】将Trace扩展到网络设备
  • web 技术栈有哪些?
  • SQL Server之DML触发器
  • docker 构建个人博客网站