ue4-异步加载资源
- 异步加载资源
ue中的异步加载分为两种模式,具体源码在 AsyncLoading.cpp 中
- 主线程分帧加载。
- 多线程加载。
可以通过接口判断当前的异步模式是那种模式。
const bool bIsMultithreaded = FAsyncLoadingThread::IsMultithreaded();
思路
- 一个FVCharPartInfo物件结构体,里面id和TAssetPtr对mesh资源的的指针。
- 一个UItemInfoDatabase物件数据库,里面有一个物件列表TArray
- 一个全局单例类USolusDataSingleton,里面有个资源加载器struct FStreamableManager* AssetLoader和物件数据库class UItemInfoDatabase* ItemDatabase
- 使用加载器异步请求加载物件数据库里的物件列表
有个不好的地方就是,绑定加载完的回调指针,不能在获取对应的加载的对象
实现
物件和数据库都在 ItemInfoDatabase.h 中(++ps++:如果修改了 FVCharPartInfo,记得要重新启动一下编辑器,否则这个结构体在编辑器中没有变化)
ItemInfoDatabase.h
#pragma once#include "Engine/DataAsset.h"#include "ItemInfoDatabase.generated.h"USTRUCT() struct FVCharPartInfo {GENERATED_USTRUCT_BODY()UPROPERTY(EditAnywhere, Category = "DATA")int32 MeshID;UPROPERTY(EditAnywhere, Category = "DATA")TAssetPtr<USkeletalMesh> MeshResource;FVCharPartInfo(){MeshID = 0;MeshResource = FStringAssetReference("");} };//Holds a dynamic collection of character parts UCLASS(BlueprintType) class UItemInfoDatabase : public UDataAsset {GENERATED_UCLASS_BODY()UPROPERTY(EditAnywhere, Category = "Model List") //Exposes the array as editable on editorTArray<FVCharPartInfo> MeshList;public:UItemInfoDatabase(); };
ItemInfoDatabase.cpp
ItemInfoDatabase.cpp#include "MySlate.h"#include "ItemInfoDatabase.h"UItemInfoDatabase::UItemInfoDatabase(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) { }
在Editor中建个UItemInfoDatabase蓝图类
在编辑器中把资源引用添加进去,下面的单例类会直接加载这个UitemInfoDatabase载蓝图类
添加资源的引用
全局单例类,初始化时把数据库加进去
SolusDataSingleton.h
#pragma once#include "SolusDataSingleton.generated.h"struct FStreamableManager; class UItemInfoDatabase;UCLASS(Blueprintable, BlueprintType) class USolusDataSingleton : public UObject {GENERATED_BODY() public:USolusDataSingleton(const FObjectInitializer& ObjectInitializer);virtual ~USolusDataSingleton();static USolusDataSingleton* Get(); // Get method to access this objectstruct FStreamableManager* AssetLoader; // Your asset loaderclass UItemInfoDatabase* ItemDatabase; };
SolusDataSingleton.cpp
#include "MySlate.h"#include "SolusDataSingleton.h"#include "Engine.h"#include "Engine/StreamableManager.h"#include "Singleton/SolusDataSingleton.h"#include "Singleton/ItemInfoDatabase.h"USolusDataSingleton::USolusDataSingleton(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer) {AssetLoader = new FStreamableManager();//加载itemDataBase蓝图类UObject* obj = AssetLoader->SynchronousLoad(FStringAssetReference(TEXT("/Game/TopDownCPP/MeshItems/MeshDateBaseBP")));ItemDatabase = Cast<UItemInfoDatabase>(obj); }USolusDataSingleton::~USolusDataSingleton() {if (AssetLoader)delete AssetLoader; }USolusDataSingleton* USolusDataSingleton::Get() {USolusDataSingleton* DataInstance = Cast<USolusDataSingleton>(GEngine->GameSingleton); //这里指定配置文件中指定的单例类if (!DataInstance)return nullptr;elsereturn DataInstance; }
在BpLib中起个方法测试异步加载
//h UFUNCTION(BlueprintCallable, Category = "Async Load")static UObject* TestAsyncLoad(AMyChar* _myChar); //cpp bool UMyBpFuncLib::TestAsyncLoad(AMyChar* _myChar) {if (!_myChar)return false;FStreamableManager* BaseLoader = USolusDataSingleton::Get()->AssetLoader;UItemInfoDatabase* _database = USolusDataSingleton::Get()->ItemDatabase;if (!BaseLoader || !_database)return false;TArray<FStringAssetReference> ObjToLoad;for (int32 i = 0; i < _database->MeshList.Num(); ++i){ObjToLoad.AddUnique(_database->MeshList[i].MeshResource.ToStringReference());}//请求异步加载BaseLoader->RequestAsyncLoad(ObjToLoad, FStreamableDelegate::CreateUObject(_myChar, &AMyChar::TestAsyncLoad));return true; }//回调方法 void AMyChar::TestAsyncLoad() {FString str = FString::Printf(TEXT("--- AMyChar::TestAsyncLoad callback"));GEngine->AddOnScreenDebugMessage(0, 5.0f, FColor::Red, str); }
蓝图调用测试方法
结果
参考资料
- https://forums.unrealengine.com/showthread.php?5309-TUTORIAL-C-Runtime-Async-Load-Modular-Character-%28Intermediate%29
- https://wiki.unrealengine.com/TAssetPtr_and_Asynchronous_Asset_Loading
- https://docs.unrealengine.com/latest/INT/Programming/Assets/AsyncLoading/index.html
ue4-异步加载资源相关推荐
- Flutter进阶—网络与本地异步加载资源
通过网络加载JSON文件的实例 import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/ ...
- yepnope.js 异步加载资源文件
yepnope.js是一个能够根据输入条件来选择性异步加载资源文件的js脚本,可以在页面上仅加载用户需要的js/css. yepnope的优点: 可以同时处理javascript以及css 能够按条件 ...
- yepnope.js – 异步加载资源文件
yepnope.js是一个能够根据输入条件来选择性异步加载资源文件的js脚本,可以在页面上仅加载用户需要的js/css. 典型代码示例 yepnope({test : Modernizr.geoloc ...
- U3D Addressables异步加载资源,创建大物体卡顿解决方案
目前做的游戏里,因为是仙侠游戏,特效都是手绘的,需要用到特别多的SPINE文件,一个主角身上可能有上百个动画文件和贴图,创建起来特别特别慢,要四五十秒,试了一下,设置QualitySettings.S ...
- 3D引擎多线程:资源异步加载
本文原创版权归 博客园 flagship 所有,如有转载,请详细标明作者及原文出处,以示尊重! 作者:flagship 原文:3D引擎多线程:资源异步加载 资源异步加载恐怕是3D引擎中应用最为广泛的多 ...
- 异步加载 yepnope.js
yepnope.js是一个超高速的按条件异步加载资源的加载器,允许你只加载使用到的资源(css及js). 典型代码示例 yepnope({ test : Modernizr.geolocation ...
- UE4 使用UAssetManager进行资源的异步加载
加载资源的主要代码 void AMyAssetLoadManager::DoAsyncLoadAssetList() { UAssetManager &AssetManager = UAsse ...
- combotree 可以异步加载吗_Unity AssetBundle 资源打包,加载,本地缓存方式,安卓,PC本地加载路径问题...
AssetBundle的定义和作用: 1,AssetBundle是一个压缩包包含模型.贴图.预制体.声音.甚至整个场景,可以在游戏运行的时候被加载. 2,AssetBundle自身保存着互相的依赖关系 ...
- Webpack实战(九):实现资源按需加载-资源异步加载
第八篇[<教你搞懂webpack如果实现代码分片(code splitting)>] (https://blog.csdn.net/lfcss/article/details/104099 ...
最新文章
- careercup-C和C++ 13.6
- 【django轻量级框架】点击前端按钮执行本地python程序(os sys的使用)
- linux 软件 web管理系统,如何快速安装Webmin(linux系统web管理配置工具)
- leetcode算法题--链表中的下一个更大节点★
- 用ASP.NET AJAX框架扩展HTML Map控件
- 网站服务器处理器计划怎么设置,服务器处理器计划
- chrome 插件 vimium 介绍
- 信息系统项目管理知识--项目人力资源管理
- 如何设置input实现同时选中多个文件并同时上传
- BZOJ4974 字符串大师(kmp)
- “2012年度IT博客大赛”获奖感言--梦想、学习、坚持、自信、淡定
- Linux操作(4)—— 如何安装或卸载软件
- skywalking oap-server 域名配置
- win7焦点总是不停丢失的解决方法
- 对pca降维后的手写体数字图片数据分类_知识干货-机器学习-TSNE数据降维
- 软件部署——nvidia-docker的学习笔记
- 千锋锋云智慧联合百度重磅打造人工智能产业学院建设方案
- 小仙女讲JVM(1)—综述
- 纯CSS实现超美选项卡
- 新时代动画电影的互联网宣传
热门文章
- 医疗系统信息网络安全等级保护设计方案
- R实战:【实战分析】大盘历年几月是行情?
- iOS,Android调用手机QQ与指定用户或公众号聊天界面
- 【HDU4276】The Ghost Blows Light
- linux udev 命令,Linux下udev详细介绍
- 广州翰智软件有限公司
- Java:Scanner中的nextInt()方法
- 飞鹅小票打印机嵌入生成指定小程序页面二维码的解决方案 | 扫普通链接二维码打开小程序示例 | 生成正方形小程序码
- Android room 学习
- XP下使用aircrack注入式破解WEP(Commview支持网卡)