一、设置assetBundleName
二、构建AssetBundle包
三、上传AssetBundle到服务器
四、把AssetBundle放到本地
五、操作AssetBundle
六、完整例子
七、AssetBundle Manager管理工具
八、备注知识
二、构建AssetBundle包
三、上传AssetBundle到服务器
四、把AssetBundle放到本地
五、操作AssetBundle
六、完整例子
七、AssetBundle Manager管理工具
八、备注知识
一、设置assetBundleName
如果没有设置AssetBundleName,会打包所有的Assets下的资源,如果设置,就只打包设置了名字的资源
1、在unity编辑器界面手动设置
输入所需的AssetBundle名称。请注意,AssetBundle名称确实支持一种类型的文件夹结构,这取决于您键入的内容。要添加子文件夹,将文件夹名称以“/”分隔。例如:AssetBundle名称“environment/ forest”将在environment子文件夹下创建一个名为forest的包
2、遍历所有要打包的资源,通过代码修改assetBundleName
第一步:先获取你要打包的资源的完整目录
方法1:
先用Selection.objects返回场景中所有的对象,
然后用AssetDatabase.GetAssetPath(selected)获取对象完整目录
方法2:
用AssetPostprocessor的OnPostprocessAllAssets方法
方法3:
用IO流的DirectoryInfo.GetFileSystemInfos()
和FileInfonfo获取完整目录(这种方法要注意:获取到的目录如果是”\”或者”//”要替换为“/”)
第二步:用AssetImporter asset= AssetImporter.GetAtPath(path);方法获取AssetImporter
第三步:用asset.assetBundleName=“text”设置AssetBundleName
有以下三种获取目录然后设置assetBundleName的方法
//Selection.objects返回场景中所有的对象
Object[] selects = Selection.objects;
foreach (Object selected in selects)
{
//返回所有对象相对于工程目录的存储路径如Assets/_Scenes/Main.unity
string path = AssetDatabase.GetAssetPath(selected);
//把一个目录的对象检索为AssetImporter
AssetImporter asset = AssetImporter.GetAtPath(path);
asset.assetBundleName = selected.name; //设置Bundle文件的名称
asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
asset.SaveAndReimport();
}
AssetDatabase.Refresh();
using UnityEngine;
using System.Collections;
using UnityEditor;
//文件描述:自动设置Assetbundle名字为文件夹名_文件名.unity3d;
public class AutoSetTextureUISprite : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (var str in importedAssets)
{
if (!str.EndsWith(".cs"))
{
AssetImporter importer = AssetImporter.GetAtPath(str);
importer.assetBundleName = str;
}
}
foreach (var str in deletedAssets)
{
if (!str.EndsWith(".cs"))
{
AssetImporter importer = AssetImporter.GetAtPath(str);
importer.assetBundleName = str;
}
}
for (var i = 0; i < movedAssets.Length; i++)
{
//Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
/// <summary>
/// 把Resource下的资源打包成.unity3d 到StreamingAssets目录下
/// </summary>
public class Builder : Editor
{
public static string sourcePath = Application.dataPath + "/Resources";
const string AssetBundlesOutputPath = "Assets/StreamingAssets";
//[MenuItem("Tools/AssetBundle/Build")]
public static void BuildAssetBundle()
{
ClearAssetBundlesName();
Pack(sourcePath);
string outputPath = Path.Combine(AssetBundlesOutputPath, Platform.GetPlatformFolder(EditorUserBuildSettings.activeBuildTarget));
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
//根据BuildSetting里面所激活的平台进行打包
BuildPipeline.BuildAssetBundles(outputPath, 0, EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh();
Debug.Log("打包完成");
}
/// <summary>
/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
/// </summary>
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames().Length;
Debug.Log(length);
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
}
for (int j = 0; j < oldAssetBundleNames.Length; j+