"Reprint" Unity reasonably arranges incremental updates (hot update)

Source: Internet
Author: User
Tags shallow copy

Original address: Because I see that the website sent this post is very large may be stolen paste, I temporarily do not post address. Avoid harming the original author

The original write a little messy, I personally modified to tidy up the next.

----------------------------------------------------------------------------------------------------

Tools

There are three sources of resources in unity: one is Unity's automatic packaging resources, one is resource, and the other is assetbundle.

    • Unity Auto-packaging resources refers to resources that are used directly in the unity scene to be automatically packaged into the game as the scene is loaded and automatically loaded by unity when the scene loads. These resources are placed in the assets folder of the Unity Engineering Directory, and the program does not need to care about their packaging and loading, which means that the resources are loaded statically. But in the actual game development we generally will dynamically create gameobject, resources are dynamically loaded, so this kind of resources is not much.
    • Resources refers to a resource folder under the assets directory of the Unity project, where all resources placed under this folder, whether or not used by the scene, will be packaged in the game. And can be dynamically loaded through the Resources.load method. This is usually the development is commonly used in the load of resources, but the disadvantage is that the resources are packaged directly into the game package, can not do incremental updates.
    • Assetbundle resources means that we can package resources into multiple independent assetbundle through an editor script. These assetbundle and game packs are separated and can be loaded via the WWW class. The use of Assetbundle is very flexible: can be used to do subcontracting, for example, most of the page resources are downloaded with the process of the game, or some of the resources are too large, the channel requires the release of the package is limited to 100M, it can only start to play the content of an incremental package, When the player is playing, it is downloaded through the Internet. Assetbundle can also be used to do the automatic incremental updates we discuss below.
Scheme

This is what unity offers, and it's up to you: How do you make a convenient resource management solution that can be easily developed and easily released? The full use of assetsbundle in the development process is inappropriate, as resources are often added and updated in development, and it is cumbersome to generate assetsbundle each time they are added or more . And what we're going to do is auto-update instead of sub-download, which means that the resources should be in the game pack when the game is released, so they shouldn't be loaded from Assetsbundle.

After analyzing the requirements, the solution comes out: the resources are still under resource, but they are also packaged in Assetbundle. All the places in the code where the resources are loaded are loaded by their own ResourceManager, and the Resourcemananger decides whether to call Resources.load to load the resources or to load them from Assetsbundle. In the development environment (EDITOR) These resources are clearly loaded directly from the resource, the release of the full installation package resources are also loaded from the resource, only when there is an incremental version, the game main program will go to the server to download the incremental assetbundle, Then load the resource from Assetbundle.

Realize

The first thing we need to consider in the implementation is the granularity of the assetbundle, that is, how many resources each assetbundle contains. The minimum granularity of an incremental package is asssetbundle, if a single assetbundle is too large, as long as there is a resource change in this assetbundle, you need to re-download the entire assetbundle, wasting traffic and the player's waiting time If a single assetbundle is too small, the extreme situation is one assetbundle per resource, although the update is minimized, but with additional overhead: Assetbundle itself is also of a size, and it takes time to find the loading assetbundle. We all go to the inside of the U disk, copy a 1G file than the copy 1000 1M files much faster. It is reasonable to follow the logic, for example, each role can have independent assetbundle, some common UI resources can hit a assetbundle inside, each scene independent UI resources can be played as independent assetbundle. This is also handy when preloading resources, where each scenario requires several bundles to load several bundles, and irrelevant resources are not loaded.

The next consideration is how to determine whether a resource is loaded from resources or Assetbundle. For this we need a configuration file resourcesinfo. This file is automatically generated with the packaging process. It contains the resource version number, the name of all packages, the hashcode of each package, and the names of the resources contained within each package. Hashcode can be obtained directly from unity-generated manifest (Assetbundlemanifest.getassetbundlehash) to check whether the contents of the package have changed. This resourceinfo generates one every time the assetbundle is packaged, and it is all copied to the server with the new bundle when the increment is published. At the same time in the Resources folder also saved a copy, with the full installation package released, which ensures that the new installation of the game player mobile phone also has a complete resource profile, recording the full package contains resources.

When the game starts, ask the server to check the version number first, and the version number on the front end is the resourcesinfo in the resources below. The server compared this version number to tell the front-end whether it needs to be updated. If the need to update, the front end to get the server side of the new Resourcesinfo, and then each bundle in the hashcode, the hashcode different bundles recorded, and then through the WWW class to download the changed bundles, Of course, if the server version of Resourcesinfo contains bundles that are not in the local resourceinfo, these bundles are new and need to be downloaded. After all the downloads are complete, the front end saves the new resourceinfo to the local storage, and all operations on the back end will be based on this resourceinfo and no longer be the resourceinfo under resources. The resourceinfo of resources can exit the historical stage, except in one case: the locally stored resourceinfo is considered deleted. Mobile-phone player cleanup app data will cause downloaded bundles and resourceinfo to be deleted. It doesn't matter, the front end because can't find external resourceinfo, also will use resources under the Resourceinfo and server comparison, the new bundle again download down.

Now it's clear where the resources are loaded: Resourcemananger reads Resourcesinfo, knows all the bundles in the game and the resources that each bundle contains, and then goes to the external store to find out if these bundles exist, if they exist, The resource that records this bundle should be loaded from the external assetbundle, if not, from the internal resources. In the development process (editor), because there is no external storage of bundles, resources are naturally loaded from the resource, to achieve our convenience for the purpose of development. This process implies that not all resources need to have bundlename to be packaged into Assetbundle, and that the resources in the game that do not require subsequent updates are not set bundlename, and they are not packaged for updates. Such resources ResourceManager in the resourceinfo is not found, go directly to the Resources folder read on the line.

Loading Assetbundle, we directly use the WWW class without WWW.LoadFromCacheOrDownload, because our resources have been downloaded to the external storage at the beginning of the game, do not download or cache. Note that the WWW class load is asynchronous, in the game we need to load the resources in the place to be aware of the pre-loading of resources to exist ResourceManager, otherwise, when the load must write asynchronous code. Most of the time we should pre-load all the resources when a scene is initialized, using the ResourceManager cache directly.

Resource Load Offload

Finally, simply say the loading and unloading of resources, this online also has a lot of articles introduced.
From what I understand, resources is a special assetbundle that is automatically packaged by default. Creating a assetbundle, whether from www or assetbundle.createfromfile, is actually creating a file memory image. There is no asset at this time. Assetbundle.loadasset and Resource.load really created the asset, and Instaniate copied the asset. Note that there are two kinds of replication, learn C + + know shallow copy and deep copy, there is a copy of the copy is true, there are some references. Why would you do that? Because some of the game resources are read-only, like stickers texture, so large and read-only, of course, there is no need to copy them completely. But a resource like gameobject. Its properties can be changed by script and must be copied one copy. So a resource from Assetbundle to the scene is instantiated, in fact, there are 3 blocks of memory is created, the release of 3 fast memory has different methods.

    • The file memory image is freed by Assetbundle.unload (false).
    • Instaniate out of Object memory is released through Object.destory.
    • Assetbundle.unload (True) will not only release the file memory image, but also release the assets created by Assetbundle.load. This method is unsafe unless you can guarantee that these assets are not referenced by object, otherwise there is a problem.
    • Resources.unloadasset and resources.unloadunusedassets can be used to release asset.

The following diagram is intuitive:

This is the old unity figure, Unity5 has changed assetbundle.load to Assetbundle.loadasset. This change makes it more clear that the load is asset this area of memory. When will you change the resource.load?

Precautions (Pits)
      • Resources.load method The resource path that is passed in must be a relative path starting from the lower level of the resources folder and cannot contain an extension, whereas the resource name passed in by the Assetbundle.loadasset method needs to be the full path starting from the assets file and include the extension. The path is case-insensitive and is recommended for all lowercase because the resource name returned by the Assetbundle.getallassetnames method is lowercase.
      • Dependencies are automatically handled when the Unity5 is packaged assetbundle, but not when loaded at run time, and the program needs to handle it itself, loading the dependent packages first.
      • Assetbundle.createfromfile cannot load compressed assetbundle, so we can only use WWW to load assetbundle asynchronously.
      • At present, I use the Unity5.0.2f1 Resources.load method in the mobile phone side is much slower than the original, if can not be cached every time the use of the call Resource.load now is not. Frequent calls can lead to significant performance overhead and are not known to be bugs.

"Reprint" Unity reasonably arranges incremental updates (hot update)

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.