Assetbundle Packaging for custom shader

Source: Internet
Author: User

Http://docs.unity3d.com/Manual/managingassetdependencies.html

Managing Asset Dependencies

Any given asset in a bundle could depend on other assets. For example, the a model may incorporate materials which in turn make use of textures and shaders. It is possible to include all a asset ' s dependencies along with it on its bundle. However, several assets from different bundles-all depend on a common set of other assets (eg, several different model S of buildings may use the same brick texture). If A separate copy of a shared dependency is included in each bundle the has objects using it and then redundant instances O f The assets is created when the bundles is loaded. This would result in wasted memory.

To avoid such wastage, it's possible to separate GKFX dependencies out into a separate bundle and simply reference T Hem from any bundles with assets, that need them. First, the referencing feature needs to is enabled with a call tobuildpipeline.pushassetdependencies. Then, the bundle containing the referenced dependencies needs to be built. Next, another call to Pushassetdependencies should is made before building the bundles that reference the assets from the First bundle. Additional levels of dependency can is introduced using further calls to Pushassetdependencies. The levels of reference is stored on a stack, so it's possible to go back a level using the Corresponding buildpipe Line. Popassetdependencies function. The push and pop calls need to being balanced including the initial push that happens before building.

At runtime, you need to the load a bundle containing dependencies before any other bundles that references them. For example, would need to load a bundle of GKFX textures before loading a separate bundle of materials that Referen Ce those textures.

Asset IDs

If you anticipate needing to rebuild asset bundles that is part of a dependency chain then you should build them with the Buildassetbundleoptions.deterministicassetbundle option enabled. This guarantees, the internal ID values used to identify assets'll be, the same each time, the bundle is rebuilt.

When building the asset bundle with this method, the objects in it was assigned a a-bit hash code that's calculated Usin G The name of the asset bundle file, the GUID of the asset and the local ID of the the object in the asset. For this reason make sure to use the same file name when rebuilding. Also Note that has a lot of objects might cause hash collisions preventing Unity from building the asset bundle.

Shaders dependencies

Whenever shaders is directly referenced as parameters in Buildpipeline.buildassetbundle, or indirectly with the Optionbui Ldassetbundleoptions.collectdependencies The shader ' s code is included with the asset bundle(The shader specified here is that the built-in shader in the project with the code in the custom shader,unity will not be packaged). This could cause a problem if the buildassetbundle alone to create several asset bundles, since referenced shaders wil L be included in every generated bundle. There could is conflicts, i.e. when you mix different versions of a shader, so you'll have the to rebuild all your bundles a Fter modifying the shaders. The shader ' s code would also increase the size of bundles. To avoid these problems you can usebuildpipeline.pushassetdependencies to separate shaders in a single bundle, and that WI ll allow you to update the shader bundle only. As an example of what to achieve this workflow, you can create a prefab that includes references to the required shaders:

C
class ShadersList : MonoBehaviour {    public Shader[] list;}

Create an empty object, assign the script, add the shaders to the list and create the prefab, i.e. "shaderslist". Then you can create a exporter that generates all the bundles and updates the bundle of shaders:

C
Using Unityengine;Using Unityeditor;PublicClass Exporter:monobehaviour {[MenuItem ("Assets/export all asset bundles")]Staticvoid Export () {buildassetbundleoptions options = buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets | Buildassetbundleoptions.deterministicassetbundle; Buildpipeline.pushassetdependencies (); Buildpipeline.buildassetbundle (Assetdatabase.loadmainassetatpath ("Assets/shaderslist.prefab"),Null"Webplayer/shaderslist.unity3d", options); Buildpipeline.pushassetdependencies (); Buildpipeline.buildassetbundle (Assetdatabase.loadmainassetatpath ("Assets/scene1.prefab"),Null "Webplayer/scene1.unity3d", options); Buildpipeline.buildassetbundle (Assetdatabase.loadmainassetatpath ( "Assets/scene2.prefab"), null,  "Webplayer/scene2.unity3d", options); Buildpipeline.popassetdependencies (); Buildpipeline.popassetdependencies (); } [MenuItem ( "Assets/update shader bundle")] static void exportshaders () {buildassetbundleoptions options = buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets | Buildassetbundleoptions.deterministicassetbundle; Buildpipeline.pushassetdependencies (); Buildpipeline.buildassetbundle (Assetdatabase.loadmainassetatpath ( "assets/ Shaderslist.prefab "), null, " Webplayer/shaderslist.unity3d ", options); Buildpipeline.popassetdependencies (); }} 

Must load the shader bundle first. One drawback of this method is, the optionbuildassetbundleoptions.deterministicassetbundle can produce conflicts due t o colliding hashes when the amount of objects is too large. In this case the build would fail, and it won ' t is possible to update the shader bundle alone. In this case, you'll have a to remove, the option and rebuild all the asset bundles.

Assetbundle Packaging for custom shader

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.