Unity5 packaging mechanism, a resource to play AB and resource management scheme.
1. Hit AB:
1. Set platform
2. Clear All resources assetbundlename:
string[] Abnamearr = Assetdatabase.getallassetbundlenames ();
Assetdatabase.removeassetbundlename (abnamearr[i],true);
3. Create the AB directory
4. According to the configuration to find the resources needed to package, for each setabname
5. Call Buildpipeline.buildassetbundles (ab folder path, Buildassetbundleoptions.deterministicassetbundle,editoruserbuildsettins.activebuildtarget);
6. Complete, but here are some explanations for the 4th, because resources are dependent:
We can choose a simple and crude way to deal with dependent resources: make a few public dependency assetbundle, put some public resources into these public AB, specifically:
in 4th step, After we set the abname of a resource, we make a dependency on it:
string[] deps = assetdatabase.getdependencies (sourcepath);
if (Deps[i] needs to enter public dependency 1)
Assetimporter aIt = Assetimporter.getatpath (Deps[i]);
Ait.assetbundlename = Public dependent 1.assetBundle.
if (Deps[i] needs to enter public dependency 2)
...
Other dependencies do not have to continue to separate, that is, to hit the various resources AB, although it will increase the size of AB, but rely on resource management is much easier.
2. Resource management:
Load each public dependency into memory first, and always refer to this unload, other official resources load the corresponding AB can be loaded, no need to load dependencies. The
specific code is as follows: (Start the game load public dependencies, the same loading mode)
//Raw Resource ReferenceUnityengine.object m_resgameobj =NULL; //3 Asynchronous Load handles://resource Bundle load handle, reference www load AB resultsWWW M_loadhandle =NULL; //Resource Package requests, referencing Loadfromfileasync and Loadfrommemoryasync resultsAssetbundlecreaterequest m_request =NULL; //resource load request, referencing Loadassetasync resultsAssetbundlerequest m_assetrequest =NULL; //resource bundles, referencing various loaded resource bundlesAssetbundle M_assetbundle =NULL; //resource relative path with no suffix, as the base path, convenient for subsequent various splicing Public stringV_assetpath =NULL;//whether to use asynchronous Public BOOLV_useasync =true; //Load Priority level Public intV_priority =0;
/// <summary> ///loading pictures can only be used www mode/// </summary> /// <param name= "path" >Absolute Path</param> /// <returns></returns>IEnumerator Loadbundleimpimage (stringpath) {M_loadhandle=NewWWW (Pathhelper.getinstance (). F_addfilepro (path)); yield returnM_loadhandle; if(M_loadhandle! =NULL&&string. IsNullOrEmpty (m_loadhandle.error) && m_loadhandle.assetbundle! =NULL) {M_assetbundle=M_loadhandle.assetbundle; string[] Assets =M_assetbundle.getallassetnames (); M_resgameobj= M_assetbundle.loadasset (assets[0]); } Else if(M_loadhandle! =NULL&&string. IsNullOrEmpty (m_loadhandle.error) && m_loadhandle.texture! =NULL) {M_resgameobj=m_loadhandle.texture; } if(M_loadhandle! =NULL) M_loadhandle.dispose (); M_loadhandle=NULL; } /// <summary> ///Load Resource Imp/// </summary> /// <returns></returns>IEnumerator Loadresimp () {resourcerequest request=Resources.loadasync (V_assetpath); yield returnrequest; if(Request! =NULL&& Request.asset! =NULL) {M_resgameobj=Request.asset; } m_assetbundle=NULL; }/// <summary> ///Load Bundles/// </summary> /// <param name= "path" >Absolute Path</param> /// <returns></returns>IEnumerator Loadbundleimp (stringpath) {M_request=Assetbundle.loadfromfileasync (path); yield returnm_request; if(M_request! =NULL&& M_request.assetbundle! =NULL) {M_assetbundle=M_request.assetbundle; string[] Assets =M_assetbundle.getallassetnames (); M_assetrequest= M_assetbundle.loadassetasync (assets[0]); yield returnm_assetrequest; M_resgameobj=M_assetrequest.asset; F_sendfinishevent (); M_request=NULL; M_assetrequest=NULL; } }
//Loading Portal Public voidStartloadasset () {stringAbsolutepath =Getpersistentassetpath (); if(v_useasync) {stringAbpath =string. Empty; if(Pathhelper.getinstance (). F_checkfileexists (Absolutepath)) {Abpath=Absolutepath; } if(Abpath.endswith (pathhelper.abfile)) {assetmanager.getinstance (). Startcoroutine (Loadbundleimp (Abpath)); } Else if(Abpath.endswith (". jpg") || Abpath.endswith (". PNG") {assetmanager.getinstance ()). Startcoroutine (Loadbundleimpimage (Abpath)); } Else//load the file in the installation package, or load the resource using the editor{assetmanager.getinstance (). Startcoroutine (Loadresimp ()); } } Else { //Synchronous Loading Try { if(M_resgameobj = =NULL) {M_assetbundle=NULL; if(Assetmanager.getinstance (). F_isloadbyab () &&Absolutepath.endswith (Pathhelper.abfile)) {M_assetbundle=Assetbundle.loadfromfile (Absolutepath); } if(M_assetbundle! =NULL) { string[] Assets =M_assetbundle.getallassetnames (); M_resgameobj= M_assetbundle.loadasset (assets[0]); } Else{m_resgameobj=resources.load (V_assetpath); } } } Catch { } } }
A scheme of resource-playing AB and resource management under the Unity5 packing mechanism