AssetBundleManifest文件的内容转txt文件输出

打完补丁包后,补丁包中会有一个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]);
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值