UE4 C++动态的SpawnActor()
// Fill out your copyright notice in the Description page of Project Settings.
#include "OnePlayerController.h"
#include "HdsPlayer.h"
#include "OneCamera.h"
#include "OneGamemode.h"
#include "Kismet/GameplayStatics.h"
void AOnePlayerController::BeginPlay()
{
Super::BeginPlay();
AHdsPlayer* HdsPlayer = Cast<AHdsPlayer>(UGameplayStatics::GetPlayerPawn(GetWorld(),0));
FActorSpawnParameters SpawnInfo;
if (HdsPlayer)
{
//复制的路径没有_C,复制后记得加
UClass* BlueprintVar = StaticLoadClass(AOneCamera::StaticClass(), nullptr, TEXT("Blueprint'/Game/1game/Blueprint/BP_Camera.BP_Camera_C'"));
if (BlueprintVar!=nullptr)
{
AOneCamera* SpawnActor = GetWorld()->SpawnActor<AOneCamera>(BlueprintVar,HdsPlayer->GetTransform(),SpawnInfo);
if (SpawnActor)
{
UE_LOG(LogTemp,Warning,TEXT("sdfsdf"));
}
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5dfd7afc43e1f6614732fa50724418e5.png)