您的位置:首页 > 移动开发 > Unity3D

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

2018-02-03 16:07 3163 查看


目标实现

外部 DLL 工程:https://docs.unity3d.com/Manual/UsingDLL.html

集成 protobuf-net :https://github.com/mgravell/protobuf-net


Windows 平台


工具

Visual Studio 2015

Unity 5.6

Visual Studio 2015 Tools for Unity 

pdb2mdb for Visual Studio 2015 https://gist.github.com/jbevain/ba23149da8369e4a966f

Python 2.7.9


步骤

创建 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.


查阅官网(http://www.mono-project.com/docs/about-mono/releases/5.0.0/ ),提到已经使用【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...】来进行调试:






可以看到已经进行挂载调试了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: