Giter Site home page Giter Site logo

pinkuburu / unitydebugsheet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haruma-k/unitydebugsheet

0.0 0.0 0.0 22.6 MB

Hierarchical debug menu system for Unity that makes it easy to create intuitive and organized debug menus.

License: MIT License

C# 100.00%

unitydebugsheet's Introduction

Unity Screen Navigator

license

日本語ドキュメント(Japanese Documents Available)

Hierarchical debug menu system for Unity that makes it easy to create intuitive and organized debug menus.

Eyecatch

Table of Contents

Details

Overview

Concept and Features

In general, many debug commands are created during game development, and the number of them increases as development progresses. As a result, it becomes difficult to find the desired command, which in turn leads to a decrease in development efficiency.

Unity Debug Sheet allows you to easily create a debug menu with a hierarchical structure. Its GUI can be easily and intuitively controlled by anyone, especially on mobile platforms.

Debug Sheet

Of course, it also supports vertical layout.

Vertical Layout

Adding debug commands is also easy.

// Label
AddLabel("Example Label");

// Button
AddButton("Example Button", clicked: () => { Debug.Log("Clicked"); });

// Switch
AddSwitch(false, "Example Switch", valueChanged: x => Debug.Log($"Changed: {x}"));

// Slider
AddSlider(0.5f, 0.0f, 1.0f, "Example Slider", valueChanged: x => Debug.Log($"Value Changed: {x}"));

Demo

You can try the demo by cloning this repository itself and playing the demo scenes. The following demo scenes are available.

Character Viewer: CharacterViewerDemo.unity

This demo allows you to switch between character models, motions, etc. from the debug menu. It also include samples that integrate with other libraries such as Graphy and In-Game Debug Console to monitor performance.

Character Viewer Demo

Default Cells: DefaultCellsDemo.unity

This demo allows you to check the behavior of all the cells (generic name for items such as button, labels, and sliders, etc.) that are included in this library by default.

Default Cells Demo

Custom Cells: CustomCellsDemo.unity

In addition to the default cells, you can create your own custom cells. This demo shows how to create custom cells.

Custom Cells Demo

Entry Scene: DemoEntry.unity The above three scenes can also be transitioned from the this scene. You can see how the debug sheet placed in each scene behaves as singleton.

Setup

Requirements

This tool is compatible with the following environments.

  • Unity 2020.3 or later

Installation

You can install this library by the steps below.

  1. Select Window > Package Manager from the menu bar.
  2. Click the + button in the upper left corner of the window and select Add package from git URL....
  3. Enter the following URL in the input field and click Add.
https://github.com/Haruma-K/UnityDebugSheet?path=/Assets/UnityDebugSheet

Package Manager

Or, you can also install by adding the following line to the dependencies block of manifest.json file in the Packages folder.

{
  "dependencies": {
    "com.harumak.unitydebugsheet": "https://github.com/Haruma-K/UnityDebugSheet.git?path=/Assets/UnityDebugSheet"
  }
}

If you want to specify the version, you can do so by adding the version number to the end of the URL.

https://github.com/Haruma-K/UnityDebugSheet.git?path=/Assets/UnityDebugSheet#1.0.0

You can update the version by changing the version number above.
If you do not specify the version, you can update it by opening the file Packages/package-lock.json and rewriting the hash of this library.

{
  "dependencies": {
      "com.harumak.unitydebugsheet": {
      "version": "https://github.com/Haruma-K/UnityDebugSheet.git?path=/Assets/UnityDebugSheet",
      "depth": 0,
      "source": "git",
      "dependencies": {},
      "hash": "..."
    }
  }
}

Quick Start

This section shows an easy way to set up and use the Unity Debug Sheet.

Place the prefab in the scene

First, drag and drop the prefab named DebugSheetCanvas to Hierarchy window to place it in the scene. And if EventSystem not exists, create it.

Debug Sheet Canvas

Create the debug page

Next, create a page for debugging. Create the page by inheriting from DebugPageBase as shown below.
The following is an example page with a single button that logs Clicked when clicked.

using System.Collections;
using UnityDebugSheet.Runtime.Core.Scripts;
using UnityEngine;

public sealed class ExampleDebugPage : DebugPageBase
{
    protected override string Title { get; } = "Example Debug Page";

    public override IEnumerator Initialize()
    {
        // Add a button to this page.
        AddButton("Example Button", clicked: () => { Debug.Log("Clicked"); });

        // Shen you added any item, call Reload(). 
        Reload();

        yield break;
    }
}

Add a link to the debug page.

Next, add a link to transition to the debug page created above.
Get the root page and add a link button to it as shown below.

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));

        // You must call Reload() after adding cells.
        rootPage.Reload();
    }
}

NOTE If you want to use your own page as the root page instead of linking as above, use Initialize<ExampleDebugPage>() instead of GetOrCreateInitialPage().AddPageLinkButton<ExampleDebugPage().

Open and close the debug menu

The debug menu can be opened and closed by flicking up and down along the edge of the screen. On the actual device, the area extends from the edge of the screen to approximately 6mm within the safe area. In the demo scene, the red band is displayed in this area to visualize flickable range.

Open/Close the debug menu

This behavior can be changed from the Global Control Mode on the Debug Sheet component.
You can enable only the left or right side of the screen, or disable flick operation.

Global Control Mode

Warning In the Unity editor, this range is not necessarily 6mm because it simulate the resolution of the actual device. It is recommended to also use the operation with keyboard shortcuts described below.

You can use keyboard shortcuts to open and close the debug menu. In default, Control (Command on Mac) + Shift + D toggles the debug menu. Shortcuts can be freely changed from Keyboard Shortcut on the Debug Sheet component.

Keyboard Shortcut

And you can also open/close the debug menu from your script as below.

// 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);

Result

With the implementation up to this point, you can confirm that the debug menu works as follows.

Result

Basic Usage

This section describes the basic usage of the Unity Debug Sheet. The Quick Start section is the prerequisite, so please read it first.

List of Available Cells

In default, you can use the following cells.

Cell Name Method Name to Add Description
Label AddLabel Used to display strings.
Button AddButton Used to trigger actions when clicked.
Switch AddSwitch Used to switch ON and OFF.
Slider AddSlider Used to specify a numerical value within a range.
Picker AddPicker Used to select one of several options.
Enum Picker AddEnumPicker Used to select one of the elements of the enum.
Multi Picker AddMultiPicker Used to select more than one from multiple options.
Enum Multi Picker AddEnumMultiPicker Used to select more than one from the elements of the enum.
Page Link Button AddPageLinkButton Used to transition to the other debug pages when clicked.

You can check the behavior of each cell by playing the Default Cells Demo Scene. You can also create your own cells. See the Custom Cells section for more information about this.

Update the cell contents

Using CellModel, you can update the contents of the cell that has been generated. The following is an example of renaming a generated button each time the Space key is pressed. Please refer to the comments for details.

using System.Collections;
using UnityDebugSheet.Runtime.Core.Scripts;
using UnityDebugSheet.Runtime.Core.Scripts.DefaultImpl.Cells;
using UnityEngine;

public sealed class ExampleDebugPage : DebugPageBase
{
    private int _buttonCellIndex;
    private ButtonCellModel _buttonCellModel;
    private int _counter;
    protected override string Title => "Example Debug Page";

    public override IEnumerator Initialize()
    {
        // Create the CellModel and set data and events.
        var buttonCellModel = new ButtonCellModel(false);
        buttonCellModel.CellTexts.Text = GetButtonName();
        buttonCellModel.Clicked += () => { Debug.Log("Clicked"); };
        
        // Keep the index of the cell and the CellModel.
        _buttonCellIndex = AddButton(buttonCellModel);
        _buttonCellModel = buttonCellModel;

        Reload();
        
        yield break;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Update the cell data
            _counter++;
            _buttonCellModel.CellTexts.Text = GetButtonName();
            
            // Refresh the target cell.
            RefreshDataAt(_buttonCellIndex);
            
            // You can also refresh all data by calling RefreshData().
            //RefreshData();
        }
    }

    private string GetButtonName()
    {
        return $"Example Button {_counter}";
    }
}

Workflow in multiple scenes

In default, DebugSheetCanvas is used as a singleton. That is, if DebugSheetCanvas is placed in two scenes, the one instantiated first will be used and the one loaded later will be destroyed.

In this case, initialization should be only be done on the first DebugSheet. If the order of scene loading is not guaranteed, you can use DebugSheet.GetOrCreateInitializePage() to get a page that has already been initialized, if any, and initialize it. Please refer to the DemoEntry scene for the workflow in multiple scenes.

Note that if you do not want to use it as singleton, you can do it by unchecking Singleton in the DebugSheet component.

Singleton

Exclude from the release builds

You should exclude GameObject, script files, and resources files related to the debug menu from the release builds.

You can exclude all scripts of the Unity Debug Sheet by adding EXCLUDE_UNITY_DEBUG_SHEET to the Scripting Define Symbols in the Player Settings. Thus, if you enclose all your own code that accesses the Unity Debug Sheet with #if EXCLUDE_UNITY_DEBUG_SHEET, you can exclude all the code from the release builds. It may also be a good idea to combine your own scripts that accesses the Unity Debug Sheet into a single assembly and set Define Constraints in the asmdef.

In addition, you can completely exclude the debug menu from your build by doing the following.

  • Delete the Resources folder containing the debug menu resources if you have created.
  • Delete the GameObject containing the DebugSheet component in your scenes.

Custom Cells

To create your own cell, first create a component that inherits from Cell and a model that inherits from CellModel to set data to it.

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;
}

Then, create a GUI, attach this component to it, and make it a prefab. Due to the implementation of the cell recycling system, the following points should be taken into account when implementing the cell.

  • Attach a Layout Element to the root GameObject and input a height in the Preferred Height property.
  • Set the width of the cell to a fixed value.

Next, set this cell to Cell Prefabs on the Debug Sheet.

Cell Prefabs

All that remains is to add this cell to the page. Please refer to the demo scene of the custom cells for the actual implementation.

Advanced Usage

Hide Backdrop

In default, the black GUI is displayed as the backdrop of the debug menu. You can hide it if you want by deactivating the GameObject of DebugSheetCanvas > Backdrop.

Disable Backdrop

Don't close the debug menu when the backdrop is clicked

In default, the debug menu is closed when the backdrop is clicked. You can disable this behavior by unchecking Close When Backdrop Clicked in the Stateful Drawer Backdrop Controller component attached to the DebugSheetCanvas > Drawer.

Close When Backdrop Clicked

Change the Show/Hide animations

You can change the Show/Hide animations by changing the AnimationDuration and AnimationCurve properties in the Stateful Drawer component attached to the DebugSheetCanvas > Drawer.

Animation Settings

Work within the safe area

By default, the debug menu appears from outside the screen, and is placed outside the screen when it is hidden. You can work it within the safe area by checking Move Inside Safe Area property in the Debug Sheet Drawer component attached to the DebugSheetCanvas > Drawer.

Move Inside Safe Area

Change the min/max size of the debug menu

You can also change the min/max size of the debug menu. If you changed this, it can always be displayed at the bottom of the screen, even when minimized, as shown below, for example.

Set Min/Max Size

To change the size of each state, edit the properties of the Debug Sheet Drawer attached to Debug Sheet Canvas > Drawer. Adjust the Min's Progress to change the size shen minimized and Size to change the size when maximized. Middle is the middle size applied when held vertically.

Set Min/Max Size

You can apply and check the size of each state by clicking Set State button in Debug area.

Size Debugging

Customize the look

Unity Debug Sheet consists of uGUI, so you can freely customize the look by adjusting the properties.

  • Color of the back and close buttons
  • Color of the backdrop
  • Text color of the title

The design of each cell can be freely customized by creating a custom cell. See the Custom Cells section for more information on this.

Licenses

This software is released under the MIT License. You are free to use it within the scope of the license, but the following copyright and license notices must be displayed when using it.

In addition, the table of contents of this document was created using the following software

The following software is used in the demo scenes.

See Third Party Notices.md for details on these licenses.

unitydebugsheet's People

Contributors

haruma-k avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.