一般来说Bundle之间共同使用的资源会提取出来为一个公用Bundle,但是可能不注意的情况下会导致Bundle下的Prefab引用到了其他Bundle的资源,若这种情况一直积累,就有可能发生启动加载Bundle的时候连带引用加载了一堆Bundle,启动卡顿及占用内存。所以需要根据个项目不同的Bundle打包策略定制一个检测Bundle的错误引用关系,及时纠正。
一般来说都是按目录就区分Bundle的,大概的思路就是这样:
public void Start()
{
//获取所有的Prefab和材质
string[] listString = AssetDatabase.FindAssets("t:Prefab t:Material", new string[] { "Assets/AssetsPackage" });
for (int i = 0; i < listString.Length; i++)
{
string cur = listString[i];
List<string> refs = new List<string>();
string assetPath = AssetDatabase.GUIDToAssetPath(cur);
string headPath = GetHeadPath(assetPath);//获取当前资源应该在的Bundle
string[] names = AssetDatabase.GetDependencies(new string[] { assetPath }); //依赖的东东
foreach (var name in names)
{
bool is_common = false;
//排除引用的是公用资源 commonRef需要定制(值为目录数组),有的第三方库,有的公用资源Bundle等,项目不同这个也不同
foreach (var cc in commonRef)
{
if (name.Contains(cc))
{
is_common = true;
break;
}
}
if (!is_common)
{
if (name.Contains(headPath)) //不是用公用,看看引用的是不是自己所属的bundle
{
is_common = true;
}
}
//上面情况都不是的话应该是有问题的
if (!is_common)
{
if (name == assetPath)
{
is_common = true;
}
}
if (!is_common)
AddProblem(assetPath, name);
}
EditorUtility.DisplayProgressBar("Progress", (i + 1) + "/" + listString.Length, (i + 1) / listString.Length);
}
EditorUtility.ClearProgressBar();
isDone = true;
this.ShowNotification(new GUIContent("Done!"));
}