ro-webgl/Assets/Editor/AssetBundleAnalyzer.cs

185 lines
5.8 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine;
using UnityEditor;
public class AssetBundleAnalyzer : EditorWindow
{
// Start is called before the first frame update
/// <summary>
/// 构建全部资源
/// </summary>
///
public static Dictionary<string, List<string>> AllABs;
public struct ABInfo
{
public string abName;
public int count;
public long selfSize;
public long totalSize;
public ABInfo(string n, int c, long self)
{
abName = n;
count = c;
selfSize = self;
totalSize = 0;
}
public static int compare(ABInfo x, ABInfo y)
{
return y.count - x.count;
}
}
[MenuItem("AssetBundle/Analyzer")]
public static void AnalyzerAllAssetBundles()
{
AllABs = new Dictionary<string, List<string>>();
Debug.Log("<color=green>================ Analyzer AllAssetBundles Start================</color>");
{
string path = Application.dataPath + "/../assetbundle/assetbundle.manifest";
if (File.Exists(path) == false)
{
return;
}
StreamReader sr = new StreamReader(path);
bool isInfo = false;
string info_Name = "";
int nextLine = 0;
while (sr.Peek() != -1)
{
var str = sr.ReadLine();
if (isInfo == false && str.Contains("AssetBundleInfos:"))
{
isInfo = true;
nextLine = 1;
}
else if (isInfo)
{
if (nextLine == 0)
{
if (str.Contains("Name:"))
{
info_Name = str.Substring(str.IndexOf(":") + 2);
AllABs.Add(info_Name, new List<string>());
}
nextLine = 2;
}
else if (nextLine == 1)
{
str = sr.ReadLine();
if (str.Contains("Name:"))
{
info_Name = str.Substring(str.IndexOf(":") + 2);
AllABs.Add(info_Name, new List<string>());
}
nextLine = 2;
}
else if (nextLine == 2)
{
if (str.Contains("Dependencies:"))
{
if (str.Contains("{ }"))
nextLine = 1;
else
nextLine = 3;
}
else
break;
}
else if (nextLine == 3)
{
if (str.Contains("Dependency_"))
{
AllABs[info_Name].Add(str.Substring(str.IndexOf(":") + 2));
}
else
{
nextLine = 0;
}
}
}
}
sr.Close();
}
Dictionary<string,ABInfo> infoDep = new Dictionary<string, ABInfo>();
foreach (var key in AllABs.Keys)
{
long lSize = 0;
#if UNITY_INSTANTGAME
string abPath = Application.dataPath + "/../CustomCloudAssets/AssetsAndroid/" + key;
#else
string abPath = Application.dataPath + "/StreamingAssets/AssetsAndroid/" + key;
#endif
if (File.Exists(abPath))
lSize = new FileInfo(abPath).Length;
infoDep.Add(key, new ABInfo(key, AllABs[key].Count, lSize));
}
// infoDep.Sort(ABInfo.compare);
{
StreamWriter sw = new StreamWriter("D://__InfoResult.txt");
foreach (var i in infoDep.Keys)
{
string abName = infoDep[i].abName;
var ABDep = AllABs[abName];
if (infoDep[i].count == 0)
continue;
List<string> temp = new List<string>(ABDep);
int head = 0;
long total = infoDep[i].selfSize;
while (head < temp.Count)
{
var ABDepDep = AllABs[temp[head]];
foreach (var DDname in ABDepDep)
{
if (temp.Contains(DDname) == false)
temp.Add(DDname);
}
total += infoDep[temp[head]].selfSize;
head++;
}
sw.WriteLine(string.Format("{0}({1}) : {2}/{3}", infoDep[i].abName, toFileSize(infoDep[i].selfSize), infoDep[i].count, temp.Count));
string dep_str = " " + abName + ": ";
for (int j = 0; j < temp.Count; ++j)
{
if (AllABs[temp[j]].Count > 0)
dep_str += (temp[j] + " , ");
}
sw.WriteLine(string.Format("{0,10:f5} : {1} ", toFileSize(total),dep_str));
}
sw.Flush();
sw.Close();
}
Debug.Log("<color=green>================ Analyzer AllAssetBundles Finish================</color>");
}
static string toFileSize(long size)
{
if (size > 1024 * 1024)
{
float sf = 0.001f * 0.001f * size;
return string.Format("{0, 6:F2}MB", sf);
}
else if (size > 1024)
{
return string.Format("{0, 5}Kb", size / 1024);
}
else
return string.Format("<1Kb");
}
}