编程文汇

unity中如何调试native插件

#1

如果用c++写unity插件,虽然很多代码都有单元测试,但是实际运行的时候,还是可能出问题的。问题来了,怎么调试呢?

unity本身只带有c#的调试插件,那c++就没办法调试了么?

先google一把,好像只有人问,没人能说怎么能调试。只有自己来了。

隐约记得vc6是可以附加到进程的,现在应该也可以,试试看:

  1. debug编译dll
  2. unity中运行游戏(非常重要,debug前先运行)
  3. 在msvc中选中菜单:调试 -> 附加到进程。
  4. 在弹出的进程列表中选择Unity.exe,附加成功
  5. 在msvc中打个断点

可以看到断点触发,ok,debug可用了。

再写个英文版给国际友人:

How to debug native plugins ( written in c++ c) in Unity?

  1. Open your native plugin project with msvc
  2. Build it in debug mode.
  3. Play your Game in unity (NOTE: Play before debuging)
  4. Choose from menus: "Debug" -> "Attach to process ," (NOT "Attach to Unity"): If not works , try with another msvc without Unity extension. For me, msvc2017 with Unity extersion is for c# developing, msvc2019 for cpp developing.
  5. Pick "Unity.exe" from the process list.
  6. Make a break point in msvc.
  7. Break point should be triggered.
0 Likes