AssetManager
請先閱讀官方文檔:資源管理
/**
* A singleton UObject that is responsible for loading and unloading PrimaryAssets, and maintaining game-specific asset references
* Games should override this class and change the class reference
*/
突然發(fā)現(xiàn)內(nèi)容有點少,既然提到了asset manager,那我們把資源加載、資源引用也一并學了吧。
參考官方文檔:
閱讀之后,你應(yīng)該知道:
- 什么是SoftObject、軟引用
- 資源加載:LoadObject<> 和 FStreamableManager的作用
(FStreamableManager實例可以通過AssetManager::GetStreamableManager()來獲得。與AssetManager不同,在引擎中可以有多個FStreamableManager。) - 你可以利用資源注冊表和對象庫檢索資源
RPGAssetManager.h/cpp
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "ActionRPG.h"
#include "Engine/AssetManager.h"
#include "RPGAssetManager.generated.h"
class URPGItem;
/**
* Game implementation of asset manager, overrides functionality and stores game-specific types
* It is expected that most games will want to override AssetManager as it provides a good place for game-specific loading logic
* This is used by setting AssetManagerClassName in DefaultEngine.ini
*/
UCLASS()
class ACTIONRPG_API URPGAssetManager : public UAssetManager
{
GENERATED_BODY()
public:
// Constructor and overrides
URPGAssetManager() {}
virtual void StartInitialLoading() override;
/** Static types for items */
static const FPrimaryAssetType PotionItemType;
static const FPrimaryAssetType SkillItemType;
static const FPrimaryAssetType TokenItemType;
static const FPrimaryAssetType WeaponItemType;
/** Returns the current AssetManager object */
static URPGAssetManager& Get();
/**
* Synchronously loads an RPGItem subclass, this can hitch but is useful when you cannot wait for an async load
* This does not maintain a reference to the item so it will garbage collect if not loaded some other way
*
* @param PrimaryAssetId The asset identifier to load
* @param bDisplayWarning If true, this will log a warning if the item failed to load
*/
URPGItem* ForceLoadItem(const FPrimaryAssetId& PrimaryAssetId, bool bLogWarning = true);
};
源文件:
// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "RPGAssetManager.h"
#include "Items/RPGItem.h"
#include "AbilitySystemGlobals.h"
const FPrimaryAssetType URPGAssetManager::PotionItemType = TEXT("Potion");
const FPrimaryAssetType URPGAssetManager::SkillItemType = TEXT("Skill");
const FPrimaryAssetType URPGAssetManager::TokenItemType = TEXT("Token");
const FPrimaryAssetType URPGAssetManager::WeaponItemType = TEXT("Weapon");
/** 獲得asset manager單例*/
URPGAssetManager& URPGAssetManager::Get()
{
URPGAssetManager* This = Cast<URPGAssetManager>(GEngine->AssetManager);
if (This)
{
return *This;
}
else
{
UE_LOG(LogActionRPG, Fatal, TEXT("Invalid AssetManager in DefaultEngine.ini, must be RPGAssetManager!"));
return *NewObject<URPGAssetManager>(); // never calls this
}
}
/** Starts initial load, gets called from InitializeObjectReferences */
void URPGAssetManager::StartInitialLoading()
{
Super::StartInitialLoading();
// 加載GAS中的全局數(shù)據(jù),如tag等
UAbilitySystemGlobals::Get().InitGlobalData();
}
/** 同步加載指定資源 , TryLoad() 會調(diào)用 LoadObject()*/
URPGItem* URPGAssetManager::ForceLoadItem(const FPrimaryAssetId& PrimaryAssetId, bool bLogWarning)
{
FSoftObjectPath ItemPath = GetPrimaryAssetPath(PrimaryAssetId);
// This does a synchronous load and may hitch
URPGItem* LoadedItem = Cast<URPGItem>(ItemPath.TryLoad());
if (bLogWarning && LoadedItem == nullptr)
{
UE_LOG(LogActionRPG, Warning, TEXT("Failed to load item for identifier %s!"), *PrimaryAssetId.ToString());
}
return LoadedItem;
}