Strength Package: Unity Pack Assetbundle (ii)

Source: Internet
Author: User

→ Previously summary: Unity's most basic assetbundle packaging method.

The second way of packing

The Buildassetbundles API provided by Unity also has an overloaded form, see below ↓↓

public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);

This overloaded function has one more parameter, this parameter is a assetbundlebuild array, below we say Assetbundlebuild is where doer ↓↓

Assetbundlebuild is a struct with four properties, which are described below ↓↓

Assetbundlename String type, the name of the Assetbundle package after packaging
Assetnames String array type, assetbundle the path of each file in the package relative to the project directory (multiple files can be in a assetbundle package)
Addressablenames String array type, equivalent to Assetnames alias, must be consistent with assetnames length
Assetbundlevariant String type, set Assetbundle variant, is actually the suffix name

In general, as long as you set the Assetbundlename property and the Assetnames property in the Assetbundlebuild struct, if you want to rename the file in the Assetbundle package, Just set the Addressablenames property, and the file you don't want to rename is given an empty string, so the default is to use Assetnames. If you still want to set a custom suffix name, try assetbundlevariant.

With assetbundlebuild such a struct array to do the argument, it means we can make the packaging process simpler. Instead of setting the Assetbundle name in the Inspector panel for each file that needs to be packaged, we can simply click on the file that needs to be packaged in the editor and then package it with one click. Of course, this also means that we can not arbitrarily control the name of each Assetbundle bag, the pros and cons of it.

Here is the way to talk about packaging, we can put a lot of files into a assetbundle package, you can also separate each file into a Assetbundle package, specifically how to play depends on the demand.

The following is the packaged code ↓↓

  1. [MenuItem ("Assetbundles/buil (All)")]
  2. private static void _packagebuddlesinone () {
  3. string _packagepath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "f:/", " " ");
  4. if (_packagepath.length <= 0 | |! Directory.Exists (_packagepath))
  5. return;
  6. buildmap[0].assetbundlename = "Mybnndle";
  7. //Package The selected objects together
  8. assertbundleassetbundlebuild[] Buildmap = new assetbundlebuild[1];
  9. gameobject[] Objs = selection.gameobjects; //Get all the objects currently selected
  10. string[] itemassets = new string[objs. Length];
  11. For (int i = 0; i < Objs. Length; i++) {
  12. itemassets [i] = Assetdatabase.getassetpath (Objs [i]); //Gets the relative path of the object under the project directory
  13. }
  14. Buildmap [0].assetnames = itemassets;
  15. Assetbundlemanifest manifest = Buildpipeline.buildassetbundles (_packagepath, Buildmap, Buildassetbundleoptions.none , BUILDTARGET.STANDALONEWINDOWS64);
  16. Assetdatabase.refresh (); //Refresh
  17. if (manifest = = null)
  18. Debug.logerror ("package assetbundles faild.");
  19. Else
  20. Debug.Log ("package assetbundles Success.");
  21. }
So all of your selected objects will be in a assetbundle package, suggesting that associated or similar files can be packaged like this.

We can also put each object into a different Assetbundle package, see the Code ↓↓

  1. [MenuItem ("Assetbundle/build (Selected)")]
  2. private static void _packagebuddleselected () {
  3. string _packagepath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "f:/", " " ");
  4. if (_packagepath.length <= 0 | |! Directory.Exists (_packagepath))
  5. return;
  6. assetbundlebuild[] Buildmap = new Assetbundlebuild[objs. Length];
  7. gameobject[] Objs = selection.gameobjects;
  8. For (int i = 0; i < Objs. Length; i++) {
  9. string[] Itemasset = new string[] {assetdatabase.getassetpath (objs[i])};
  10. Buildmap[i].assetbundlename = Objs[i].name;
  11. Buildmap [i].assetnames = Itemasset;
  12. }
  13. Assetbundlemanifest manifest = Buildpipeline.buildassetbundles (_packagepath, Buildmap, Buildassetbundleoptions.none , BUILDTARGET.STANDALONEWINDOWS64);
  14. Assetdatabase.refresh ();
  15. if (menifest = = null)
  16. Debug.logerror ("Error:package Failed");
  17. Else
  18. Debug.Log ("package Success.");
  19. }

[CPP]View PlainCopy

You can package the selected files by pressing the ↑ diagram.

More tricks, now you can create it yourself. It's here today, we'll see you next time.

Strength Package: Unity Pack Assetbundle (ii)

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.