How to use Rider to debug C# and lua code in Unity

It is often necessary to debug lua code at work. Before, I used VS to debug C# code and Intellij Idea to debug lua code. This is actually quite troublesome, so I checked it out, and I can directly use Rider to debug C# and lua code uniformly.

Rider installation:

Rider download: Download from the Rider official website , select the corresponding version according to your needs, it is recommended to use Rider2020.1.0, refer to the link

Use Rider to debug C# code:

Open Unity and set it to Rider in preference. When you double-click a C# file, it will be opened automatically with Rider:

Select "Attach to Unity Editor" in the Rider menu bar
:

Notice:

1. Generally select "Attach to Unity Editor", do not select "Attach to Unity Editor & Play". The latter will automatically start Unity after clicking the debug icon - in some cases it is not necessary to automatically start Unity for debugging convenience. The difference between the two is only whether to automatically start Unity during debugging, which can be determined according to the specific situation

2. When breaking the C# code, you need to start the debugging in Rider first, and then run Unity.

Using Rider breakpoint C# code is very simple, because you can associate Unity with Rider after installing Rider, and code debugging is very simple

Use Rider to debug Lua code:

1. Install the EmmyLua plugin for lua debugging:

In the Rider menu bar "File -> Settings":

2. Configure LuaDebugger:

Click "Edit Configuration": 

 

Click the "+" in the upper left corner to add LuaDebugger
:

Set the Name and Connection method of LuaDebugger:

3.LuaDebugger has two Conection methods

Method 1: Rider acts as a server - Tcp (Debugger connect IDE)

Rider first starts the debug mode, waits for Unity to start and then connects

1. According to the settings in LuaDebugger, you need to paste "dbg.tcpConnect" into the lua code:

2. Find the initial file where C# calls Lua code, such as "GameMain.lua", etc., and add at the top of the code:

Or you can put the above copied "dbg.tcpConnect" and other codes into the independent lua script "EmmyLuaDebgger.lua", and then "Require EmmyLuaDebugger" in "GameMain.lua" can also:

For example, create a new "EmmyLuaDebugger.lua" script:

-- TCP (Debugger connect IDE)
package.cpath = package.cpath .. ';C:/Users/Frank/AppData/Roaming/JetBrains/Rider2021.3/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

In GameMain.lua:

3. Start Rider debugging mode:

After selecting "LuaDebugger", click the "Debug" button to start debugging:

Then run Unity, after the automatic connection is successful, the breakpoint lua code can be performed normally

Method 2: Unity acts as a server - Tcp (IDE connect debugger)

Start Unity first, wait for Rider to connect and then debug

1. Copy the "dbg" connection code:

2. Create a new EmmyLuaDebugger.lua file:

--[[-- TCP (Debugger connect IDE)
package.cpath = package.cpath .. ';C:/Users/Frank/AppData/Roaming/JetBrains/Rider2021.3/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)]]

-- TCP (IDE connect debugger)
package.cpath = package.cpath .. ';C:/Users/Frank/AppData/Roaming/JetBrains/Rider2021.3/plugins/EmmyLua/debugger/emmy/windows/x64/?.dll'
local dbg = require('emmy_core')
dbg.tcpListen('localhost', 9966)
dbg.waitIDE()                       -- 这句话非常重要,否则无法确定Rider连接状态

 3. Execute the script where C# originally calls the Lua code:

Add the following statement in "GameManager.cs"
:

PS:

1. In this mode, you need to add the "dbg.waitIDE()" statement at the end of "dbg.tcpListen", otherwise, it will run directly after starting Unity, and there will be no "waiting for Rider connection stage", so it is impossible to breakpoint debugging normally of.

After "dbg.waitIDE()" is executed, Unity will be stuck directly. At this time, Unity will continue to execute only after the Rider connection is successful.

2. After executing "dbg.waitIDE", Unity will be stuck directly, waiting for Rider to connect. At this point, you can add some explanations to remind you to start Rider debugging, for example, add "OnGUI" display in "GameManager.cs", and add specific implementation methods later

3. Start Unity, and then start debugging on the Rider side to break the lua code normally

Guess you like

Origin blog.csdn.net/m0_47975736/article/details/124573215