→ 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 ↓↓
- [MenuItem ("Assetbundles/buil (All)")]
- private static void _packagebuddlesinone () {
- string _packagepath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "f:/", " " ");
- if (_packagepath.length <= 0 | |! Directory.Exists (_packagepath))
- return;
- buildmap[0].assetbundlename = "Mybnndle";
- //Package The selected objects together
- assertbundleassetbundlebuild[] Buildmap = new assetbundlebuild[1];
- gameobject[] Objs = selection.gameobjects; //Get all the objects currently selected
- string[] itemassets = new string[objs. Length];
- For (int i = 0; i < Objs. Length; i++) {
- itemassets [i] = Assetdatabase.getassetpath (Objs [i]); //Gets the relative path of the object under the project directory
- }
- Buildmap [0].assetnames = itemassets;
- Assetbundlemanifest manifest = Buildpipeline.buildassetbundles (_packagepath, Buildmap, Buildassetbundleoptions.none , BUILDTARGET.STANDALONEWINDOWS64);
- Assetdatabase.refresh (); //Refresh
- if (manifest = = null)
- Debug.logerror ("package assetbundles faild.");
- Else
- Debug.Log ("package assetbundles Success.");
- }
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 ↓↓
- [MenuItem ("Assetbundle/build (Selected)")]
- private static void _packagebuddleselected () {
- string _packagepath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "f:/", " " ");
- if (_packagepath.length <= 0 | |! Directory.Exists (_packagepath))
- return;
- assetbundlebuild[] Buildmap = new Assetbundlebuild[objs. Length];
- gameobject[] Objs = selection.gameobjects;
- For (int i = 0; i < Objs. Length; i++) {
- string[] Itemasset = new string[] {assetdatabase.getassetpath (objs[i])};
- Buildmap[i].assetbundlename = Objs[i].name;
- Buildmap [i].assetnames = Itemasset;
- }
- Assetbundlemanifest manifest = Buildpipeline.buildassetbundles (_packagepath, Buildmap, Buildassetbundleoptions.none , BUILDTARGET.STANDALONEWINDOWS64);
- Assetdatabase.refresh ();
- if (menifest = = null)
- Debug.logerror ("Error:package Failed");
- Else
- Debug.Log ("package Success.");
- }
[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)