In a Unity project, the release package itself does not necessarily include all asset (translated as assets or components), others can be published separately as. Unity3d, and then executed from local/remote loading by the program, which is not covered in this article. Although Unity does not directly provide the. Unity3d export functionality, it can be learned through its manual and open menu items.
Look at Unity's Handbook on Assetbundle, with links to:
- Buildpipeline.buildassetbundle
- Building Assetbundles
"Note" the export. Unity3d format requires the Pro version, and the non-Pro version can open the menu item, but the export will prompt for an error:
We can open two exports using the ready-made script provided by untiy. Unity3d menu items can also be written using the API according to your needs. When the project becomes bigger and larger, manual export of Assetbundle will become more and more difficult, then you may need to develop the export function, automatically create Assetbundle.
Open menu item
Create a C # script named Exprotassetbundles in Unity and put it in the editor directory (this must be the directory in order to take effect in the compiler). Copy the following code into the Exprotassetbundles script (this code can be found in the building Assetbundles)
[CSharp]View Plaincopy
- <span style="FONT-SIZE:14PX;" > //C # Example
- //Builds an asset bundle from the selected objects in the Project view.
- //Once compiled go to "menu", "Assets" and select one of the choices
- //To build the Asset Bundle
- using Unityengine;
- using Unityeditor;
- public class Exportassetbundles {
- [MenuItem ("Assets/build assetbundle from Selection-track dependencies")]
- static void Exportresource () {
- //Bring up save panel
- string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- //Build The resource file from the active selection.
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets);
- Selection.objects = Selection;
- }
- }
- [MenuItem ("Assets/build Assetbundle from Selection-no Dependency tracking")]
- static void Exportresourcenotrack () {
- //Bring up save panel
- string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- //Build The resource file from the active selection.
- Buildpipeline.buildassetbundle (Selection.activeobject, selection.objects, Path);
- }
- }
- }</span>
At this point, you can see two new menu items under the Assets menus:
1. Build assetbundle from Selection-track dependenciesThis option will
Current ObjectPackaged into a asset bundle, and contains all dependencies.
2.
Build Assetbundle from SELECTION-NO dependency trackingAs opposed to the previous option, contains only the selected asset example: Create a cube, drag and drop to generate a preset body. Right click on the preset body, select
"Build assetbundle from Selection-track dependencies", you can see the Save window for. Unity3d. Create a directory named Assetbundles in your project, and save the selected preset as Cube.unity3d, and you can see that the window appears as follows: Now you can put cube.unity3d anywhere, or on your own server.
How to Modify properties when you create a component package
You can use Assetdatabase.importasset to force the import of components after calling Buildpipeline.buildassetbundle, and then use the Assetpostprocessor.onpreprocesstexture to set the desired properties.
The following example shows how to set a different texture map when building a component package.
[CSharp]View Plaincopy
- Builds an asset bundle from the selected objects in the Project view,
- and changes the texture format using an assetpostprocessor.
- Using Unityengine;
- Using Unityeditor;
- Public class Exportassetbundles {
- //Store current texture format for the textureprocessor.
- public static Textureimporterformat TextureFormat;
- [MenuItem ("Assets/build assetbundle from Selection-pvrtc_rgb2")]
- static void ExportResourceRGB2 () {
- TextureFormat = TEXTUREIMPORTERFORMAT.PVRTC_RGB2;
- Exportresource ();
- }
- [MenuItem ("Assets/build assetbundle from Selection-pvrtc_rgb4")]
- static void ExportResourceRGB4 () {
- TextureFormat = TEXTUREIMPORTERFORMAT.PVRTC_RGB4;
- Exportresource ();
- }
- static void Exportresource () {
- //Bring up save panel.
- string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
- if (path. Length! = 0) {
- //Build The resource file from the active selection.
- object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
- foreach (object Asset in selection) {
- string assetpath = Assetdatabase.getassetpath ((unityengine.object) asset);
- if (asset is texture2d) {
- //Force reimport thru Textureprocessor.
- Assetdatabase.importasset (Assetpath);
- }
- }
- Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets);
- Selection.objects = Selection;
- }
- }
- }
[CSharp]View Plaincopy
- Changes the texture format when building the Asset Bundle.
- Using Unityengine;
- Using Unityeditor;
- Public class Textureprocessor:assetpostprocessor
- {
- void Onpreprocesstexture () {
- Textureimporter importer = Assetimporter as textureimporter;
- Importer.textureformat = Exportassetbundles.textureformat;
- }
- }
Create a. Unity3d package that can be loaded remotely in unity.