Unity hot update uses AB package resource files

        Using the resource files in the AB package is actually calling Unity's API.

        First of all, in the first step, we need to load the AB package, and in the second step, we load the resources in the AB package.

        For the path, select Application.streamingAssetsPath, you can get the path location under different platforms

        LoadAsset has three overloads, by name, by type, and by generics. If you only use the name to load, it will be difficult to distinguish different types of resources with the same name. For example, there is a picture called "Cube" at this time, which will be confusing. Therefore, it is recommended to use the latter two types and generics to read.

Generic case:

 

Type occasion: (It should be noted that the value returned by reading type here is Object type, so a forced type conversion is required here, but because lua does not support generics, so lua needs C# to load objects When, we need to use type)

 

         The AB package cannot be loaded repeatedly: (it can be loaded repeatedly after being released, so it is necessary to make a cache record when loading)

 


         The above is the way to load the AB package synchronously. Next, let’s talk about asynchronous loading. When it comes to asynchronous loading, we will mention the coroutine:

        Then start the coroutine:

 


         Just mentioned that the AB package cannot be loaded repeatedly, but after you release it, you can load it, use the API, and if the Boolean value parameter filled in is true, it will be connected to the resources in the AB package that have been loaded in the scene Delete them together.

        There is also a single package to uninstall, and the function of the following parameters is the same as that of the previous API.

 


        If a shader is hung on a resource that has been entered into the AB package, the shader will be the same as the AB package name of the resource by default. At this time, if a package name is specified for it, then the two packages will be dependent relation.

        AB package dependency: A resource uses resources in other AB packages. At this time, if you only load your own AB package and create objects through it, resources will be lost. At this time, you need to load the dependent packages together to be normal.

        But in a game, it is difficult for us to remember the corresponding dependency packages one by one, so we need to use the main package to obtain dependencies.

 

        But this method has a disadvantage. If A in the package depends on 1 package, but B in the package depends on 2 packages, it cannot distinguish, and will get all the packages that a package depends on, so our resource management is also very important .

 

Guess you like

Origin blog.csdn.net/weixin_45081191/article/details/129062690