您当前的位置: 首页 >  unity

unity工具人

暂无认证

  • 3浏览

    0关注

    205博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

unity 在画面中显示Debug信息

unity工具人 发布时间:2021-01-12 23:41:58 ,浏览量:3

脚本中新建Scroll View ,在Content下新建Text

在这里插入图片描述

text宽度设置到和Content一样宽 在这里插入图片描述 Content 加组件并设置参数 在这里插入图片描述 Text加组件并设置参数 在这里插入图片描述 然后是代码了 代码如下:

using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class DebugPanel : MonoBehaviour
{

    public static DebugPanel Instance;

    public Text logText;
    public RectTransform content;

    private int count = 0;
    private Vector2 contentVe2 = new Vector2();
    StringBuilder MyStrBulder;
    private bool isUpdate = false;

    private bool isShow = false;

    private DebugPanel()
    {
        if (Instance == null)
            Instance = this;
    }

    private string strDebg = string.Empty;

    public void AddText(string str)
    {
        isUpdate = true;
        MyStrBulder.AppendFormat("{0}:{1}\n", count, str);
        count++;
        isUpdate = false;

    }
    // Use this for initialization
    void Awake()
    {
        MyStrBulder = new StringBuilder();

#if UNITY_5
            Application.logMessageReceived += HandleLog;  
#else
        Application.logMessageReceived += HandleLog;
#endif

    }

    void HandleLog(string message, string stackTrace, LogType type)
    {
        switch (type)
        {
            case LogType.Error:
                message = "" + message + "";
                break;
            case LogType.Assert:
                message = "" + message + "";
                break;
            case LogType.Warning:
                message = "" + message + "";
                break;
            case LogType.Log:
                message = "" + message + "";
                break;
            case LogType.Exception:
                break;
            default:
                break;
        }

        AddText(message);
    }

    public void ShowHide()
    {
        isShow = !isShow;
        if (isShow)
        {
            transform.GetChild(0).localPosition = new Vector3(-9990, 0, 0);
        }
        else
        {
            transform.GetChild(0).localPosition = new Vector3(0, 0, 0);
        }
    }

    private bool isAdd = false;
    // Update is called once per frame
    void Update()
    {
        logText.text = MyStrBulder.ToString();
        LayoutRebuilder.ForceRebuildLayoutImmediate(logText.GetComponent());
        //logText.text += MyStrBulder;
        //contentVe2.Set(0, 16f * count); 
        //content.sizeDelta = contentVe2;
    }

    //IEnumerator UpdateLayout(RectTransform rect)
    //{
    //    yield return new WaitForEndOfFrame();

    //    LayoutRebuilder.ForceRebuildLayoutImmediate(rect);

    //}
}

代码挂在物体上,脚本上挂上Content和Text,运行即可 记得自己在代码里打点Debug测试 效果如下在这里插入图片描述

关注
打赏
1656671177
查看更多评论
立即登录/注册

微信扫码登录

0.0432s