让你可以以编程方式生成程序或资源包, 以便于从网络中加载。
||BuildPlayer|编译一个项目。||GetCRCForAssetBundle|提取给定资源包的CRC校验。||GetHashForAssetBundle|提取指定资源包的哈希值。|
BuildPipeline.BuildAssetBundles 编译资源包
public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);
Description 描述
在编辑编译所有指定的资源。
在Unity5.0,在编辑器中让你在包含被命名的资源包中标记资源。这个函数实际上是在编辑器中编译你指定的资源,如果编译成功返回true,否则返回false。此外,错误消息显示说明最常见的编译失败,如不正确的目标文件夹路径。
可选项assetBundleOptions是修改资源包的构建方式,targetPlatform是部署资源要使用的目标平台(如果pc,手机等)。注意,编译为pc平台的资源包,不兼容手机等其他平台,你需要为不同的平台编译不同版本的资源包。
public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);
public class SimpleBundleBuilder
{
[MenuItem("Test/Build Asset Bundles")]
static void BuildABs() {
// Create the array of bundle build details.
AssetBundleBuild[] buildMap = new AssetBundleBuild[2];
buildMap[0].assetBundleName = "enemybundle";
string[] enemyAssets = new string[2];
enemyAssets[0] = "char_enemy_alienShip";
enemyAssets[1] = "char_enemy_alienShip-damaged";
buildMap[1].assetBundleName = "herobundle";
string[] heroAssets = new string[1];
buildMap[1].assetNames = heroAssets;
BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap);
}
}
分开打包
最核心的方法其实就它:
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中
[MenuItem("Custom Editor/Create AssetBunldes ALL")]
static void CreateAssetBunldesALL ()
{
Caching.CleanCache ();
string Path = Application.dataPath + "/StreamingAssets/ALL.assetbundle";
Object[] SelectedAsset = Selection.GetFiltered (typeof(Object), SelectionMode.DeepAssets);
{
}
//这里注意第二个参数就行
if (BuildPipeline.BuildAssetBundle (null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies)) {
AssetDatabase.Refresh ();
} else {
}
}
读取Assetbundle
Assetbundle是可以同时放在服务器或者本地的,无论放在哪里两种下载读取的方式是完全一样的。
None Build assetBundle without any special option.
UncompressedAssetBundle Don't compress the data when creating the asset bundle.
DisableWriteTypeTree Do not include type information within the AssetBundle.
DeterministicAssetBundle Builds an asset bundle using a hash for the id of the object stored in the asset bundle.
ForceRebuildAssetBundle Force rebuild the assetBundles.
IgnoreTypeTreeChanges Ignore the type tree changes when doing the incremental build check.
AppendHashToAssetBundleName Append the hash to the assetBundle name.
ChunkBasedCompression Use chunk-based LZ4 compression when creating the AssetBundle.
DryRunBuild Do a dry run build.