Dependency Packaging
The role of dependency packaging is to avoid resource redundancy while increasing the flexibility of resource loading and unloading, and its importance is self-evident. In the 4.x version of the Assetbundle packaging system, a pair of The Buildpipeline.pushassetdependencies and Buildpipeline.popassetdependencies interfaces, which can be understood in the official documentation: HTTP// Docs.unity3d.com/scriptreference/buildpipeline.pushassetdependencies.html
It is simple to think that pushassetdependencies is to put resources into the stack, popassetdependencies is to let the resources out of the stack, each dozen a package, the engine will check the current stack of all the dependencies, to see if the same resources are already in the stack. If so, establish a dependency relationship with the assetbundle associated with it.
Buildpipeline.pushassetdependencies (): Rely on the resource into the stack;
Buildpipeline.popassetdependencies (): Dependency resource out of stack.
Look directly at the code, below to package the sample code, PREFAB1 and PREFAB2 shared map resource Tex1, packaging Tex1 separately packaged, and PREFAB1 and PREFAB2 corresponding Assetbundle package does not actually contain TEX1 resources, Instead, record a reference to the TEX1 resource:
usingUnityeditor;usingUnityengine;usingSystem.IO;usingSystem.Collections;usingSystem.Collections.Generic; Public classPushandpop {[MenuItem ("Test/buildassetbundle")] Static voidExecute () {stringSavepath ="c:\\"; Buildassetbundleoptions Buildop= Buildassetbundleoptions.collectdependencies |buildassetbundleoptions.completeassets|Buildassetbundleoptions.deterministicassetbundle; Buildpipeline.pushassetdependencies (); //shared Resources Tex1.tgaObject Sharedasset = Assetdatabase.loadmainassetatpath ("Assets/resources/test/tex1.png"); Buildpipeline.buildassetbundle (Sharedasset,NULL, Savepath + Sharedasset.name +". Assetbundle", Buildop, buildtarget.standalonewindows); //PREFAB1, quoting Tex1.png.buildpipeline.pushassetdependencies (); Object P1asset= Assetdatabase.loadmainassetatpath ("Assets/resources/test/p1.prefab"); Buildpipeline.buildassetbundle (P1asset,NULL, Savepath + P1asset.name +". Assetbundle", Buildop, buildtarget.standalonewindows); Buildpipeline.popassetdependencies (); //PREFAB2, quoting Tex1.png.buildpipeline.pushassetdependencies (); Object P2asset= Assetdatabase.loadmainassetatpath ("Assets/resources/test/p2.prefab"); Buildpipeline.buildassetbundle (P2asset,NULL, Savepath + P2asset.name +". Assetbundle", Buildop, buildtarget.standalonewindows); Buildpipeline.popassetdependencies (); Buildpipeline.popassetdependencies (); Editorutility.displaydialog ("","completed","OK"); Assetdatabase.refresh (); }}
As you can see, both push and Pos are paired, a Push/pop pair is equivalent to a layer, the layer can be nested, and the inner layer can depend on the outer resources. That is, when a resource in the inner layer is packaged, if a resource referenced by it has already been loaded in the outer layer, the resource bundle in the inner layer will contain a reference to that resource instead of the resource itself. The Push/pop actually maintains a dependent stack.
So, when loading a dependent resource bundle, it is important to note that the dependent resources are loaded first, and then other resources are loaded, and this order needs to be ensured. The following code shows how to use a dependent resource bundle:
usingUnityengine;usingSystem.Collections; Public classNewbehaviourscript:monobehaviour {voidOngui () {//empty the local cache if(GUI. Button (NewRect (0f, 0f, 100f, 20f), Caching.spaceOccupied.ToString ())) {Caching.cleancache (); } if(GUI. Button (NewRect (0f, 30f, 100f, 20f),"Load Share Res") {startcoroutine (Load (@"File://c:\tex1.assetbundle",1)); } if(GUI. Button (NewRect (0f, 60f, 100f, 20f),"Load and Instantiate Prefab") {startcoroutine (Loadandinstantiate (@"File://c:\p1.assetbundle",1)); Startcoroutine (Loadandinstantiate (@"File://c:\p2.assetbundle",1)); } } //LoadingIEnumerator Load (stringUrlintversion) {www www=WWW.LoadFromCacheOrDownload (URL, version); yield returnwww; } //Load and InstantiateIEnumerator Loadandinstantiate (stringUrlintversion) {www www=WWW.LoadFromCacheOrDownload (URL, version); yield returnwww; if(!System.String.IsNullOrEmpty (Www.error)) {Debug.Log (www.error); } Else{Object main=Www.assetBundle.mainAsset; Gameobject.instantiate (main); } }}
Press the second button first, then press the third button to display the two prefab correctly, if the third step is done directly, the prefab will be missing the map.
In addition we can look at the size of the Assetbundle package:
If you do not hit the dependent package, two prefab are all playing the full package, the resulting packet size is:
If you hit a dependent package, the size of the resulting package is:
Unity Assetbundle Shared Resource Packaging/resource-dependent packaging