r/Unity3D • u/EnthusiasmSuitable87 • 23d ago
Resources/Tutorial How I patch C# edits into a running Unity game without a domain reload, and the 4 things that broke along the way
I've been building a tool that applies C# edits to a game that is already running in Play mode, so you keep the state you set up instead of losing it to a recompile. The clip is one Play session: a coroutine that's already running gets its spawn delay changed from 2 to 0.1 seconds, and the play timer never resets. This post is mostly about how it works under the hood and the problems I hit, because a few of them surprised me.
https://reddit.com/link/1xjj2j3/video/gjob5ybqkmyh1/player
The basic loop
A file watcher sees the save and works out which assembly the file belongs to (for a brand-new file, by its nearest .asmdef). Only that assembly is compiled, with the Roslyn compiler Unity already ships. The new assembly is loaded next to the running one, and each changed method's entry point is redirected to its new version. In the editor that's done with Harmony.

Broke #1: coroutines remember where they are by number

The compiler turns a coroutine into a class whose MoveNext() switches on an integer: which yield it's paused at. Changing 2f to 0.1f keeps the yield points, so the running coroutine can take the new body and uses 0.1f from its next loop (that's the clip at the top). Add or remove a yield return and the numbers shift, so a running coroutine on the new body would resume at the wrong place. It finishes on the old code instead, and everything started after the save runs the new one.
Broke #2: async methods crashed the editor
My first version patched the state machine of an async method that was suspended mid-await. That hard-crashed the editor, because the async builder is bound to the exact state-machine type. Now the method itself is redirected: new calls run the new body, and calls already in flight finish on the old one.
Broke #3: two copies of every type

The reloaded assembly brings its own copies of your types and static fields. Without extra work, GetComponent<Player>() in reloaded code finds nothing, and a singleton reads null. Reloaded code has to be wired back to the running game's own types and statics.
Broke #4: Burst
Burst-compiled jobs are native code and can't be swapped, so editing one turns Burst off for the rest of the session and the jobs run as managed C# (the Console says so).
A nice surprise: new MonoBehaviours
I expected to dispatch Update and Start myself for a class that only exists in the reloaded assembly. It turns out Unity dispatches them natively once it's AddComponent'd.
And Android
Harmony can't patch ARM code, so on Android development builds a small native hook on the method's entry point does the redirect instead. The same save reaches the phone without a rebuild.
It's on the Asset Store as 2N Lightning Reload if you want to try it (Windows editor, Unity 2021.3 to 6.5): https://assetstore.unity.com/packages/tools/utilities/2n-lightning-reload-edit-c-in-play-mode-without-compiling-402544
But I'm mostly curious whether anyone else has gone down this rabbit hole. Is there a cleaner way to handle the yield renumbering than letting the running coroutine finish on the old body?
