UE4 Loading Resources asynchronously

Source: Internet
Author: User

Transferred from: http://blog.ch-wind.com/ue4%E5%BC%82%E6%AD%A5%E8%BD%BD%E5%85%A5%E8%B5%84%E6%BA%90/

All the resources pointed to by the "hard" pointers are loaded by UE4 at startup, and in order to prevent a huge delay in some cases, we need to load the system with asynchronous resources when necessary.

This article refers to: https://docs.unrealengine.com/latest/INT/Programming/Assets/AsyncLoading/index.html to organize. It is also a record of research engines.

It is important for asynchronous loading of two classes: Fstringassetreferences and tassetptr.

Fstringassetreferences is a "soft" reference to a resource (Asset), which is used in BP like a uobject pointer. The tassetptr is a weak reference to the Fstringassetreferences package, while the specification points to the type, you can call get () when needed to resolve to the specific resources.

In order to avoid the deviation of understanding, it is the quickest to actually test once. Add the following attributes in your code:

Uproperty (Visibleanywhere, blueprintreadwrite, Category = test)    = test)    tassetptr< Utexture2d> TTP;

Fstringassetreference is shown below in the visual editor and can be used as a uobject pointer:

The tassetptr is displayed as follows:

Most of the time, it doesn't make sense to load a single resource asynchronously unless he's really big. If you want to load resources asynchronously, you need to use object Libraries. This is the class that content Brower uses for resource filtering and display, and it is also possible to use it in game logic.

if (! objectlibrary) {       false, giseditor);       Objectlibrary-Addtoroot ();} Objectlibrary->loadassetdatafrompath (TEXT ("/game/pathwithallobjectsofsametype") ); if (bfullyload) {       objectlibrary-loadassetsfromassetdata ();}

The code above creates a library for the specified directory, and the resource is loaded. The second step is not required:

Tarray<fassetdata>assetdatas;objectlibrary-getassetdatalist (assetdatas); for(Int32 i =0; I < Assetdatas.num (); ++i) {Fassetdata& assetdata =Assetdatas[i]; Constfstring* foundtypenamestring =AssetData.TagsAndValues.Find (get_member_name_checked (uassetobject,typename)); if(Foundtypenamestring && foundtypenamestring->contains (TEXT ("Footype")))       {              returnAssetdata; }}

The above code is filtered in Objectlibrary and returns the first resource found.

After you get assetdata, you can convert it to fstringassetreference, and then use Streamablemanager to load asynchronously.

voidUgamecheatmanager::grantitems () {Tarray<FStringAssetReference>Itemstostream; Fstreamablemanager& streamable =Ugameglobals::get ().       Streamablemanager;  for(Int32 i =0; I < Itemlist.num (); ++i) {itemstostream.addunique (Itemlist[i].       Tostringreference ()); } streamable.requestasyncload (Itemstostream, Fstreamabledelegate::createuobject ( This, &ugamecheatmanager::grantitemsdeferred));} voidugamecheatmanager::grantitemsdeferred () { for(Int32 i =0; I < Itemlist.num (); ++i) {ugameitemdata* ItemData =Itemlist[i].              Get (); if(itemdata) {MyPC-Grantitem (ItemData); }       }}

Streamablemanager can load all the resources referenced by the stringreference that are passed to him. In the example above, itemlist is defined as tarray< tassetptr<ugameitem> >. It also says that for a list of weak references, all stringreference are passed to Streamablemanager for one-time asynchronous loading. After loading, the callback function is called to process it in a timely manner. Before the callback function, all references are retained by Streamablemanager to prevent them from being garbage collected.

As above, the Streamablemanager and object libaries are combined to enable asynchronous resource loading.

UE4 Loading Resources asynchronously

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.