A brief analysis of Unity3d asset bundle format

Source: Internet
Author: User

Welcome to Unity Learning, unity training, Unity Enterprise training and education zone, there are many u3d resources, u3d training videos, u3d tutorials, U3d Frequently asked questions, U3d Project source code, we are committed to creating the industry Unity3d training, learning the first brand.

The format of the Unity3d Asset bundle is not disclosed. But in order to make a better difference update, we still want to know its packaging format. This makes it possible to make a special diff-diff merge tool that is much better than doing binary differences directly. Because the data within the asset bundle can be divided into independent units, only the change of the unit to make the difference between.

The information that can be found online is not officially given, the most popular is an open source tool called disunity. It is written in Java, with only the source code, not the format specification (which is more important than the code). By reading disunity's code, I sort out the following records:

Asset bundles are divided into compression mode and non-compression mode. The compression mode only uses the open-source Lzma library to do a whole compression of the entire uncompressed package. The header of the compressed data has 13 bytes, the first 5 bytes are the props that the Lzma decompression API needs to wear, and the next 4 bytes are the uncompressed database length. The last 4 bytes ignore it.

After extracting the compressed data, it is not different from the non-compression mode, only the non-compressed format is discussed below:

The file header of the assert bundle is serialized from such a data structure.

struct Assetbundlefilehead {
struct Levelinfo {
unsigned int packsize;
unsigned int uncompressedsize;
};

String FileID;
unsigned int Version;
String mainversion;
String buildversion;
size_t Minimumstreamedbytes;
size_t Headersize;
size_t numberoflevelstodownloadbeforestreaming;
size_t Levelcount;
Levelinfo levellist[];
size_t Completefilesize;
size_t Fileinfoheadersize;
BOOL compressed;
};
String is a straight-to-the-end strings, sequential serialization; size_t is a 4-byte number of big-endian; bool is a single byte; vector is the structure that follows the arrangement.

Depending on the Unity version, the assert bundle format is not exactly the same. Version indicates the format of the bundle, using Version = 3 from Unity 3.5 to version 4.x, only this version is discussed below. The headersize should exactly equal the data length of the header of the above file.

An assert bundle is packaged with multiple asset files, and these asset are packaged in the next order. Serialization into such a structure:

struct Assetfileheader {
struct Assetfileinfo {
String name;
size_t offset;
size_t length;
};
size_t FileCount;
Assetfileinfo file[];
};
This allows us to break down multiple Asset that are packaged together (in most cases, only one). Offset represents the offset after the headersize is removed. We can use Headersize plus that part of offset to get this part of the file offset relative to the entire bundle.

For each asset, there is its own data header. Data headers In addition to the basic data header structure Assetheader, there are an additional three parts. The disunity called them Typetree ObjectPath and Assetref. Note: Here format varies with different unity3d versions, we only care about the current version of the format, where format is 9 (other versions of the format, on the size side of the issue, etc.).

struct Assetheader {
size_t Typetreesize;
size_t FileSize;
unsigned int Format;
size_t Dataoffset;
size_t Unknown;
Unity has made simple, brutal serialization of Asset data. The entire serialization process is for the data structure of each object. Typetree is a description of the data structure itself, which can be deserialized out of each object.

The Assetheader is followed by Typetree. However, this typetree is optional for asset bundles, because information about data structures can be placed in the engine beforehand (most engines only support intrinsic data types). When publishing to a mobile device, Typetree is not packaged into the asset bundle.

Each asset object has a class ID that can be found in typetree to deserialize. The correspondence between the class ID and the specific type can be found in the official documentation of the Unity3d. But if we just want to compare the differences at the object level (rather than the specific attributes in the object), then we don't need to unravel the details of the specific object, which is not to be cared for. So it doesn't start here (it's interesting to read the disunity code, the format is not complicated).

The typetreesize in Assetheader refers to the size of the typetree part. Next is the description data for each assetobject.

struct Objectheader {
struct Objectinfo {
int Pathid;
int offset;
int length;
BYTE classid[8];
};
int objectcount;
Objectinfo object[];
};

Here, all int is a 4-byte integer encoded in the small end (different from the big-endian encoding used in the external file format). In Unity3d, each object has a unique string path, but in the asset bundle it does not directly save the string, but rather a hash integer, or it can be considered as the index number of the object. The real object is placed at the back of the data header, offset to the place.

The offset here is relative to the current asset block. If you want to get the correct position relative to the entire file, it should be the file's Headersize + asset offset + Asset's Dataoffset + object offset here.

After the Objectheader is the Assetref table, which records the reference relationship of Asset. Used to indicate a reference to an external asset within the bundle asset. The assetreftable structure is as follows:

struct Assettable {
struct Assetref {
BYTE guid[8];
int type;
String FilePath;
String Assetpath;
};
int Count;
BYTE Unknown;
Vector Refs;

For more highlights, please click http://www.gopedu.com/


A brief analysis of Unity3d asset bundle format

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.