打完补丁包后,补丁包中会有一个Android.ab或者iphone.ab文件,里面记录了所有ab资源的依赖关系,但是这种格式的文件直接打开是看不了的,无论怎么转码都看不了,所以就写了一个编辑器工具用来查看里面的资源依赖关系。(其实打完包后,会附带输出一个manifest文件的,也记录有ab的依赖关系,但是我就是无聊想读Android.ab中的内容,所以简单写了下代码,大佬勿喷)
代码如下:
/********************************************************************
DateTime: 2021-03-23 23:44:17
Author: xiaolin
Description:加载Android.ab文件,读取为txt文件
*********************************************************************/
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
public class ReadAndroidABFile : EditorWindow
{
[MenuItem("开发工具/ShowAndroidABName")]
private static void OpenShowManifestWindow()
{
GetWindow<ReadAndroidABFile>("导出ab文件为txt");
}
private string inputPath = null;
private string outputPath = null;
private void OnGUI()
{
inputPath = EditorGUILayout.TextField("请输入文件路径:", inputPath);
outputPath = EditorGUILayout.TextField("导出txt文件的路径:", outputPath);
if (GUILayout.Button("确定", GUILayout.Width(150)))
{
if (inputPath == string.Empty)
Debug.LogError("输入的文件路径不能为空!!!");
if (!File.Exists(inputPath))
Debug.LogError("输入的文件路径不存在!!!");
if (outputPath == string.Empty)
Debug.LogError("输出的文件路径不能为空!!!");
ReadABFile();
Debug.Log("导出完成 !");
}
}
public void ReadABFile()
{
AssetBundleManifest manifest = new AssetBundleManifest();
//string manifestFileUrl = @"C:\Users\Administrator\Desktop\ver_1.0.1.167_android_644428_20210323222358/Android.ab";
Stream stream = File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read);
if (stream == null)
{
Debug.LogError("文件不存在:" + inputPath);
return;
}
else
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
stream.Seek(0, SeekOrigin.Begin);
try
{
AssetBundle assetBundle = AssetBundle.LoadFromMemory(bytes);
manifest = assetBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest;
assetBundle.Unload(false);
}
catch (System.Exception e)
{
Debug.LogError("加载文件AssetBundleManifest文件异常,路径:" + inputPath + " 异常信息:" + e.ToString());
return;
}
}
stream.Close();
string[] allAssetBundles = manifest.GetAllAssetBundles();
using (StreamWriter sw = new StreamWriter(outputPath))//(@"C:\Users\Administrator\Desktop\Android.txt"))
{
for (int i = 0; i < allAssetBundles.Length; i++)
{
sw.WriteLine(allAssetBundles[i]);
string[] abDependencies = manifest.GetDirectDependencies(allAssetBundles[i]);//获取 AssetBundle 直接依赖
for (int j = 0; j < abDependencies.Length; j++)
{
sw.WriteLine(abDependencies[j]);
}
sw.WriteLine("---------------------------------------------------------");
//Debug.Log(allAssetBundles[i]);
}
}
}
}