unity5.x Version Assetbundle Packaging research

Source: Internet
Author: User

Unity5 's assetbundle packaging mechanism is not the same as the previous version. Simply put, as long as you set a assetbundlename for the resources you want to package, unity itself will package the names of those resources, if one resource relies on another. Unity itself handles dependencies, and assetbundlemanifest files preserve the dependencies of those resources.
such as a UI panel. Prefab, relies on an atlas, a font file
Do a test:
Set Assetbundlename to UI panel 3.prefab only.

Out of the bag to see, don't look only 371KB, that is because I take the panel is not very complex, dependent on the Atlas, font, itself is not very large.
If you don't deal with dependencies in a project, you'll have a few m.

If there are other UI panels, set Assetbundlename, and hit packets, it's all that big.

Dependent files show that resources are not dependent, because each of the panels is individually packaged with a copy of the Atlas resource, font resources. Obviously this is not advisable.
For the same type of UI panel, these atlas and font files, we use the same copy, as long as the packaging of a copy, everyone sharing.
Then to the Atlas resources, font files are set Assetbundlename, and then packaged, you can see the smaller.

Looking at the. manifest file, there is a dependency.

Project, the resource is spicy, can not be in the editor a one to the resources to set assetbundlename it, it will hurt the egg, right.
On the Code

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122
Using unityengine;using system.collections;using unityeditor;using system.io; ///<summary>/// Package the resources under resource into a. Unity3d to streamingassets directory///</summary>public class Builder:editor{public static string SourcePath = Application.datapath + "/resources"; const string assetbundlesoutputpath = "Assets/streamingassets";  [MenuItem ("Tools/assetbundle/build")]public static void Buildassetbundle () {clearassetbundlesname ();  pack ( SourcePath);  string OutputPath = Path.Combine (Assetbundlesoutputpath,platform.getplatformfolder ( Editoruserbuildsettings.activebuildtarget)); if (! Directory.Exists (OutputPath)) {directory.createdirectory (OutputPath);} The  //is packaged Buildpipeline.buildassetbundles (outputpath,0, based on the platform activated within the buildsetting Editoruserbuildsettings.activebuildtarget);  assetdatabase.refresh ();  debug.log ("Packaged complete"); }  <summary>///clears previously set assetbundlename, avoids unnecessary resources and packages///previously said, as long as the assetbundlename is set, will be packaged, regardless of the directory///< /summary>static VoiD clearassetbundlesname () {int length = Assetdatabase.getallassetbundlenames (). Length;debug.  Log (length); string[] Oldassetbundlenames = new String[length];for (int i = 0; i < length; i++) {oldassetbundlenames[i] = Assetdatabase.getallassetbundlenames () [i];}  for (int j = 0; J < Oldassetbundlenames.length; J + +) {Assetdatabase.removeassetbundlename (oldassetbundlenames[ J],true);} Length = Assetdatabase.getallassetbundlenames (). Length;debug. Log (length);}  static void Pack (string source) {DirectoryInfo folder = new DirectoryInfo (source); filesysteminfo[] Files = folder. Getfilesysteminfos (); int length = files. length;for (int i = 0; i < length; i++) {if (Files[i] is DirectoryInfo) {Pack (files[i]. FullName);} Else{if (!files[i]. Name.endswith (". Meta")) {file (Files[i]. FullName);}}}  static void file (string source) {String _source = Replace (source); string _assetpath = "Assets" + _source. Substring (Application.dataPath.Length); string _assetpath2 = _source. Substring (APPLICATION.DAtapath.length + 1);//debug.log (_assetpath);  //set the resource in code assetbundlenameassetimporter Assetimporter = Assetimporter.getatpath (_assetpath); string assetname = _assetpath2.substring (_assetpath2.indexof ("/") + 1); Assetname = Assetname.replace (Path.getextension (Assetname), ". Unity3d");//debug.log (Assetname); Assetimporter.assetbundlename = Assetname;}  static string Replace (string s) {return s.replace ("\ \", "/");}}  public class Platform {public static string Getplatformfolder (Buildtarget target) {switch (target) {case BuildTarget.Android:return "Android", Case BuildTarget.iOS:return "IOS", Case BuildTarget.WebPlayer:return "Webplayer Case BuildTarget.StandaloneWindows:case BuildTarget.StandaloneWindows64:return "Windows"; BuildTarget.StandaloneOSXIntel:case BuildTarget.StandaloneOSXIntel64:case buildtarget.standaloneosxuniversal: Return "OSX";d Efault:return Null;}}}

With this. manifest file that contains the dependencies of all resources, when loading a resource, it is necessary to load all resources dependent on the resource, then load the resource, and then use it. Load this piece and finish it next time.
The code is here, and I'm not uploading the project.
Thanks for a prayer!

unity5.x Version Assetbundle Packaging research

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.