About Unity Assets Research

1. Assets

I have studied it before, but it has been too long. Let me reorganize it, some of the articles of the moving masters.
I paid attention to Asset when I was researching AssetBundle. The official explanation is that AssetBundle is an archive file (that is, a bundled file type), and I personally understand AssetBundle as aArchive. Contains models, textures, prefabs, sounds, and even the entire scene, which can be loaded when the game is running. For
Assets, generally speaking, we have two layers of cognition: one layer comes from Unity's default project directory Assets, and the other layer comes from Unity's packaging system AssetBundles. Then we will normalize from these two aspects to understand what Unity's Assets are.

1. Assets directory

When we use Unity to create a project, have you paid attention to the default directories created under the project? The directory is as follows:
insert image description here

There are 4 directories in total, and their functions are:

  • Assets
    The actual resource directory of the Unity project. All original resources such as resources, codes, configurations, and libraries used in the project will only be recognized and processed by Unity if they are placed in this folder
  • The Library
    stores the resources processed by Unity. After most of the resources are imported into the Assets directory, they need to be converted into Unity-approved files through Unity. The converted files will be stored in this directory.
  • Packages
    is a new directory added after 2018, which is used to manage the packages components separated by Unity.
  • The ProjectSettings
    directory is used to store various project settings of Unity.
    insert image description hereWhen your project starts to have script files, an obj directory will be added for code compilation. So when you want to migrate a project or copy the project to others, you only need to back up the three directories of Assets, Packages and ProjectSettings. As for the Library, it will be checked and automatically converted when Unity is opened. Of course, if your project is very large and has a lot of resources, copying and transferring the library together will save a lot of conversion time during migration.

In addition, during actual project development, we may need to package for different platforms, such as PC packages and Android packages. Then copy two projects, one is set to PC platform, and the other is set to Android platform, the efficiency will be far greater than switching platforms when needed, because every time you switch to a different platform, Unity needs to reprocess all built-in resources , which is very time consuming.

2、AssetBundles

Putting aside all other understandings, from the perspective of English naming alone, this is a bundle, which is a format for archiving Assets. The concept tends to be that we use Zip or RAR formats to compress, encrypt, and archive resources or directories. , storage, etc. The difference is that compression formats such as Zip are for files, while AssetBundles are for Unity Assets. But if you change the concept again to understand, in fact, Zip operates and archives files that the operating system can recognize, while AssetBundles operates and archives files that Unity can recognize. In this way, the functions of the two are almost the same.

After laying the groundwork, let's take a look at how Unity officially defines it:

AssetBundles are archives that contain platform-specific Assets in non-code form.

Official document link:

There are several important information here. First, it is an archive file (that is, a bundled file type). Second, it has platform differences. Third, it does not contain code. Finally, it stores Unity Assets.

2. Unity assets

Asset actually plays the role of Unity assets in the entire Unity engine

The Windows operating system recognizes files through extensions. Register the suffix name and the corresponding processing software in the system, then when the file is double-clicked, the system will invoke the specified software to parse and process the file. If it is not registered in the system, or the suffix is ​​removed, the operating system will not recognize the file.

The same is true for Unity's Asset. We call an Asset an asset, which can be understood as a file that Unity can recognize. There are actually two types here, one is the format natively supported by Unity, such as: shader; the other is the format that needs to be processed by Unity, such as: FBX. For formats that need to be processed to support, Unity provides an importer (Importer).

It should be noted that all asset original files must be placed in the Assets directory of the Unity project, and then stored in the Library directory after being processed by Unity.

3. Identification and reference of Assets

As an asset file, there are many types of Assets. For example: shaders, texture maps, audio files, FBX files, various animations, configurations or Clip files, etc. We are usually used to various operations such as dragging, adding, modifying, renaming and even changing directories in Unity, but no matter how you operate in the Unity engine (excluding deletion), those related references are will not be lost. Why is this?

1、Assets和Objects

Before proceeding with the subsequent explanations, let’s unify the concepts, including if mentioned in the following chapters, they will follow the unified concepts here. Assets here and the following content refer to Unity's assets, which can mean a single file (or folder) seen in Unity's Projects window. And Objects here we refer to the object inherited from UnityEngine.Object, which is actually a serializable data used to describe an instance of a specific resource. It can represent any type supported by the Unity engine, such as: Mesh, Sprite, AudioClip or AnimationClip, etc.

Most Objects are supported by Unity, but there are two exceptions:

  • ScriptableObject
    is used to provide developers with the type of custom data format. Formats inherited from this class can be serialized and deserialized like Unity's native types, and can be manipulated from Unity's Inspector window.
  • MonoBehaviour
    provides a converter to MonoScript. MonoScript is an internal data type of Unity. It is not executable code, but it keeps a reference to a certain or special script under a specific namespace and assembly.

There is a one-to-many relationship between Assets and Objects. For example, a Prefab can be considered as an Asset, but this Prefab can contain many Objects. For example: if it is a UGUI Prefab, there may be many Text, Button, Image and other components.

2、File GUIDs和Local IDs

Anyone familiar with Unity knows that UnityEngine.Objects can refer to each other. There will be a problem here, these mutually referenced Objects may be in the same Asset, or they may be in different Assets. For example: an Image of UGUI needs to refer to a Sprite in a Sprite Atlas, which requires Unity to have a robust resource identifier and be able to stably handle the reference relationship of different resources. In addition, Unity must also consider that these resource identifiers should have nothing to do with the platform, so that developers cannot pay attention to the reference relationship of resources when switching platforms. After all, Unity itself is a cross-platform deployment engine.

Based on these specific requirements, Unity splits the serialization into two expressive parts. The first part is called File GUID, which identifies the location of the asset. This GUID is automatically generated by Unity based on an internal algorithm, and stored in the same directory as the original file, with the same name but a file with the suffix .meta.

Here are a few points to note:

  • Unity will automatically generate it when importing resources for the first time.
  • Move the position in the Unity panel, and Unity will automatically synchronize the .meta file for you.
  • With Unity open, delete .meta alone, and Unity can ensure that the regenerated GUID is the same as the existing one.
  • When Unity is closed, if you move or delete the .meta file, Unity cannot restore the original GUID, which means the reference will be lost.

After the asset file is determined, a Local IDs is needed to represent the unique identifier of the current Objects in the asset. File GUID ensures that the asset is unique in the entire Unity project, and Local ID ensures that Objects are unique in the asset, so that the corresponding reference can be quickly found through the combination of the two.

Unity also maintains a mapping table of asset GUID and path internally. Whenever a new resource enters the project, or some resources are deleted, or the resource path is adjusted, Unity's editor will automatically modify this mapping table so that asset locations are properly documented. So if the .meta file is lost or a different GUID is regenerated, Unity will lose the reference. The performance in the project is that a certain script displays "Missing", or the loss of some texture materials causes the scene to appear pink.

3. Resource location in Library

We mentioned earlier that formats not supported by Unity need to be converted by the importer. The reason why resources need to be converted and stored is also to make it easier for the next startup to not need to process resources again. Compared with importing resources each time, it is a huge time-consuming operation. Simply put, all conversion results will be stored in the Library/metadata/ directory, in a folder named after the first two digits of the File GUID. For example:
insert image description here
natively supported Assets resources will also have the same stored procedure, but there is no need to use the importer for conversion.

4、Instance ID

File GUID and Local ID have indeed been able to help Unity complete its planning in the editor mode, regardless of the platform, quickly locate and maintain resource locations and reference relationships. But if it is put into operation, there are still relatively large performance problems. That is to say, the runtime still needs a better-performing system. So Unity made another set of cache (remember the previous set of cache? It is used to record the path relationship between GUID and file). PersistentManager is used to convert File GUIDs and Local IDs into a simple, Session-only integer, which is the Instance ID. Instance ID is very simple, it is an incrementing integer, whenever a new object needs to be registered in the cache, simply increment it.

Guess you like

Origin blog.csdn.net/Brave_boy666/article/details/123140475