美文网首页
【Unity】自己做一个console窗口

【Unity】自己做一个console窗口

作者: 木心Sepith | 来源:发表于2020-03-17 14:26 被阅读0次

效果

1.gif

功能

  • 显示Log,以及堆栈信息
  • console窗口开关
  • console日志上下吸附开关
  • 窗口拖拽
目录结构

EasyLogView

主要逻辑写在这里

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


namespace EasyLogView
{


    [Serializable]
    public class LogModel
    {
        public string logInfo;
        public string logStackTrace;
        public LogType logType;
        public DateTime logTime;

        public LogModel(string logInfo, string logStackTrace, LogType logType, DateTime logTime)
        {
            this.logInfo = logInfo;
            this.logStackTrace = logStackTrace;
            this.logType = logType;
            this.logTime = logTime;
        }

        public string GetTime()
        {
            return logTime.ToString("hh:mm:ss");
        }
    }




    public class EasyLogView : MonoBehaviour
    {
        public List<LogModel> allLog = new List<LogModel>();

        public Transform content;
        public GameObject infoItem;

        private void Start()
        {
            Application.logMessageReceived += Log;
        }

        private void OnDestroy()
        {
            Application.logMessageReceived -= Log;
        }



        private void Log(string msg, string stackTrace, LogType type)
        {
            LogModel log = new LogModel(msg, stackTrace, type, DateTime.Now);

            allLog.Add(log);

            RefreshUI();
        }

        public void RefreshUI()
        {
            foreach (Transform t in content)
            {
                t.gameObject.SetActive(true);
            }

            if (content.childCount < allLog.Count)
            {
                var createCount = allLog.Count - content.childCount;

                for (int i = 0; i < createCount; i++)
                {
                    var item = GameObject.Instantiate(infoItem);
                    item.transform.SetParent(content);
                    item.transform.localPosition = Vector3.zero;
                }
            }
            else if (content.childCount > allLog.Count)
            {
                for (int i = allLog.Count; i < content.childCount; i++)
                {
                    content.GetChild(i).gameObject.SetActive(false);
                }
            }


            for (int i = 0; i < allLog.Count; i++)
            {
                var itemInfo = content.GetChild(i).GetComponent<EasyLogInfoItem>();
                itemInfo.FlushUI(allLog[i]);
            }
        }

        public void ClearLog()
        {
            allLog.Clear();

            foreach (Transform t in content)
            {
                t.gameObject.SetActive(false);
            }
        }
    }
}

EasyLogInfoItem

单个日志UI刷新

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace EasyLogView
{

    public class EasyLogInfoItem : MonoBehaviour
    {
        public Text textLogType;
        public Text textLogInfo;
        public Text textLogStack;

        private LogModel model;

        public void FlushUI(LogModel model)
        {
            this.model = model;

            textLogType.text = model.logType.ToString();
            textLogType.color = GetColor();
            textLogInfo.text = "[" + model.GetTime() + "] " + model.logInfo;
            textLogStack.text = model.logStackTrace;
        }

        private Color GetColor()
        {
            if (model.logType == LogType.Log)
            {
                return Color.white;
            }
            else if (model.logType == LogType.Warning)
            {
                return Color.yellow;
            }
            else if (model.logType == LogType.Assert)
            {
                return Color.yellow;
            }
            else if (model.logType == LogType.Error)
            {
                return Color.red;
            }
            else if (model.logType == LogType.Exception)
            {
                return Color.red;
            }

            return Color.white;
        }
    }
}

EasyLogViewMove

窗口拖拽逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;


namespace EasyLogView
{

    public class EasyLogViewMove : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {


        public RectTransform canvas;

        /// <summary>
        /// UI和指针的位置偏移量
        /// </summary>
        Vector3 offset;

        public void OnPointerDown(PointerEventData eventData)
        {
            Vector3 globalMousePos;

            //将屏幕坐标转换成世界坐标
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas, eventData.position, null, out globalMousePos))
            {
                //计算UI和指针之间的位置偏移量
                offset = canvas.position - globalMousePos;
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            Vector3 globalMousePos;

            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas, eventData.position, null, out globalMousePos))
            {
                canvas.position = offset + globalMousePos;
            }
        }


    }
}

EasyLogSwitchLogPivot

窗口吸附逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace EasyLogView
{

    public class EasyLogSwitchLogPivot : MonoBehaviour
    {
        public RectTransform content;

        public void SwitchLog()
        {
            bool isOn = GetComponent<Toggle>().isOn;

            if (isOn)
            {
                content.pivot = new Vector2(0.5f, 0f);
            }
            else
            {
                content.pivot = new Vector2(0.5f, 1f);
            }
        }

    }

}

下载

链接:https://pan.baidu.com/s/1gUdNbVj6WMJsZExaRoxuwA 
提取码:3t2y 
复制这段内容后打开百度网盘手机App,操作更方便哦

相关文章

网友评论

      本文标题:【Unity】自己做一个console窗口

      本文链接:https://www.haomeiwen.com/subject/tqdtyhtx.html