1、AssetBundle 在unity 5 中要先标志好,然后就可以打包时根据平台直接打包即可,手动设置不现实,根据文件路径来确定其归属是一个不错的选择
把需要打包的资源都存在 <span style="font-family: Arial, Helvetica, sans-serif;">AssetBundle 中,然后在Editor 目录加入下面代码</span>
using UnityEngine;
using UnityEditor;
using System.Collections;
using System;
using System.IO;
public class AssetBundlePostProcessor : AssetPostprocessor
{
private const string assetBundleRootPath = "Assets/AssetBundle/";
static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (var str in importedAssets)
{
bool isChanged = CheckModifyIsTarget (str);
if (isChanged)
{
Debug.Log("isChanged Asset: " + str);
SetAssetBundleName (str);
}
}
}
// 检测是否是我希望设置的文件
static bool CheckModifyIsTarget(string path)
{
if(Directory.Exists (path))
{
return false;
}
string fileName = Path.GetFileName (path);
if (string.IsNullOrEmpty (fileName) || fileName.EndsWith (".meta"))
{
return false;
}
else
{
return path.Contains (assetBundleRootPath);
}
}
static void SetAssetBundleName(string path)
{
string asName = GetAssetNameByPath (path);
AssetImporter importer = AssetImporter.GetAtPath (path);
importer.SetAssetBundleNameAndVariant (asName, "");
}
static string GetAssetNameByPath(string path)
{
int index = assetBundleRootPath.Length;
string substr = path.Substring (index);
int lastFlashIndex = substr.LastIndexOf ('/');
return substr.Substring (0, lastFlashIndex);
}
}
2、在网络上看到别人的做法,感觉更好,那就是在打包的时候先设置好,然后再打包。
因为上面 1 的方法没有 reimport 就可能会有问题,而这个方法是在打包时全部设置一次,挺好的。。不过就是可能打包时间比较长。。。
每次打包都设置一次,似乎也会重复,如果要打包两次可能就会重复