BuildPipeline 编译管线

Description 描述

让你可以以编程方式生成程序或资源包, 以便于从网络中加载。

静态变量说明
isBuildingPlayer当前是否正在编译程序?
静态函数说明
BuildAssetBundles在编辑模式编译所有指定的资源。Unity5.x 会多一个依赖关系列表

||BuildPlayer|编译一个项目。||GetCRCForAssetBundle|提取给定资源包的CRC校验。||GetHashForAssetBundle|提取指定资源包的哈希值。|


BuildPipeline.BuildAssetBundles 编译资源包

public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);

参数说明
outputPath资源包的输出路径。
assetBundleOptions资源包编译选项。
targetPlatform目标编译平台

Description 描述

在编辑编译所有指定的资源。

在Unity5.0,在编辑器中让你在包含被命名的资源包中标记资源。这个函数实际上是在编辑器中编译你指定的资源,如果编译成功返回true,否则返回false。此外,错误消息显示说明最常见的编译失败,如不正确的目标文件夹路径。

outputPath是资源要保存的路径,资源将被编译保存到那里(例如,Assets/MyBundleFolder)。该文件夹不会自动创建,如果不存在该函数会简单返回错误。

可选项assetBundleOptions是修改资源包的构建方式,targetPlatform是部署资源要使用的目标平台(如果pc,手机等)。注意,编译为pc平台的资源包,不兼容手机等其他平台,你需要为不同的平台编译不同版本的资源包。

  1. using UnityEditor;
  2. public class SimpleBundleBuilder
  3. {
  4. [MenuItem("Simple Bundles/Build")]
  5. [MenuItem("Test/Build Asset Bundles")]
  6. static void BuildABs() {
  7. // Put the bundles in a folder called "ABs" within the
  8. // Assets folder.
  9. BuildPipeline.BuildAssetBundles("Assets/ABs");
  10. }
  11. }

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

参数说明
outputPath资源包的输出路径。
assetBundleOptions资源包编译选项。
targetPlatform Target目标编译平台。
builds资源包编译地图。
  1. using UnityEditor;
  2. public class SimpleBundleBuilder
  3. {
  4. [MenuItem("Test/Build Asset Bundles")]
  5. static void BuildABs() {
  6. // Create the array of bundle build details.
  7. AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
  8. buildMap[0].assetBundleName = "enemybundle";
  9. string[] enemyAssets = new string[2];
  10. enemyAssets[0] = "char_enemy_alienShip";
  11. enemyAssets[1] = "char_enemy_alienShip-damaged";
  12. buildMap[0].assetNames = enemyAssets;
  13. buildMap[1].assetBundleName = "herobundle";
  14. string[] heroAssets = new string[1];
  15. heroAssets[0] = "char_hero_beanMan";
  16. buildMap[1].assetNames = heroAssets;
  17. BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap);
  18. }
  19. }

分开打包

  1. [MenuItem("Custom Editor/Create AssetBunldes Main")]
  2. static void CreateAssetBunldesMain ()
  3. {
  4. //获取在Project视图中选择的所有游戏对象
  5. Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
  6. //遍历所有的游戏对象
  7. foreach (Object obj in SelectedAsset)
  8. {
  9. string sourcePath = AssetDatabase.GetAssetPath (obj);
  10. //本地测试:建议最后将Assetbundle放在StreamingAssets文件夹下,如果没有就创建一个,因为移动平台下只能读取这个路径
  11. //StreamingAssets是只读路径,不能写入
  12. //服务器下载:就不需要放在这里,服务器上客户端用www类进行下载。
  13. string targetPath = Application.dataPath + "/StreamingAssets/" + obj.name + ".assetbundle";
  14. if (BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)) {
  15. Debug.Log(obj.name +"资源打包成功");
  16. }
  17. else
  18. {
  19. Debug.Log(obj.name +"资源打包失败");
  20. }
  21. }
  22. //刷新编辑器
  23. AssetDatabase.Refresh ();
  24. }

最核心的方法其实就它:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)

参数1:它只能放一个对象,因为我们这里是分别打包,所以通过循环将每个对象分别放在了这里。

参数2:可以放入一个数组对象。

默认情况下打的包只能在电脑上用,如果要在手机上用就要添加一个参数。

Android上:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)

IOS上:

BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)

另外,电脑上和手机上打出来的Assetbundle不能混用,不同平台只能用自己的。

将所有对象打包在一个Assetbundle中

  1. [MenuItem("Custom Editor/Create AssetBunldes ALL")]
  2. static void CreateAssetBunldesALL ()
  3. {
  4. Caching.CleanCache ();
  5. string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
  6. Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
  7. foreach (Object obj in SelectedAsset)
  8. {
  9. Debug.Log ("Create AssetBunldes name :" + obj);
  10. }
  11. //这里注意第二个参数就行
  12. if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
  13. AssetDatabase.Refresh ();
  14. } else {
  15. }
  16. }

读取Assetbundle

Assetbundle是可以同时放在服务器或者本地的,无论放在哪里两种下载读取的方式是完全一样的。

  1. using UnityEngine;
  2. using System.Collections;
  3. public class RunScript : MonoBehaviour
  4. {
  5. //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
  6. public static readonly string PathURL =
  7. #if UNITY_ANDROID
  8. "jar:file://" + Application.dataPath + "!/assets/";
  9. #elif UNITY_IPHONE
  10. Application.dataPath + "/Raw/";
  11. #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
  12. "file://" + Application.dataPath + "/StreamingAssets/";
  13. #else
  14. string.Empty;
  15. #endif
  16. void OnGUI()
  17. {
  18. if(GUILayout.Button("Main Assetbundle"))
  19. {
  20. StartCoroutine(LoadMainGameObject(PathURL + "Prefab0.assetbundle"));
  21. StartCoroutine(LoadMainGameObject(PathURL + "Prefab1.assetbundle"));
  22. }
  23. if(GUILayout.Button("ALL Assetbundle"))
  24. {
  25. StartCoroutine(LoadALLGameObject(PathURL + "ALL.assetbundle"));
  26. }
  27. }
  28. //读取一个资源
  29. private IEnumerator LoadMainGameObject(string path)
  30. {
  31. WWW bundle = new WWW(path);
  32. yield return bundle;
  33. //加载到游戏中
  34. yield return Instantiate(bundle.assetBundle.mainAsset);
  35. bundle.assetBundle.Unload(false);
  36. }
  37. //读取全部资源
  38. private IEnumerator LoadALLGameObject(string path)
  39. {
  40. WWW bundle = new WWW(path);
  41. yield return bundle;
  42. //通过Prefab的名称把他们都读取出来
  43. Object obj0 = bundle.assetBundle.Load("Prefab0");
  44. Object obj1 = bundle.assetBundle.Load("Prefab1");
  45. //加载到游戏中
  46. yield return Instantiate(obj0);
  47. yield return Instantiate(obj1);
  48. bundle.assetBundle.Unload(false);
  49. }
  50. }

BuildAssetBundleOptions

  1. None Build assetBundle without any special option.
  2. UncompressedAssetBundle Don't compress the data when creating the asset bundle.
  3. DisableWriteTypeTree Do not include type information within the AssetBundle.
  4. DeterministicAssetBundle Builds an asset bundle using a hash for the id of the object stored in the asset bundle.
  5. ForceRebuildAssetBundle Force rebuild the assetBundles.
  6. IgnoreTypeTreeChanges Ignore the type tree changes when doing the incremental build check.
  7. AppendHashToAssetBundleName Append the hash to the assetBundle name.
  8. ChunkBasedCompression Use chunk-based LZ4 compression when creating the AssetBundle.
  9. StrictMode Do not allow the build to succeed if any errors are reporting during it.
  10. DryRunBuild Do a dry run build.
  1. CollectDependencies 包含所有依赖关系。
  2. CompleteAssets 强制包括整个资源。
  3. DisableWriteTypeTree 在资源包不包含类型信息。
  4. DeterministicAssetBundle 编译资源包使用一个哈希表储存对象ID在资源包中。

?