generated from Basemash_UE/UE_Template
Initial Commit
This commit is contained in:
parent
910d1e3133
commit
658e69962b
1413 changed files with 6375 additions and 0 deletions
121
Source/Simulore_Tirsova/PreloadSubsystem.cpp
Normal file
121
Source/Simulore_Tirsova/PreloadSubsystem.cpp
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
#include "PreloadSubsystem.h"
|
||||
#include "AssetRegistry/AssetRegistryModule.h"
|
||||
#include "Engine/AssetManager.h"
|
||||
|
||||
void UPreloadSubsystem::StartPreload(const TArray<FString>& ContentPaths)
|
||||
{
|
||||
if (bIsPreloading)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("PreloadSubsystem: Already preloading, ignoring request."));
|
||||
return;
|
||||
}
|
||||
|
||||
IAssetRegistry& AssetRegistry = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry").Get();
|
||||
|
||||
PendingAssets.Empty();
|
||||
|
||||
for (const FString& Path : ContentPaths)
|
||||
{
|
||||
TArray<FAssetData> FoundAssets;
|
||||
AssetRegistry.GetAssetsByPath(FName(*Path), FoundAssets, /*bRecursive=*/ true);
|
||||
|
||||
for (const FAssetData& Asset : FoundAssets)
|
||||
{
|
||||
PendingAssets.Add(Asset.GetSoftObjectPath());
|
||||
}
|
||||
|
||||
UE_LOG(LogTemp, Log, TEXT("PreloadSubsystem: Found %d assets in '%s'"), FoundAssets.Num(), *Path);
|
||||
}
|
||||
|
||||
BeginAsyncLoad();
|
||||
}
|
||||
|
||||
void UPreloadSubsystem::PreloadAssets(const TArray<FSoftObjectPath>& Assets)
|
||||
{
|
||||
if (bIsPreloading)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("PreloadSubsystem: Already preloading, ignoring request."));
|
||||
return;
|
||||
}
|
||||
|
||||
PendingAssets = Assets;
|
||||
BeginAsyncLoad();
|
||||
}
|
||||
|
||||
void UPreloadSubsystem::BeginAsyncLoad()
|
||||
{
|
||||
TotalAssets = PendingAssets.Num();
|
||||
|
||||
if (TotalAssets == 0)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("PreloadSubsystem: No assets to preload."));
|
||||
OnPreloadComplete.Broadcast();
|
||||
return;
|
||||
}
|
||||
|
||||
UE_LOG(LogTemp, Log, TEXT("PreloadSubsystem: Starting async load of %d assets..."), TotalAssets);
|
||||
bIsPreloading = true;
|
||||
|
||||
FStreamableManager& Manager = UAssetManager::GetStreamableManager();
|
||||
StreamableHandle = Manager.RequestAsyncLoad(
|
||||
PendingAssets,
|
||||
FStreamableDelegate::CreateUObject(this, &UPreloadSubsystem::OnAssetsLoaded),
|
||||
FStreamableManager::DefaultAsyncLoadPriority,
|
||||
false, // bManageActiveHandle
|
||||
false, // bStartStalled
|
||||
TEXT("PreloadSubsystem")
|
||||
);
|
||||
|
||||
// Tick progress until complete
|
||||
TickerHandle = FTSTicker::GetCoreTicker().AddTicker(
|
||||
FTickerDelegate::CreateUObject(this, &UPreloadSubsystem::TickProgress),
|
||||
0.05f // 20 updates per second
|
||||
);
|
||||
}
|
||||
|
||||
bool UPreloadSubsystem::TickProgress(float DeltaTime)
|
||||
{
|
||||
if (!bIsPreloading)
|
||||
{
|
||||
return false; // stop ticking
|
||||
}
|
||||
|
||||
OnPreloadProgress.Broadcast(GetProgress());
|
||||
return true; // keep ticking
|
||||
}
|
||||
|
||||
void UPreloadSubsystem::OnAssetsLoaded()
|
||||
{
|
||||
UE_LOG(LogTemp, Log, TEXT("PreloadSubsystem: All %d assets loaded."), TotalAssets);
|
||||
|
||||
bIsPreloading = false;
|
||||
|
||||
if (TickerHandle.IsValid())
|
||||
{
|
||||
FTSTicker::GetCoreTicker().RemoveTicker(TickerHandle);
|
||||
TickerHandle.Reset();
|
||||
}
|
||||
|
||||
OnPreloadProgress.Broadcast(1.0f);
|
||||
OnPreloadComplete.Broadcast();
|
||||
}
|
||||
|
||||
float UPreloadSubsystem::GetProgress() const
|
||||
{
|
||||
if (!bIsPreloading || !StreamableHandle.IsValid())
|
||||
{
|
||||
return bIsPreloading ? 0.0f : 1.0f;
|
||||
}
|
||||
|
||||
return StreamableHandle->GetProgress();
|
||||
}
|
||||
|
||||
int32 UPreloadSubsystem::GetTotalAssetCount() const
|
||||
{
|
||||
return TotalAssets;
|
||||
}
|
||||
|
||||
bool UPreloadSubsystem::IsPreloading() const
|
||||
{
|
||||
return bIsPreloading;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue