.h文件
// Copyright Access! GameStudio. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidgetPool.h"
#include "Subsystems/WorldSubsystem.h"
#include "ObjectPoolSubsystem.generated.h"
/**
*
*/
USTRUCT(BlueprintType)
struct FActorPool
{
GENERATED_BODY()
public:
FActorPool(){
};
void SetWorld(UWorld* World)
{
if(World)
{
OwningWorld = World;
}
}
bool IsInitialized() const {
return OwningWorld.IsValid(); }
AActor* GetActor(TSubclassOf<AActor> ActorClass);
//创建actor
template <typename ActorT = AActor>
AActor* GetOrCreateActor(TSubclassOf<ActorT> ActorClass)
{
if (!ActorClass || !ensure(IsInitialized()))
{
return nullptr;
}
AActor* Actor = nullptr;
for (AActor* InactiveActor : InactiveActors)
{
if (InactiveActor->StaticClass() == ActorClass)
{
Actor = InactiveActor;
InactiveActors.RemoveSingleSwap(InactiveActor);
break;
}
}
if(Actor)
{
SetActorEnable(Actor,true);
}
if (!Actor)
{
Actor = OwningWorld.Get()->SpawnActor(ActorClass);
if (!Actor)
{
return nullptr;

该文章展示了在UnrealEngine中实现的一个名为UObjectPoolSubsystem的系统,用于管理游戏中的对象池,如Actor和UserWidget。它包含FActorPool结构,用于存储和检索Actor实例,支持对象的创建、释放和重用,优化游戏性能。
最低0.47元/天 解锁文章
2622

被折叠的 条评论
为什么被折叠?



