本例使用 UE 4.26,只剩下 NewObject 用来创建 UObject,提供两个带不同可选参数构造函数的模板类
Outer 表示这个对象的外部对象,通常可传 this 指针进去
Name 为对象名,如果没有自定义,默认生成,自带 GetName() 方法获取
template<class T> T* NewObject(UObject* Outer) { T* Object = ::NewObject<T>(Outer); Object->SetInternalFlags(EInternalObjectFlags::Async); return Object; } template<class T> T* NewObject(UObject* Outer, UClass* Class, FName Name = NAME_None, EObjectFlags Flags = RF_NoFlags, UObject* Template = nullptr, bool bCopyTransientsFromClassDefaults = false, FObjectInstancingGraph* InInstanceGraph = nullptr) { T* Object = ::NewObject<T>(Outer, Class, Name, Flags, Template, bCopyTransientsFromClassDefaults, InInstanceGraph); Object->SetInternalFlags(EInternalObjectFlags::Async); return Object; }
创建一个 UObject 类
UCLASS() class TIPS_API UItemObject : public UObject { GENERATED_BODY() FString m_Name; public: UItemObject() { m_Name = GetName(); UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s"), *m_Name); } ~UItemObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" %s"), *m_Name); } };
创建 UObject 实例
UItemObject* Obj = NewObject<UItemObject>(); UItemObject* Obj2 = NewObject<UItemObject>(this, TEXT("Obj2"));
UObject及其派生 具有被 UE4 垃圾回收机制管理,因而当指向对象的指针为 nullptr 后,将会被 UE4 自动回收掉
Obj = NewObject<UItemObject>(this, TEXT("Obj")); Obj = nullptr;
UObject::ConditionalBeginDestroy()
Obj->ConditionalBeginDestroy(); Obj = nullptr;
MarkPendingKill()
Obj->MarkPendingKill(); Obj = nullptr;
Engine\Config \BaseEngine.ini 更改下面参数,设置销毁时间间隔
gc.TimeBetweenPurgingPendingKillObjects=60
UWorld::ForceGarbageCollection 弃用
GEngine->ForceGarbageCollection
GEngine->ForceGarbageCollection(true);
需要手动清理,易造成内存泄漏
delete 一般将指针置为 nullptr ,防止指向的地址不固定
class SimpleObject { public: SimpleObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); } ~SimpleObject() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__)); } };
SimpleObject* Obj = new SimpleObject(); delete Obj; Obj = nullptr;
支持容器 TArray、TMap 的 <UObject*>类型。TArray、TMap使用UPROPERTY() ,也可以使元素对象常驻内存
UPROPERTY() class UItemObject* m_ItemObject2; UPROPERTY() TArray<class UItemObject*> m_ObjList1;
构造时 和 AddToRoot 一起
析构时和 RemveFromRoot 一起
//创建对象 m_ItemObject1 = NewObject<UItemObject>(this,TEXT("m_ItemObject1")); m_ItemObject1->AddToRoot(); // 释放对象 m_ItemObject1->RemoveFromRoot(); m_ItemObject1 = nullptr;
FStreamableHandle::ReleaseHandle()
TSharedPtr<FStreamableHandle> Handle = UAssetManager::GetStreamableManager().RequestSyncLoad(AssetPath); UObject* Obj = Handle->GetLoadedAsset(); Handle->ReleaseHandle();
Destroy()
方法DestroyComponent()
方法扩展:FGCObjectScopeGuard