1.减少生成销毁的次数(提高对象复用率):对象池
比如UI组件,可以第一次生成,之后不需要则隐藏,需要就显示。可以一次性生成最大数量的对象,也可以每次判断数量不够就再生成。
示例代码:
#pragma once
#include "CoreMinimal.h"
#include "UObject/ObjectMacros.h"
#include "UObject/Interface.h"
#include "UICustomWidgetsPoolTemplate.generated.h"
UINTERFACE()
class UUIWidgetPoolInterface :public UInterface
{
GENERATED_BODY()
};
class IUIWidgetPoolInterface
{
GENERATED_IINTERFACE_BODY()
public:
virtual void Show() = 0;
virtual void Hide() = 0;
};
template<typename WidgetObject>
class UUIWidgetPool
{
public:
bool HasInit() { return objects.Num() > 0; }
void Init(TArray<WidgetObject*> _maxObjects)
{
objects.Empty();
objects = _maxObjects;
currentShowNum = objects.Num();
}
TArray<WidgetObject*> GetWi