Unity Debug Sheet

repository·master·Indexed 20 days ago

https://github.com/haruma-k/unitydebugsheet

A hierarchical debug menu system for Unity (version 1.5.4) that provides an intuitive, touch-friendly GUI for managing debug commands. It allows developers to create organized debug interfaces with buttons, switches, sliders, and labels, making it particularly useful for mobile development. The system supports custom cells, async lifecycle methods, and integrations with Unity system information, In-game Debug Console, and Graphy.

Tokens
9.9K
Snippets
26
Records
33
Agent score
19%

What's inside Unity Debug Sheet

  1. Overview of Unity Debug Sheet

    master
    Unity Debug Sheet is a hierarchical debug menu system for Unity designed to create intuitive, organized, and easily navigable debug interfaces. It is particularly well-suited for mobile platforms due to its touch-friendly GUI and supports both standard and vertical layouts. It allows developers to quickly add debug commands like buttons, switches, and sliders to manage game state during development.
  2. Handle multiple scenes with DebugSheet

    master

    By default, DebugSheetCanvas acts as a singleton. If multiple DebugSheetCanvas objects exist in different scenes, the first one instantiated is used, and subsequent ones are destroyed.

    To safely handle initialization across multiple scenes where loading order might be unpredictable, use DebugSheet.GetOrCreateInitialPage(). This method retrieves an existing initialized page if available, or initializes a new one if not.

    If you need multiple independent debug sheets, uncheck the Singleton option on the DebugSheet component.

  3. Manage DebugSheet singleton in multiple scenes

    master

    By default, DebugSheetCanvas acts as a singleton. If multiple DebugSheetCanvas objects exist in different scenes, the first one instantiated is used, and subsequent ones are destroyed.

    To handle unpredictable scene loading orders, use DebugSheet.GetOrCreateInitializePage() to retrieve and initialize an existing page if one is already active.

    If you want to disable singleton behavior, uncheck the Singleton option on the DebugSheet component.

  4. Create a custom cell

    master

    To implement a custom cell, you must create two components: a Cell<TModel> component for the UI and a CellModel class for the data.

    Implementation Steps:

    1. Define the Model: Create a class inheriting from CellModel containing the data properties.
    2. Define the Cell: Create a class inheriting from Cell<CustomCellModel>. Implement the SetModel(model) method to apply model data to your UI elements.
    3. UI Setup: Create a Prefab with your UI.
      • Crucial: Attach a Layout Element to the root GameObject and set a Preferred Height.
      • Crucial: Set a fixed width for the cell.
    4. Registration: Add your new cell prefab to the Cell Prefabs list in the Debug Sheet configuration.
    5. Usage: Call your custom Add[CellName] method from your debug page.
    using UnityDebugSheet.Runtime.Core.Scripts;
    using UnityEngine;
    using UnityEngine.UI;
    
    public sealed class CustomTextCell : Cell<CustomTextCellModel>
    {
        [SerializeField] private Text _text;
        [SerializeField] private LayoutElement _layoutElement;
    
        private const int Padding = 36;
    
        protected override void SetModel(CustomTextCellModel model)
        {
            _text.text = model.Text;
            _text.color = model.Color;
            _layoutElement.preferredHeight = _text.preferredHeight + Padding;
        }
    }
    
    public sealed class CustomTextCellModel : CellModel
    {
        public string Text { get; set; }
        public Color Color { get; set; } = Color.black;
    }
  5. Open and close the debug menu

    master

    The debug menu can be accessed via several methods:

    • Flick Gesture: Flick up or down along the edge of the screen. The flickable area is approximately 6mm from the screen edge. This behavior can be configured on the Debug Sheet component via Flick To Open (enabling/disabling sides or disabling entirely).
    • Click Gesture: Configure the Click To Open area and Click Count To Open on the Debug Sheet component to open the menu via taps.
    • Keyboard Shortcut: The default shortcut is Control (Command on Mac) + Shift + D. This can be customized in the Keyboard Shortcut field of the Debug Sheet component.
    • Scripting: You can programmatically toggle the menu by accessing the StatefulDrawerController on the DebugSheetCanvas > Drawer object.
    // Programmatic toggle via script
    // These scripts are attached on the GameObject "DebugSheetCanvas > Drawer".
    StatefulDrawer drawer;
    StatefulDrawerController drawerController;
    
    // Toggle debug sheet.
    var isClosed = Mathf.Approximately(drawer.Progress, drawer.MinProgress);
    var targetState = isClosed ? DrawerState.Max : DrawerState.Min;
    drawerController.SetStateWithAnimation(targetState);
  6. Add a link to a debug page

    master

    To navigate to a custom debug page, you can add a link button to the root page. Use DebugSheet.Instance.GetOrCreateInitialPage() to access the root, then call AddPageLinkButton<T>() where T is your page class.

    Note: If you want your custom page to be the actual starting page (instead of just a link on the root page), use Initialize<T>() instead of adding a link button.

    using UnityDebugSheet.Runtime.Core.Scripts;
    using UnityEngine;
    
    public sealed class DebugSheetController : MonoBehaviour
    {
        private void Start()
        {
            // Get or create the root page.
            var rootPage = DebugSheet.Instance.GetOrCreateInitialPage();
    
            // Add a link transition to the ExampleDebugPage.
            rootPage.AddPageLinkButton<ExampleDebugPage>(nameof(ExampleDebugPage));
        }
    }
  7. Exclude Unity Debug Sheet from release builds

    master

    To ensure debug tools are not included in production builds, follow these steps:

    1. Code Exclusion: Add the EXCLUDE_UNITY_DEBUG_SHEET symbol to your Scripting Define Symbols. Wrap all code that accesses Unity Debug Sheet in preprocessor directives:

      #if !EXCLUDE_UNITY_DEBUG_SHEET
      // Debug sheet related code
      #endif

      Alternatively, place debug code in a separate assembly (.asmdef) and use Define Constraints.

    2. Resource Exclusion: Delete any Resources folders containing debug menu assets.

    3. Scene Cleanup: Remove the Unity Debug Sheet GameObject from your production scenes.

  8. Create custom cells

    master

    To implement a custom cell, you must create two parts: a component inheriting from Cell<TModel> and a model inheriting from CellModel.

    Implementation Steps:

    1. Define the Model: Create a class inheriting from CellModel to hold the data.
    2. Define the Cell: Create a component inheriting from Cell<CustomModel>. Override SetModel(model) to apply the model data to your UI elements.
    3. Setup Prefab:
      • Attach a Layout Element to the root GameObject and set a Preferred Height.
      • Set the width to a fixed value.
      • Create a prefab from this GameObject.
    4. Register: Add the new prefab to the Cell Prefabs list on the DebugSheet component.

    Example Implementation:

    using UnityDebugSheet.Runtime.Core.Scripts;
    using UnityEngine;
    using UnityEngine.UI;
    
    public sealed class CustomTextCell : Cell<CustomTextCellModel>
    {
        [SerializeField] private Text _text;
        [SerializeField] private LayoutElement _layoutElement;
    
        private const int Padding = 36;
    
        protected override void SetModel(CustomTextCellModel model)
        {
            _text.text = model.Text;
            _text.color = model.Color;
            _layoutElement.preferredHeight = _text.preferredHeight + Padding;
        }
    }
    
    public sealed class CustomTextCellModel : CellModel
    {
        public string Text { get; set; }
        
        public Color Color { get; set; } = Color.black;
    }
  9. Display Unity System Information

    master

    You can add debug pages to display information from various Unity classes using the UnityDebugSheet.Unity extension.

    Available Debug Pages

    Class name of DebugPageDescription
    SystemInfoDebugPageShow the information of SystemInfo class.
    ApplicationDebugPageShow the information of Application class.
    TimeDebugPageShow the information of Time class.
    QualitySettingsDebugPageShow the information of QualitySettings class.
    ScreenDebugPageShow the information of Screen class.
    InputDebugPageShow the information of Input class.
    GraphicsDebugPageShow the information of Graphics class.
    PhysicsDebugPageShow the information of Physics class.
    Physics2DDebugPageShow the information of Physics2D class.

    Setup Steps

    1. Assembly Reference: If you are using your own assembly, add UnityDebugSheet.Unity to your referenced assemblies.
    2. Implementation: Use DefaultDebugPageBase.AddPageLinkButton<T> to add the page to your debug menu.
    DefaultDebugPageBase.AddPageLinkButton<SystemInfoDebugPage>("System Info");
  10. Integrate Graphy extension

    master

    You can add a debug menu link to control Graphy using the Unity Debug Sheet extension.

    Setup

    1. Install and set up Graphy.
    2. If NOT installed via Package Manager: Add UDS_GRAPHY_SUPPORT to your Scripting Define Symbols and restart Unity.
    3. If using a custom assembly, add UnityDebugSheet.Graphy (Assets/UnityDebugSheet/Runtime/Extensions/Graphy/UnityDebugSheet.Graphy.asmdef) to your assembly references.
    4. Add the link button in your debug page code.
    DefaultDebugPageBase.AddPageLinkButton<GraphyDebugPage>(
        "Graphy", 
        onLoad: x => x.page.Setup(GraphyManager.Instance)
    );
  11. Integrate In-game Debug Console

    master

    This extension links Unity Debug Sheet with the In-game Debug Console OSS to allow accessing the console via the debug menu.

    Setup Steps

    1. Install and setup In-game Debug Console.
    2. Scripting Define Symbols: If you did not install via the Package Manager, add UDS_INGAMEDEBUGCOSOLE_SUPPORT to your Scripting Define Symbols and restart Unity.
    3. Assembly Reference: If using your own assembly, add UnityDebugSheet.IngameDebugConsole to your referenced assemblies.
    4. Implementation: Use the onLoad callback to pass the DebugLogManager.Instance to the page setup.
    DefaultDebugPageBase.AddPageLinkButton<IngameDebugConsoleDebugPage>("In-Game Debug Console", onLoad: x => x.page.Setup(DebugLogManager.Instance));
  12. Integrate Graphy

    master

    This extension links Unity Debug Sheet with Graphy to display FPS, Memory, and other metrics within your debug menu.

    Setup Steps

    1. Install and setup Graphy.
    2. Scripting Define Symbols: If you did not install via the Package Manager, add UDS_GRAPHY_SUPPORT to your Scripting Define Symbols and restart Unity.
    3. Assembly Reference: If using your own assembly, add UnityDebugSheet.Graphy to your referenced assemblies.
    4. Implementation: Use the onLoad callback to pass the GraphyManager.Instance to the page setup.
    DefaultDebugPageBase.AddPageLinkButton<GraphyDebugPage>("Graphy", onLoad: x => x.page.Setup(GraphyManager.Instance));