美文网首页
十、RPGAssetManager.h/cpp & AssetM

十、RPGAssetManager.h/cpp & AssetM

作者: 珏_Gray | 来源:发表于2019-06-17 15:38 被阅读0次

    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
     */
    

    突然发现内容有点少,既然提到了asset manager,那我们把资源加载、资源引用也一并学了吧。
    参考官方文档:

    阅读之后,你应该知道:

    • 什么是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中的全局数据,如tag等
        UAbilitySystemGlobals::Get().InitGlobalData();
    }
    
    /**  同步加载指定资源  , TryLoad() 会调用 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;
    }
    
    

    相关文章

      网友评论

          本文标题:十、RPGAssetManager.h/cpp & AssetM

          本文链接:https://www.haomeiwen.com/subject/wygafctx.html