thieumao

ExportAssetBundles - script tạo assetbundle trên Unity 4

Aug 20th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;
  4. public class ExportAssetBundles {
  5.  
  6.     // Store current texture format for the TextureProcessor.
  7.     public static TextureImporterFormat textureFormat;
  8.  
  9.     [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")]
  10.     static void ExportResourceRGB2()
  11.     {
  12.         textureFormat = TextureImporterFormat.PVRTC_RGB2;
  13.         ExportResource();
  14.     }
  15.  
  16.     [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
  17.     static void ExportResourceRGB4()
  18.     {
  19.         textureFormat = TextureImporterFormat.PVRTC_RGB4;
  20.         ExportResource();
  21.     }
  22.  
  23.     static void ExportResource()
  24.     {
  25.         // Bring up save panel.
  26.         string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");
  27.  
  28.         if (path.Length != 0)
  29.         {
  30.             // Build the resource file from the active selection.
  31.             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  32.  
  33.             foreach (object asset in selection)
  34.             {
  35.                 string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object)asset);
  36.                 if (asset is Texture2D)
  37.                 {
  38.                     // Force reimport thru TextureProcessor.
  39.                     AssetDatabase.ImportAsset(assetPath);
  40.                 }
  41.             }
  42.  
  43.            BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
  44.             // for Android
  45.             BuildPipeline.BuildAssetBundle(Selection.activeObject,
  46.                                            selection, path + ".android.unity3d",
  47.                                            BuildAssetBundleOptions.CollectDependencies |
  48.                                            BuildAssetBundleOptions.CompleteAssets,
  49.                                            BuildTarget.Android);
  50.             Selection.objects = selection;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment