文章目录
- 前提
- 静态加载类和资源
- 静态加载资源
- 静态加载类
- 动态加载类和资源
- 动态资源
- 动态加载类
前提
有必要说一下,静态这块内容加载时我用UE5.2版本出现调用静态资源不是显示问题,修改后容易崩。所以,这里不建议5.2版本,直接用5.3,就不会出现这个问题。
导入初学者资源包
静态加载类和资源
静态加载资源
复制静态资源引用
/Script/Engine.StaticMesh’/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone’
#pragma once#include "CoreMinimal.h"
// 引入组件
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Components/AudioComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "GameFramework/Actor.h"
#include "MyCustomActor.generated.h"UCLASS()
class EXAMPLE_API AMyCustomActor : public AActor
{GENERATED_BODY()public:// Sets default values for this actor's propertiesAMyCustomActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;// 自定义组件UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScenet")USceneComponent *MySceneComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")UStaticMeshComponent *MyMeshComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScenet")UBoxComponent *MyBoxComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")UAudioComponent *MyAudioComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")UParticleSystemComponent *MyParticleSystemComponent;
};
#include "MyCustomActor.h"// Sets default values
AMyCustomActor::AMyCustomActor()
{// 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;// 创建组件MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));MyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));MyAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CustomAudio"));MyParticleSystemComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("CustomParticleSystem"));// 把组件添加到根组件RootComponent = MySceneComponent;MyMeshComponent->SetupAttachment(MySceneComponent);MyBoxComponent->SetupAttachment(MySceneComponent);MyAudioComponent->SetupAttachment(MyBoxComponent);MyParticleSystemComponent->SetupAttachment(MySceneComponent);// 静态加载资源// 静态Meshstatic ConstructorHelpers::FObjectFinder<UStaticMesh> TempStaticMesh(TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_Cone.Shape_Cone'"));MyMeshComponent->SetStaticMesh(TempStaticMesh.Object);// 粒子特效static ConstructorHelpers::FObjectFinder<UParticleSystem> TempParticleSystem(TEXT("/Script/Engine.ParticleSystem'/Game/StarterContent/Particles/P_Explosion.P_Explosion'"));MyParticleSystemComponent->SetTemplate(TempParticleSystem.Object);// 音频static ConstructorHelpers::FObjectFinder<USoundWave> TempSoundBase(TEXT("/Script/Engine.SoundWave'/Game/StarterContent/Audio/Explosion02.Explosion02'"));MyAudioComponent->SetSound(TempSoundBase.Object);
}// Called when the game starts or when spawned
void AMyCustomActor::BeginPlay()
{Super::BeginPlay();
}// Called every frame
void AMyCustomActor::Tick(float DeltaTime)
{Super::Tick(DeltaTime);
}
编译后,会显示引用上
然后想,创建蓝图类BP_MyCustomActor
到这里,都显示引用上。
静态加载类
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyClasss")TSubclassOf<AActor> MyActorClass;
加载静态类时,在引用资源类的末尾需要加上 _C,否则编译时会报错
// 静态加载类 加载静态类时,在引用资源类的末尾需要加上 _C,否则编译时会报错static ConstructorHelpers::FClassFinder<AActor> TempActorClass(TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'"));MyActorClass = TempActorClass.Class;UE_LOG(LogTemp, Warning, TEXT("MyActorClass is %s"), *MyActorClass->GetName());
编译后,创建蓝图,打开后打印日志
动态加载类和资源
动态资源
void AMyCustomActor::BeginPlay()
{Super::BeginPlay();// 动态加载资源UStaticMesh *TempStaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'"));if (TempStaticMesh){MyMeshComponent->SetStaticMesh(TempStaticMesh);}else{UE_LOG(LogTemp, Warning, TEXT("TempStaticMesh is null"));}
}
编译后,创建蓝图拖到场景中,点击运行
动态加载类
void AMyCustomActor::BeginPlay()
{// 动态加载类UClass *TempActorClass = LoadObject<UClass>(this, TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));if (TempActorClass){AActor *TempActor = GetWorld()->SpawnActor<AActor>(TempActorClass,FVector(0,0,0),FRotator(0,0,0));UE_LOG(LogTemp, Warning, TEXT("TempActorClass is %s"), *TempActorClass->GetName());}else{UE_LOG(LogTemp, Warning, TEXT("TempActorClass is null"));}
}
编译后,创建蓝图拖到场景中,点击运行