Unity OnGUI调试工具

背景

        在产品开发过程中调试数据是必不可少的过程之一,那么涉及到频繁的设置数据、运行unity等一系列繁琐的操作,有没有更好的办法呢?答案是肯定有更好的办法。这里我所用到的是unity自带的OnGUI相关的工具可以实时的更改数据调试。

代码

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();
        },"调试窗口");
    }
}

效果

代码链接

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值