Create a. Unity3d package that can be loaded remotely in unity.

Source: Internet
Author: User

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
  1. <span style="FONT-SIZE:14PX;" > //C # Example
  2. //Builds an asset bundle from the selected objects in the Project view.
  3. //Once compiled go to "menu", "Assets" and select one of the choices
  4. //To build the Asset Bundle
  5. using Unityengine;
  6. using Unityeditor;
  7. public class Exportassetbundles {
  8. [MenuItem ("Assets/build assetbundle from Selection-track dependencies")]
  9. static void Exportresource () {
  10. //Bring up save panel
  11. string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
  12. if (path. Length! = 0) {
  13. //Build The resource file from the active selection.
  14. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  15. Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets);
  16. Selection.objects = Selection;
  17. }
  18. }
  19. [MenuItem ("Assets/build Assetbundle from Selection-no Dependency tracking")]
  20. static void Exportresourcenotrack () {
  21. //Bring up save panel
  22. string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
  23. if (path. Length! = 0) {
  24. //Build The resource file from the active selection.
  25. Buildpipeline.buildassetbundle (Selection.activeobject, selection.objects, Path);
  26. }
  27. }
  28. }</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
  1. Builds an asset bundle from the selected objects in the Project view,
  2. and changes the texture format using an assetpostprocessor.
  3. Using Unityengine;
  4. Using Unityeditor;
  5. Public class Exportassetbundles {
  6. //Store current texture format for the textureprocessor.
  7. public static Textureimporterformat TextureFormat;
  8. [MenuItem ("Assets/build assetbundle from Selection-pvrtc_rgb2")]
  9. static void ExportResourceRGB2 () {
  10. TextureFormat = TEXTUREIMPORTERFORMAT.PVRTC_RGB2;
  11. Exportresource ();
  12. }
  13. [MenuItem ("Assets/build assetbundle from Selection-pvrtc_rgb4")]
  14. static void ExportResourceRGB4 () {
  15. TextureFormat = TEXTUREIMPORTERFORMAT.PVRTC_RGB4;
  16. Exportresource ();
  17. }
  18. static void Exportresource () {
  19. //Bring up save panel.
  20. string path = Editorutility.savefilepanel ("Save Resource", " ", "New Resource", "Unity3d");
  21. if (path. Length! = 0) {
  22. //Build The resource file from the active selection.
  23. object[] selection = selection.getfiltered (typeof (Object), selectionmode.deepassets);
  24. foreach (object Asset in selection) {
  25. string assetpath = Assetdatabase.getassetpath ((unityengine.object) asset);
  26. if (asset is texture2d) {
  27. //Force reimport thru Textureprocessor.
  28. Assetdatabase.importasset (Assetpath);
  29. }
  30. }
  31. Buildpipeline.buildassetbundle (Selection.activeobject, Selection, Path, buildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets);
  32. Selection.objects = Selection;
  33. }
  34. }
  35. }

[CSharp]View Plaincopy
  1. Changes the texture format when building the Asset Bundle.
  2. Using Unityengine;
  3. Using Unityeditor;
  4. Public class Textureprocessor:assetpostprocessor
  5. {
  6. void Onpreprocesstexture () {
  7. Textureimporter importer = Assetimporter as textureimporter;
  8. Importer.textureformat = Exportassetbundles.textureformat;
  9. }
  10. }

Create a. Unity3d package that can be loaded remotely in unity.

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.