Unity OnGUI debugging tool

background

        Debugging data is one of the essential processes in the product development process. So, is there a better way to do a series of tedious operations such as frequently setting data and running Unity? The answer is that there must be a better way. What I use here is the OnGUI related tools that come with unity, which can change data debugging in real time.

code

using System;
using System.Collections;
using System.Collections.Generic;
using RapidGUI;
using UnityEngine;

public class DebugTool : MonoBehaviour
{
    private WindowLaunchers _debugWindows;
    private Rect _mainRect = new Rect(10, 10, 600, 800);
    private Vector2 _scrollRect;
    private bool isShow = false;
    
    private Vector2 _scrollViews = Vector2.zero;
    private void Start()
    {
        _debugWindows = new WindowLaunchers() { isWindow = false};

        InitWondow();
    }

    private float _value = 0;
    private void InitWondow()
    {
        _debugWindows.Add("调试slider", () =>
        {
            _value = RGUI.Slider(_value, 0, 1, "slider的数值");
        });
        _debugWindows.Add("调试button", () =>
        {
            if (GUILayout.Button("button1")) {
                Debug.LogError("this is button1");
            }

            if (GUILayout.Button("button2")) {
                Debug.LogError("this is button2");
            }
        });
        _debugWindows.Add("调试text", () =>
        {
            RGUI.Field(0, "文本显示");
        });
        _debugWindows.Add("调试ScrollView", () =>
        {
            _scrollViews =
                GUILayout.BeginScrollView(_scrollViews, GUILayout.Width(1200), GUILayout.Height(600));
            foreach (var item in tasks) {
                float progress = 0f;
                if (item.Status != DownLoadTaskStatus.Downloading &&
                    item.Status != DownLoadTaskStatus.RemoteDownloading) {
                    progress = 0f;
                }
                else if (item.Status == DownLoadTaskStatus.Downloading) {
                    progress = (float)item.DownloadSize / (float)item.TotalSize;
                }
                else {
                    progress = (float)item.RemoteDownloadSize / (float)item.TotalSize;
                }

                RGUI.Slider(progress, 0f, 1.0f,
                    $"{item.Desc} [状态:{item.Status}] 速度:{StringUtils.FormatSize(item.Speed)}/秒");
            }

            GUILayout.EndScrollView();
        });
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1))
        {
            isShow = !isShow;
        }
    }

    private void OnGUI()
    {
        if (!isShow) return;

        _mainRect = GUI.Window(0, _mainRect, (id) =>
        {
            _scrollRect = GUILayout.BeginScrollView(_scrollRect, false, true, GUILayout.Width(600),
                GUILayout.Height(800));
            GUILayout.BeginVertical(GUILayout.Width(600), GUILayout.Height(800));
            
            _debugWindows.DoGUI();
            
            GUILayout.Space(20);
            GUILayout.EndVertical();
            GUILayout.EndScrollView();
        },"调试窗口");
    }
}

Effect

Code link

                https://download.csdn.net/download/ThreePointsHeat/88463003

Guess you like

Origin blog.csdn.net/ThreePointsHeat/article/details/133995003