How C++ Hot Reload Works Under the Hood
Hot reloading C++ in Unity is not straightforward. Unity locks loaded DLLs on Windows, and the editor's domain reload system is designed for C# assemblies, not native plugins.
Here's how we solve it:
Shadow Copying. Instead of loading the DLL directly, we copy it to a temporary location with a unique name (e.g., plugin_v42.dll). Unity loads the shadow copy, leaving the original free for the compiler to overwrite.
Function Pointer Table. On load, we build a table of function pointers by name. When the DLL is swapped, we update the pointers in-place. C# code that calls native functions through the table sees the new implementation immediately.
State Preservation. Before unloading the old DLL, we serialize the plugin's state to a memory buffer. After loading the new DLL, we deserialize the state back. The user sees their Inspector values preserved across reloads.
File Watcher. A background thread watches the build output directory. When the compiler writes a new DLL, we trigger the swap sequence. The entire process takes 200-500ms depending on DLL size.