Architectural Design appreciation of hearth stone (4): Asset Management

Source: Internet
Author: User

Welcome to reprint, please indicate the author [Yan Liang @ Game Development] and the original address: http://blog.csdn.net/neil3d/article/details/39580197
In addition, you are welcome to come to my QQ Group to exchange various game engine-related technologies:Can the game engine be used? (264656505)

In other words, after this period of study and exploration, the development idea of unity3d has been basically clear. There is only one assetbundle mechanism that has not yet been fully implemented. This involves the resource planning of the previous project, the writing of resource management code, and the implementation of the automatic update mechanism.

Therefore, I still want to take a look at the Asset Management of hearth stone after further analysis of the game logic. It must be said that the current analysis is a PC version of the Assembly, not necessarily suitable for mobile terminals, and as a case analysis.

This article describes the management mechanism of assetbundle in Firestone. Its mechanism is relatively simple and clear, and it is quite reasonable. The analysis process in the middle will not be discussed, and its architecture design and code logic organization will be directly presented. Start with the class related to asset management.

Class asset: Resource Information Description
The asset class does not manage direct resource objects, but stores information about an asset. For details, see. In addition, it also has a "paths" variable, which is a dictionary, the key is the assetfamily enumeration, and the value is the path and resource path of assetbundle. The following is a detailed description of assetfamily. Enum assetfamily-resource category
As shown in:
  • Hearth stone packages assetbundle based on different types of resources. One type of resources corresponds to one or more resource packages. (The rules for dividing one type of resources into multiple packages are unknown );
  • Some resource packages are packaged separately in a localized manner, for example, "fonts0.unity3d" = "fontszhcn0.unity3d ";
  • In the program, the resource package category corresponds to the enumerated type "assetfamily ";
  • The specific path information of the resource package is stored in asset. paths, which is a static variable. during initialization, manually enter the necessary information, such:
        Dictionary<AssetFamily, AssetFamilyPathInfo> dictionary = new Dictionary<AssetFamily, AssetFamilyPathInfo>();    AssetFamilyPathInfo info = new AssetFamilyPathInfo {        format = "Data/Actors/{0}.unity3d",        sourceDir = "Assets/Game/Actors"    };    info.exts = new string[] { "prefab" };    dictionary.Add(AssetFamily.Actor, info);
  • In addition, a class assetbundleinfo record the main file name corresponding to each assetbundle, the number of package files, and the corresponding object type. For details, see:
Class assetloader: Resource Loading
Various resources need to be loaded during game operation, basically through assetloader (resources. Load () is also applicable in some cases ()). Next we will focus on the implementation of assetloader.
Assetloader provides the resource object loading interface for the upper layer, and provides a set of functions for each type of resources, such as loadcardprefab and loadactor. Provides callback functions for object loading completion and loading progress. These functions are just some simple packaging, and they all call the loadcachedgameobject () or loadcachedobject () core functions internally. From the process of these two functions, we can see that resource loading uses the cache mechanism:
  • First, search from assetcache. If yes, update the timestamp of the cache item and call the callback;
  • If no request is found, add a request to assetcache and start coroutine: createcachedasset (). The call steps are as follows:
    • Call assetcache. startloading ();
    • Start coroutine: createcachedasset_frombundle <requesttype> ():
      • Use assetloader. getbundleforasset () to find the assetbundle to which the resource belongs;
      • Call assetbundle. loadasync () to actually load resources;
      • During the loading process, functions such as onloadfailed (), onloadsucceeded (), and onprogressupdate () of assetcache. cacherequest are called according to the processing results;
    • Search for this resource in assetcache. If yes, the resource is loaded successfully and the callback function is called;
      Call assetcache. stoploading ();

We all know that assetbundle cannot be used during the development process (it must be packaged every time it is started, and it cannot be received ). It is suspected that the Code related to its editor mode is implemented using a pre-compiled macro, so it is not found in the released assembly, like this:
#if UNITY_EDITOR        Obj = Resources.LoadAssetAtPath(assetPath, typeof(T));        if (Obj == null)            Debug.LogError ("Asset not found at path: " + assetPath);        yield break;#else
Class assetcache: The resource cache mechanism is described in the assetloader section.
We have already mentioned:
  • The timestamp of resource items in assetcache, which is maintained by assetloader during resource loading requests;
  • Assetcache is mainly responsible for managing cache data, and the real resource loading action is still executed in assetloader;
Assetcache resource elimination is mainly called by various external modules based on the time they think they need, for example:
  • Scenemgr. clearcachesandfreememory ()
  • Loadingscreen. clearassets ()
  • Soundmgr. unloadsoundbundle ()
  • And so on.
In addition, the resource package will be automatically updated (started in login. onassetsversion () when the program starts, mainly through the updatemanager and downloader classes. OK. Let's summarize the Resource Management Mechanism of hearth stone:
  • Game resources are subcontracted by type. Each type of resource package can have multiple;
  • Use the cache mechanism during game operation;

Architectural Design appreciation of hearth stone (4): Asset Management

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.