目录
正文
我们得有工具检查当前有哪些AB包已被加载,以及每个AB包依赖了哪些AB,下面是效果图和代码
绿色背景代表一级AB,也就是咱们主动去加载AB包资源,它一般不会被依赖,它的被依赖数量就是0
红色背景代表一级AB包依赖的资源包,被依赖数量可能是1,也可能更多,因为这个被以来的资源包也可能被其他AB包依赖。
黄色背景代表三级,原理同上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ABDependsViewer : MonoBehaviour
{
class ABInfo
{
public int levelIndex;
public string abPath;
public int dependCount;
}
public bool ShowGUI;
private Vector2 scrollPosition;
private float width;
private float height;
private int showLevel = 1;
private List<ABInfo> showLevels = new List<ABInfo>();
private GUIStyle fontStyle = new GUIStyle();
private GUIStyle levelBtnStyle = new GUIStyle();
private Color defalutColor;
private Color levelOneColor = Color.green;
private Color levelTwoColor = Color.red;
private Color levelThreeColor = Color.yellow;
private Color levelFourColor = Color.blue;
GUIStyle smallFont;
GUIStyle largeFont;
void Start()
{
smallFont = new GUIStyle();
largeFont = new GUIStyle();
smallFont.fontSize = 10;
largeFont.fontSize = 32;
defalutColor = GUI.backgroundColor;
}
private void GetDepends(int levelIndex,List<ABLoadTask> tasks,List<ABInfo> abInfoList)
{
levelIndex++;
for (int i = 0; i < tasks.Count; i++)
{
ABInfo info = new ABInfo();
info.levelIndex = levelIndex;
info.abPath = tasks[i]._bundleLoadPath;
info.dependCount = tasks[i].GetRefCount();
abInfoList.Add(info);
if (tasks[i]._depends.Count > 0)
{
GetDepends(levelIndex, tasks[i]._depends, abInfoList);
}
}
}
private void OnGUI()
{
if (!ShowGUI)
return;
if (!GameSetting.Instance.AssetbundleMode)
return;
GUILayout.BeginVertical();
if (GUILayout.Button("刷新", GUILayout.Width(300), GUILayout.Height(100)))
{
showLevel = 1;
showLevels.Clear();
List<ABLoadTask> ABList = LoadTaskManager.Instance.GetABDependsList();
GetDepends(0, ABList, showLevels);
}
GUILayout.Space(10);
GUILayout.BeginHorizontal();
if (GUILayout.Button("一级", GUILayout.Width(150), GUILayout.Height(50)))
{
showLevel = 1;
}
else if (GUILayout.Button("二级", GUILayout.Width(150), GUILayout.Height(50)))
{
showLevel = 2;
}
else if (GUILayout.Button("三级", GUILayout.Width(150), GUILayout.Height(50)))
{
showLevel = 3;
}
else if (GUILayout.Button("四级", GUILayout.Width(150), GUILayout.Height(50)))
{
showLevel = 4;
}
GUILayout.EndHorizontal();
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(100);
GUILayout.Button("ab包路径", GUILayout.Width(700), GUILayout.Height(100));
GUILayout.Button("被依赖数量", GUILayout.Width(200), GUILayout.Height(100));
GUILayout.EndHorizontal();
scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(1200), GUILayout.Height(800));
for (int i = 0; i < showLevels.Count; i++)
{
GUI.backgroundColor = levelThreeColor;
ABInfo info = showLevels[i];
if (info.levelIndex <= showLevel)
{
if (info.levelIndex == 1)
GUI.backgroundColor = levelOneColor;
else if (info.levelIndex == 2)
GUI.backgroundColor = levelTwoColor;
else if (info.levelIndex == 3)
GUI.backgroundColor = levelThreeColor;
else if (info.levelIndex == 4)
GUI.backgroundColor = levelFourColor;
GUILayout.BeginHorizontal();
int spaceValue = 100 * info.levelIndex;
GUILayout.Space(spaceValue);
GUILayout.Button(info.abPath, GUILayout.Width(800 - spaceValue), GUILayout.Height(50));
GUILayout.Button(info.dependCount.ToString(), GUILayout.Width(200), GUILayout.Height(50));
GUILayout.EndHorizontal();
}
}
GUI.backgroundColor = defalutColor;
GUILayout.EndScrollView();
GUILayout.EndVertical();
}
}