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 2015Unity 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.3Unity 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...】来进行调试:
可以看到已经进行挂载调试了。
相关文章推荐
- C# 没有动态数组,实现动态数组 && local copy(dll)
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序
- C#代码动态编译、动态执行、动态调试
- C#动态编译无法找到 Dll问题.CompilerParameters error CS0006:
- VFP&Dll注册外部 Windows 32 位动态链接库 ( .DLL ) 中的一个函数。
- maven 编译外部包extdirs遇到的问题-Mac和Windows不一样
- C# 动态编译、动态执行、动态调试
- CSC动态编译,监测文件夹下改变(Windows服务形式),自动编译生成dll供web项目使用
- Unity Mac下编译工具以及调试安卓输出Debug日志
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序 集成到Unity3D
- C#反编译工具 ILSPY-x64可动态调试-君临汉化版
- C#如何调试VC编译的DLL
- C#动态调用外部程序集(该程序集在dll文件中)
- [科普小短文]在C#中调用C语言函数(静态调用Native DLL,Windows & Microsoft.Net平台)
- C#动态编译dll或exe
- C#代码动态编译、动态执行、动态调试
- [Qt]调用外部执行程序(Windows & Mac)
- 【转】C#生成dll文件:如何用C#编译生成dll文件即动态链接库文件供其它程序调用
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序
- 艾伟:C#代码动态编译、动态执行、动态调试