61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
// Copyright Epic Games, Inc. All Rights Reserved.
|
||
|
||
#include "Factory/QuestAssetFactory.h"
|
||
#include "ProjectFish/DataAsset/QuestAsset.h"
|
||
#include "AssetRegistry/AssetRegistryModule.h"
|
||
#include "AssetRegistry/AssetData.h"
|
||
|
||
UQuestAssetFactory::UQuestAssetFactory()
|
||
{
|
||
SupportedClass = UQuestAsset::StaticClass();
|
||
bCreateNew = true;
|
||
bEditAfterNew = true;
|
||
}
|
||
|
||
UObject* UQuestAssetFactory::FactoryCreateNew(UClass* Class, UObject* InParent, FName Name, EObjectFlags Flags,
|
||
UObject* Context, FFeedbackContext* Warn)
|
||
{
|
||
UQuestAsset* NewQuestAsset = NewObject<UQuestAsset>(InParent, Class, Name, Flags | RF_Transactional);
|
||
|
||
// 自动生成唯一的 QuestID
|
||
if (NewQuestAsset)
|
||
{
|
||
NewQuestAsset->QuestID = GenerateUniqueQuestID();
|
||
}
|
||
|
||
return NewQuestAsset;
|
||
}
|
||
|
||
int32 UQuestAssetFactory::GenerateUniqueQuestID() const
|
||
{
|
||
int32 MaxQuestID = 0;
|
||
|
||
// 获取资产注册表模块
|
||
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
|
||
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
|
||
|
||
// 查找所有 QuestAsset 资产
|
||
TArray<FAssetData> AssetDataList;
|
||
AssetRegistry.GetAssetsByClass(UQuestAsset::StaticClass()->GetClassPathName(), AssetDataList, true);
|
||
|
||
// 遍历所有已存在的任务资产,找到最大的 QuestID
|
||
for (const FAssetData& AssetData : AssetDataList)
|
||
{
|
||
if (UQuestAsset* QuestAsset = Cast<UQuestAsset>(AssetData.GetAsset()))
|
||
{
|
||
if (QuestAsset->QuestID > MaxQuestID)
|
||
{
|
||
MaxQuestID = QuestAsset->QuestID;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 返回最大ID + 1,如果没有现有任务则从1开始
|
||
return MaxQuestID + 1;
|
||
}
|
||
|
||
bool UQuestAssetFactory::ShouldShowInNewMenu() const
|
||
{
|
||
return true;
|
||
}
|