When using the Windows Editor or Standalone builds, loading heavy websites (e.g., those with extensive JS or hash-based navigation like https://www.interserv.com.tw/#service) can cause Unity to crash during the Destroy process. This is typically due to a race condition between the main thread and the STA thread when WebView2 is in a 'busy' or 'loading' state.
To mitigate this, implement the following two core fixes:
1. Stop navigation before releasing COM (C++)
In plugins/Windows/WebViewPlugin.cpp, within the WM_WEBVIEW_DESTROY case, ensure you call Stop() on the webview instance before setting the controllers to nullptr. This allows WebView2 to cancel ongoing navigation and exit the busy state before the COM objects are released.
2. Prevent race conditions in C# OnDestroy
In the C# implementation (e.g., plugins/WebViewObject.cs), modify the OnDestroy logic for Windows platforms. You must nullify the webView pointer before calling the native destroy method. This prevents the Update loop from attempting to call GetMessage or Render on a pointer that is currently being destroyed in the native layer.
Recommended Implementation Pattern:
if (webView == IntPtr.Zero) return;
var ptr = webView;
webView = IntPtr.Zero;
_CWebViewPlugin_Destroy(ptr);