Unity Windows&Mac 编译和调试外部C#动态库(DLL)

目标实现

Windows 平台

工具

步骤

  • 创建 C# 类库工程

  • 引用 UnityEngine.dll 等其他库

  • 添加【成功生成时】的【后期生成事件命令行】
    $(SolutionDir)Build/.Project$(ProjectName).py $(ConfigurationName) $(TargetDir) $(TargetName) $(ProjectDir)$(IntermediateOutputPath)

  • 编写 Python 脚本,以便编译完毕,可以自动拷贝 DLL 等文件到 Unity 工程

注意脚本里面要包含 pdb2mdb,例如:

if isWin:
    p = subprocess.Popen([currentPath + "/pdb2mdb", targetName + ".dll"], shell = True, cwd = targetPath)
    p.wait()

    shutil.copy(targetPath + targetName + ".dll.mdb", resPath + targetName + ".dll.mdb")

另外,需要用 precompile.exe 来对 protobuf 的数据 dll 文件进行序列化和反序列化,如下:

if isWin:
    p = subprocess.Popen([currentPath + "/precompile", targetName + ".dll", "-o:" + targetName + ".AOT.dll", "-t:AOTDataConfig" ], shell = True, cwd = targetPath)

调试

在确定 DLL 对应的 mdb 文件有拷贝到 Unity 工程,就可以直接通过【附加 Unity 调试程序】来进行调试

Mac 平台

工具

  • Visual Studio for Mac 7.3.3

  • Unity 5.6

步骤

  • 将 Windows 平台所创建工程拷贝到 Mac 下

  • 准备修改 .csproj 文件 和 Python 脚本

因为安装 Visual Studio for Mac 时已经附带安装了 Mono.framework,所以不再需要额外安装 mono,自带的版本号为 5.4.1.7,如下所示:

编译工程,发现生成了 DLL 和其对应的 pdb 文件,使用命令:

/Library/Frameworks/Mono.framework/Versions/Current/bin/mono pdb2mdb.exe test.dll

提示不能进行转换,如下所示:

Error: A portable PDB can't be converted to mdb.

查阅官网(Mono 5.0.0 Release Notes | Mono ),提到已经使用【csc.exe】代替【mcs.exe】来进行编译了:

Replacing mcs with csc in user scripts should be straightforward but small issues can arise as command line arguments accepted by csc and features are not identical to mcs. For example, csc generates Portable PDB (.pdb) debug files instead of Mono’s MDB (.mdb) format.

而 Portable 版本的 pdb 文件是无法转换成 mdb 文件,那么就将编译方式改成【mcs.exe】,因为 Unity 也是用这种方式来编译的,编辑器方法 EditorUtility.CompileCSharp 调用如下:

更改每个 .csproj 工程配置文件,在里面添加如下属性:

  <PropertyGroup Condition=" '$(OS)' == 'Unix' ">
    <CscToolExe>mcs.exe</CscToolExe>
  </PropertyGroup>

表示在非 Windows 平台下使用【mcs.exe】进行编译。再次编译工程,可以发现在中间目录 obj\Debug 目录下生成了对应的 mdb 文件,那么修改 Python 脚本,来将这个 mdb 文件也拷贝到 Unity 工程下:

if isWin:
    p = subprocess.Popen([currentPath + "/pdb2mdb", targetName + ".dll"], shell = True, cwd = targetPath)
    p.wait()

    shutil.copy(targetPath + targetName + ".dll.mdb", resPath + targetName + ".dll.mdb")
else:
    shutil.copy(mdbPath + targetName + ".dll.mdb", resPath + targetName + ".dll.mdb")

另外,precompile.exe 的调用也需要更改,如下:

if isWin:
    p = subprocess.Popen([currentPath + "/precompile", targetName + ".dll", "-o:" + targetName + ".AOT.dll", "-t:AOTDataConfig" ], shell = True, cwd = targetPath)
else:
    p = subprocess.Popen(["/Library/Frameworks/Mono.framework/Versions/Current/bin/mono", currentPath + "/precompile.exe", targetName + ".dll", "-o:" + targetName + ".AOT.dll", "-t:AOTDataConfig" ], shell = False, cwd = targetPath)

调试

在确定 DLL 对应的 mdb 文件有拷贝到 Unity 工程,就可以直接通过【Run】→【Attach to Process...】来进行调试:

可以看到已经进行挂载调试了。

注意

Unity 2019.2 及之后的版本已经支持 Portable 版本的 pdb 文件调试,链接:Automated mdb generationicon-default.png?t=L892https://forum.unity.com/threads/automated-mdb-generation.755237/

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值