Unity Addressable Asset System 可寻址资 产系统,从零开始——打包dll文件
前言
之所以写这个是因为在Addressable + ILRuntime的时候,需要把dll文件打包成AA包,但是Addressable本身是不支持dll文件打包的,这里的解决思路是把dll文件转换成byte文件,加载解析dll文件的时候,再把byte文件转成dll文件来用,这个问题就迎刃而解了。
一、dll转byte
先上源码:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
public class DllToBytes
{
public static string normalPath =Application.dataPath+ "/ILRunTimeAndAddressable/AssetsPackage/HotFixDll";
public static string normalPathToSave =Application.dataPath+ "/ILRunTimeAndAddressable/AssetsPackage/HotFixDllToBytes";
[MenuItem("MyMenu/ILRuntime/DllToByte")]
public static void DllToByte()
{
DllToByte(true);
}
[MenuItem("MyMenu/ILRuntime/DllToByteChoose")]
public static void DllToByteChoose()
{
DllToByte(false);
}
private static void DllToByte(bool autoChoosePath)
{
string folderPath,savePath;
if (autoChoosePath)
{
folderPath = normalPath;
}
else
{
folderPath = EditorUtility.OpenFolderPanel("dll所在的文件夹", Application.dataPath + "/addressable/IlRuntime", string.Empty);
}
if (string.IsNullOrEmpty(folderPath))
{
return;
}
DirectoryInfo directoryInfo=new DirectoryInfo(folderPath);
FileInfo[] fileInfos = directoryInfo.GetFiles();
List<FileInfo> listDll=new List<FileInfo>();
List<FileInfo> listPdb=new List<FileInfo>();
for (int i = 0; i <fileInfos.Length; i++)
{
if (fileInfos[i].Extension==".dll")
{
listDll.Add(fileInfos[i]);
}
else if (fileInfos[i].Extension==".pdb")
{
listPdb.Add(fileInfos[i]);
}
}
if (listDll.Count+listPdb.Count<=0)
{
Debug.Log("文件夹下没有dll文件");
}
else
{
Debug.Log("路径为:"+folderPath);
}
if (autoChoosePath)
{
savePath = normalPathToSave;
}
else
{
savePath= EditorUtility.OpenFolderPanel("dll要保存的文件夹", Application.dataPath + "/addressable/IlRuntime", string.Empty);
}
Debug.Log("-----开始转换dll文件------");
string path = string.Empty;
for (int i = 0; i < listDll.Count; i++)
{
//$ 符号的作用:等同于string.Format(),不用写占位符了,直接拼起来就可以了
path = $"{savePath}/{Path.GetFileNameWithoutExtension(listDll[i].Name)}_dll_res.bytes";
Debug.Log(path);
BytesToFile(path,FileToBytes(listDll[i]));
}
Debug.Log("------dll文件转换结束---------");
Debug.Log("-----开始转换pdb文件------");
for (int i = 0; i < listPdb.Count; i++)
{
//$ 符号的作用:等同于string.Format(),不用写占位符了,直接拼起来就可以了
path = $"{savePath}/{Path.GetFileNameWithoutExtension(listPdb[i].Name)}_pdb_res.bytes";
BytesToFile(path,FileToBytes(listPdb[i]));
}
Debug.Log("------pdb文件转换结束---------");
AssetDatabase.Refresh();
}
/// <summary>
/// file转byte
/// </summary>
/// <param name="fileInfo"></param>
/// <returns></returns>
private static byte[] FileToBytes(FileInfo fileInfo)
{
return File.ReadAllBytes(fileInfo.FullName);
}
/// <summary>
/// byte转文件
/// </summary>
/// <param name="path"></param>
/// <param name="bytes"></param>
private static void BytesToFile(string path, byte[] bytes)
{
Debug.Log($"路径为:{path}\nlength:{bytes.Length}");
File.WriteAllBytes(path,bytes);
}
}
解释下这个脚本,需要把这个脚本放到Editor目录下,然后会在工具栏生成相应的手动按钮

第一个功能是自动的把dll,pdb文件转换成byte,要求文件位置都是固定死的。
第二个功能是手动选择需要转换的dll文件的位置。
二、加载完AA包的byte文件之后的解析
这个动作主要是在ILRuntime部分进行的,我的ILRuntime系列的文章会详细的说明。
总结
欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。
如果各位看官觉得文章有点点帮助,跪求各位给点个“一键三连”,谢啦~
声明:本博文章若非特殊注明皆为原创原文链接
https://blog.csdn.net/Wrinkle2017/article/details/122671621
————————————————————————————————
💢💢版权声明
版权声明:本博客为非营利性个人原创
所刊登的所有作品的著作权均为本人所拥有
本人保留所有法定权利,违者必究!
对于需要复制、转载、链接和传播博客文章或内容的
请及时和本博主进行联系
对于经本博主明确授权和许可使用文章及内容的
使用时请注明文章或内容出处并注明网址
转载请附上原文出处链接及本声明

本文介绍如何使用Unity将DLL文件转换为Byte文件以便通过Addressable系统进行管理,并提供了实现这一过程的具体代码示例。

被折叠的 条评论
为什么被折叠?



