一.清除上次生成这次不用的AB包(冗余AB包)
主要是思想是根据所有缓存的标签名和打包文件内所有的AB包对比名字,不一样的就删除,每次生成前先清除系统缓存的标签名字
static void DeleteAB()
{
string[] allBundleName = AssetDatabase.GetAllAssetBundleNames();
DirectoryInfo directoryInfo = new DirectoryInfo(Application.streamingAssetsPath);
FileInfo[] files = directoryInfo.GetFiles("*",SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
R = 0;
for (int j = 0; j < allBundleName.Length; j++)
{
Debug.Log(files[i].Name.Split('.')[0]+" "+ allBundleName[j]+" "+ allBundleName.Length);
if (files[i].Name.Split('.')[0]== allBundleName[j]|| files[i].Name.Split('.')[0]=="StreamingAssets")
{
R++;
break;
}
}
if (R==0)
{
Debug.Log("删除冗余AB包");
if (File.Exists(files[i].FullName))
{
File.Delete(files[i].FullName);
}
}
}
}
//清除标签的缓存文件
static void ClearABName()
{
string[] oldABNames = AssetDatabase.GetAllAssetBundleNames();
for (int i = 0; i < oldABNames.Length; i++)
{
AssetDatabase.RemoveAssetBundleName(oldABNames[i],true);
EditorUtility.DisplayProgressBar("清除AB包的名字", "名字:" + oldABNames[i], i * 1.0f / oldABNames.Length); ;
}
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
}
二.生成自己的配置依赖关系表(通过序列化)
创建关系表的基础结构
[System.Serializable]
public class AssetBundleConfig
{
[XmlElement("ABList")]
public List<ABBase> ABList { get; set; }
}
[System.Serializable]
public class ABBase
{
[XmlAttribute("Path")]
public string Path{ get; set; }//AB包路径
[XmlAttribute("Crc")]
public uint Crc { get; set; }//AB包Crc
[XmlAttribute("ABName")]
public string ABName { get; set; }//AB包名字
[XmlAttribute("AssetName")]
public string AssetName { get; set; }//AB包下资源名字
[XmlElement("ABDependce")]
public List<string> ABDependce { get; set; }//AB包的依赖
}
实例化关系表并且序列化
//key是标签名字,value是ab下资源的路径
public static Dictionary<string, string> rePathDic = new Dictionary<string, string>();
//生成资源的配置表并且序列化
static void Write(Dictionary<string,string> rePathDic)
{
AssetBundleConfig config = new AssetBundleConfig();
config.ABList = new List<ABBase>();
foreach (string path in rePathDic.Keys)
{
ABBase aBBase = new ABBase();
aBBase.Path = path;
aBBase.Crc = Crc32.GetCrc32(path);
aBBase.ABName = rePathDic[path];
aBBase.AssetName = path.Remove(0, path.LastIndexOf("/") + 1);
aBBase.ABDependce = new List<string>();
string[] resDependce = AssetDatabase.GetDependencies(path);
//根据
for (int i = 0; i < resDependce.Length; i++)
{
string tempPath = resDependce[i];
if (tempPath==path||path.EndsWith(".cs"))
{
continue;
}
string abName = "";
//传入依赖资源的路径返回其包名,已经包含不添加,否则添加
if (rePathDic.TryGetValue(tempPath,out abName))
{
if (abName == rePathDic[path])
continue;
if (!aBBase.ABDependce.Contains(abName))
{
aBBase.ABDependce.Add(abName);
}
}
}
config.ABList.Add(aBBase);
}
//序列化资源的配置表
string xmlPath = Application.dataPath + "/AssetbundleConfig.xml";
if (File.Exists(xmlPath)) File.Delete(xmlPath);
FileStream fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
XmlSerializer xml = new XmlSerializer(config.GetType());
xml.Serialize(sw, config);
sw.Close();
fileStream.Close();
}