Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 2.94 KB

File metadata and controls

57 lines (43 loc) · 2.94 KB

Unity Console Callstack Filter: Underlying Fix & Non-Invasive Interception

English | 中文版

1. Overview

Maintaining a clean Console callstack is essential for debugging efficiency in Unity. While Unity provides the [HideInCallstack] attribute, it faces two major challenges in real-world production:

  1. Native Unity Bug: The attribute is completely ineffective for generic methods.
  2. Uncontrollable Third-party Libraries: It is impossible to add filtering attributes to methods in third-party DLLs (e.g., ZLogger) without recompiling them.

This project fixes the bug via an "underlying IL patch" and implements non-invasive filtering for third-party libraries using a "reflection utility class."


2. Preparation: Enable Native Filtering

Before any operation, ensure the "Strip" option in the Console is enabled; otherwise, no filtering logic will take effect:

  1. Open the Console window.
  2. Click the three-dot menu (⋮) in the top right corner.
  3. Check Strip Logging Callstack.

3. Step 1: Core Fix (Patching the Generic Filtering Bug)

Unity incorrectly uses Split(',') when handling parameters, causing commas in generic arguments to break the method signature validation. This fix is required for the subsequent tool to support generic filtering.

3.1 Backup and Paths

Always back up UnityEditor.CoreModule.dll before modifying:

  • Windows: <Unity_Install_Dir>/Editor/Data/Managed/UnityEngine/UnityEditor.CoreModule.dll
  • macOS: /Applications/Unity/Hub/Editor/<Version>/Unity.app/Contents/Managed/UnityEngine/UnityEditor.CoreModule.dll

3.2 Modification Process

  1. Decompile: Open the DLL with dnSpy as an administrator.
  2. Locate Target: Find UnityEditor.ConsoleWindow -> GetCallstackFormattedSignatureFromGenericMethod.
  3. Perform IL Operation: image1
    • Locate the br instruction below the getSuccess() call. image1
    • Locate the brtrue instruction above the get_Value() call. image2
    • Right-click and change all instructions between them to nop.
  4. Save & Replace: Save the module, replace the original DLL, and restart Unity.

4. Step 2: Application Layer Interception (CallstackFilterTool)

Once the underlying generic bug is fixed, you can dynamically inject filtering rules into Unity without modifying any third-party code.

Usage

  1. Installation: Place CallstackFilterTool.cs into your project.
  2. Configuration: Go to Edit -> Preferences -> Callstack Filter.
  3. Add Rules: Enter the full namespace and class name (e.g., ZLogger.Unity.ZLoggerUnityLoggerProvider).
  4. Persistence: Rules are automatically saved to ProjectSettings/CallstackFilters.json.

5. Important Notes

  • Dependency: Generic method filtering requires the DLL modification to work.
  • Updates: After upgrading Unity, you must re-apply the DLL patch in Step 3.