1、直接生成Actor

1
GetWorld()->SpawnActor<AActor>();
1
GetWorld()->SpawnActor(AActor::StaticClass());
1
2
UWorld* world = GetWorld();
world->SpawnActor<AActor>(AActor::StaticClass());

SpawnActor 在 world 类中的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** Templated version of SpawnActor that allows you to specify a class type via the template type */
template< class T >
T* SpawnActor( const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
{
return CastChecked<T>(SpawnActor(T::StaticClass(), NULL, NULL, SpawnParameters),ECastCheckedType::NullAllowed);
}

/** Templated version of SpawnActor that allows you to specify location and rotation in addition to class type via the template type */
template< class T >
T* SpawnActor( FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
{
return CastChecked<T>(SpawnActor(T::StaticClass(), &Location, &Rotation, SpawnParameters),ECastCheckedType::NullAllowed);
}

/** Templated version of SpawnActor that allows you to specify the class type via parameter while the return type is a parent class of that type */
template< class T >
T* SpawnActor( UClass* Class, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
{
return CastChecked<T>(SpawnActor(Class, NULL, NULL, SpawnParameters),ECastCheckedType::NullAllowed);
}

/**
* Templated version of SpawnActor that allows you to specify the rotation and location in addition
* class type via parameter while the return type is a parent class of that type
*/
template< class T >
T* SpawnActor( UClass* Class, FVector const& Location, FRotator const& Rotation, const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters() )
{
return CastChecked<T>(SpawnActor(Class, &Location, &Rotation, SpawnParameters),ECastCheckedType::NullAllowed);
}
/**
* Templated version of SpawnActor that allows you to specify whole Transform
* class type via parameter while the return type is a parent class of that type
*/
template< class T >
T* SpawnActor(UClass* Class, FTransform const& Transform,const FActorSpawnParameters& SpawnParameters = FActorSpawnParameters())
{
return CastChecked<T>(SpawnActor(Class, &Transform, SpawnParameters), ECastCheckedType::NullAllowed);
}

2、滞后生成Actor

1
2
3
4
5
6
// 创建actor生成变换
FTransform tran;
// 滞后生成Actor
AActor* actor = GetWorld()->SpawnActorDeferred<AActor>(AActor::StaticClass(), tran);
// 确定生成对象到世界
actor->FinishSpawning(tran);

SpawnActorDeferred 在 world 类中的定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* Spawns given class and returns class T pointer, forcibly sets world transform (note this allows scale as well). WILL NOT run Construction Script of Blueprints
* to give caller an opportunity to set parameters beforehand. Caller is responsible for invoking construction
* manually by calling UGameplayStatics::FinishSpawningActor (see AActor::OnConstruction).
*/
template< class T >
T* SpawnActorDeferred(
UClass* Class,
FTransform const& Transform,
AActor* Owner = nullptr,
APawn* Instigator = nullptr,
ESpawnActorCollisionHandlingMethod CollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::Undefined
)
{
if( Owner )
{
check(this==Owner->GetWorld());
}
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = CollisionHandlingOverride;
SpawnInfo.Owner = Owner;
SpawnInfo.Instigator = Instigator;
SpawnInfo.bDeferConstruction = true;
return (Class != nullptr) ? Cast<T>(SpawnActor(Class, &Transform, SpawnInfo)) : nullptr;
}