Unity3d hot update book-download the only one

Source: Internet
Author: User

Download is so common in this era that everyone has a deep understanding of what download is.

This article only shares and introduces the downloaded code.

 

First, the download system has several missions.

First, keep the Client Version library up-to-date.

Second, the download should be able to compare and download at least

Third, try to be faster.

 

In fact, we did not write a download system, because there are actually few lines of code. Here we will introduce

Download and save the cache over HTTP. the first package is read from streamingassets, and resources are loaded. Three modules are available together.

Source code location https://github.com/lightszero/easydown

Keep up to date

To solve the first and second problems, we should try to scatter the downloaded resources.

In this way, files can be downloaded in the file unit, and existing local files will not be downloaded.

To achieve this goal, we have to determine whether to update a file before downloading it, because traffic is wasted after downloading.

You need to know whether a file should be updated. A simple method is to provide a simple feature information for each file, first download the feature information, and then decide whether to download the entire file.

Take the feature information of a large file, which is the work of the digest algorithm. Hash, CRC32, MD5, and sha1. Here we select the best sha1 Algorithm for the feature information of our file.

First, we need to generate feature information for the files we intend to download. You can download these test files and tool source code from GitHub.

Click "check" to display the above information and tell you what has happened to the version and your previous version.

Do not delete the generated allver.ver.txt file, which records the total version. Each check is based on the previous version.

Click Generate version to update all version files.

 

Here we add a group design.

The version generation tool uses the folder in its own path as the group

The unit of client download is group, which facilitates processing.

For example, I can put high-resolution resources and low-resolution resources in two groups respectively.

You can switch resources as long as the client simply switches the group name.

 

For example, two groups are generated. Each group has its own ver file.

 

Let's look at the ver file.

File name. The strange thing in the middle is the sha1 feature string. Even if the file only changes one byte, the generated feature string will be different. The last step is the file size. In this way, we only need to obtain the ver file and know the size of the file before downloading it.

In this way, we have a mechanism to keep the file version up-to-date.

You only need to download the ver file first, and then determine whether the files in each group have changed and how many changes there are before downloading them.

Put these test files on an HTTP server, and then proceed.

Client download

How to download the client? View the code description directly

Initialize the resource management system with the first line of code

The first example is to download the server address, which corresponds to the HTTP address of the allver.ver.txt File

The second parameter is a callback function. Because it takes time for the resource system to download the ver file from the network during initialization, the callback function is used to notify you.

The third parameter is a list <string> to notify the resource manager of the groups to be checked.

You may wonder, how can there be groups that do not check? For example, if our game is to be used on several platforms and some resources are not commonly used, we place them on different platforms so that we will not check resources on other platforms on one platform.

Note that resmgr must be updated all the time, because neither download nor load is completed immediately, resmgr must be monitored all the time.

Using macros, you can get a special string for each platform and mix them with the group name. Then, you can automatically select a platform-specific group to download it.

The fourth default parameter is the number of concurrent downloads. The default value is 1. to make full use of the download bandwidth, you can change it to 2. Changing more concurrency does not make much sense, the effect of 3 concurrency and 2 concurrency is not significantly different.

Taskstate is a simple statistical tool provided by resource manager. It is used to obtain several tasks that are being executed and completed.

Getneeddownloadres is the resource to be downloaded.

Loop, download, and download each resource can provide an independent notification callback. However, here we demonstrate the usage of batch download, which is not handled separately.

Finally, the waitfortaskfinish function will be called back to us until all tasks are completed,

This completes the entire download process.

Download progress

For the download progress, we can use taskstate to calculate the number of simple files, or we can calculate the length of each file in the downlist.

Demonstrate a simple

Note that resmgr must be updated all the time, because neither download nor load is completed immediately, resmgr must be monitored all the time.

Load and use

Whether the file is the latest locally or just downloaded, whether it is the first package resource or the resource to be updated, we load the file in the same way.

Obtain the corresponding group from verlocal and local version management of resmgr, retrieve the file from the group, and then call the beginloadxxx method of file.

We provide four load interfaces. In fact, one bytes is enough, and all other resources are from bytes. We have discussed this in the loading section.

However, considering the common usage, we encapsulate the string texture2d assetbundle.

The following other resources must be loaded, and multiple files need to be combined before they can be loaded. We will put these files in the loading article for research.

Now you have the most important update module for hot update, and it is completely for free.

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.