UE4中如何不继承UObject就能spawn一个actor

介绍

其实不用继承 UObject 也可以生成一个 actor,

关键的点就是拿到 UWorld ,
所以只要从一个拥有 UWorld 的虚幻相关实例中传递它的 UWorld 给一个原生 C++ 类也可以.

示例代码

. . .

RealTimeSrvEntityFactory.h
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
40
41
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include <memory>
#include "RealTimeSrvEntity.h"
#include "RealTimeSrvPawn.h"

/**
*
*/

typedef RealTimeSrvEntityPtr( *GameObjectCreationFunc )( );

class RealTimeSrvEntityFactory
{
public:

static void StaticInit(UWorld * inWorld);

UWorld* GetWorld() const;

RealTimeSrvEntityPtr CreateGameObject( uint32_t inFourCCName );


void SetDefaultPawnClass( TSubclassOf<class ARealTimeSrvPawn> inDefaultCharacterClasses ) { DefaultCharacterClasses = inDefaultCharacterClasses; }


public:
static std::unique_ptr<RealTimeSrvEntityFactory> sInstance;

private:
RealTimeSrvEntityFactory();
RealTimeSrvEntityPtr CreateActionPawn();


private:
TSubclassOf<class ARealTimeSrvPawn> DefaultCharacterClasses;

UWorld* mWorld;
};
RealTimeSrvEntityFactory.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Fill out your copyright notice in the Description page of Project Settings.


#include "RealTimeSrvEntityFactory.h"
#include "RealTimeSrvWorld.h"

std::unique_ptr<RealTimeSrvEntityFactory> RealTimeSrvEntityFactory::sInstance;

RealTimeSrvEntityFactory::RealTimeSrvEntityFactory()
{
}


void RealTimeSrvEntityFactory::StaticInit(UWorld * inWorld)
{
sInstance.reset( new RealTimeSrvEntityFactory() );
check( sInstance );
if (sInstance)
{
sInstance->mWorld = inWorld;
}
}

UWorld* RealTimeSrvEntityFactory::GetWorld() const
{
return mWorld;
}



RealTimeSrvEntityPtr RealTimeSrvEntityFactory::CreateGameObject( uint32_t inFourCCName )
{

switch ( inFourCCName )
{
case 'CHRT':
return CreateActionPawn();
default:
break;
}

return RealTimeSrvEntityPtr();
}

RealTimeSrvEntityPtr RealTimeSrvEntityFactory::CreateActionPawn()
{
check( GetWorld() );

UWorld* const world = GetWorld();

if ( world )
{

FActorSpawnParameters SpawnParams;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
ARealTimeSrvPawn* const newActionPawn = world->SpawnActor<ARealTimeSrvPawn>( DefaultCharacterClasses, FTransform::Identity, SpawnParams );


if ( newActionPawn )
{
RealTimeSrvWorld::sInstance->AddGameObject( newActionPawn );
}
return RealTimeSrvEntityPtr( newActionPawn );

}
return RealTimeSrvEntityPtr();
}