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

动态加载类和资源:指在运行时加载
.cpp

void AMyActor::BeginPlay()
{
	Super::BeginPlay();

	//动态加载资源
	UStaticMesh* MyTempStaticMesh = LoadObject(nullptr,TEXT("/Script/Engine.StaticMesh'/Game/StarterContent/Shapes/Shape_NarrowCapsule.Shape_NarrowCapsule'"));	//nullptr:空指针
	if (MyTempStaticMesh)
	{
		MyMesh->SetStaticMesh(MyTempStaticMesh);
	}
	//动态加载类
	UClass* MyTempClass = LoadClass(this,TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_WallSconce.Blueprint_WallSconce_C'"));	//AActor:Actor类型;this:指这段代码自身,相当于蓝图的self
	if (MyTempClass)
	{
		//就是SpawnActor蓝图节点
		GetWorld()->SpawnActor(MyTempClass, FVector::ZeroVector, FRotator::ZeroRotator);	//缩放值不写默认为1。
	}
}

你可能感兴趣的:(C++,ue4,c++)