Unity Debug输出到屏幕 | log带颜色

Unity Debug输出到屏幕:

 

新版本可用!!!测试版本:Unity2018.3.0

将脚本挂载到场景中,运行。

BackQuote 键显示,就是ESC下面的那个~键,当然你也可以自己手动改

//#define USE_TESTCONSOLE
using System.Collections.Generic;
using UnityEngine;

namespace Consolation
{
    /// 
    /// 一个在游戏中显示Unity调试日志的控制台。
    /// 
    class TestConsole : MonoBehaviour
    {
//#if USE_TESTCONSOLE
        struct Log
        {
            public string message;
            public string stackTrace;
            public LogType type;
        }

        #region Inspector Settings

        /// 
        /// 显示和隐藏控制台窗口的热键。
        /// 
        public KeyCode toggleKey = KeyCode.BackQuote;

        /// 
        /// Whether to open the window by shaking the device (mobile-only).
        /// 是否通过摇动设备来打开窗口(仅限移动设备)。
        /// 
        public bool shakeToOpen = true;

        /// 
        /// 窗口应打开的(平方)加速度。
        /// 
        public float shakeAcceleration = 3f;

        /// 
        /// Whether to only keep a certain number of logs.
        ///是否仅保留一定数量的日志。
        /// Setting this can be helpful if memory usage is a concern.
        /// 如果担心内存使用情况,则设置此选项将很有帮助。
        /// 
        public bool restrictLogCount = false;

        /// 
        /// Number of logs to keep before removing old ones.
        /// 删除旧日志之前要保留的日志数。
        /// 
        public int maxLogs = 1000;
 
        #endregion
 
        readonly List logs = new List();
        Vector2 scrollPosition;
        bool visible;
        bool collapse;

        // Visual elements:视觉元素:

        static readonly Dictionary logTypeColors = new Dictionary
		{
			{ LogType.Assert, Color.white },
			{ LogType.Error, Color.red },
			{ LogType.Exception, Color.red },
			{ LogType.Log, Color.white },
			{ LogType.Warning, Color.yellow },
		};
 
        const string windowTitle = "Console";
        const int margin = 20;
        static readonly GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
        static readonly GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
 
        readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
        Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));

        [System.Obsolete]
        void OnEnable()
        {
#if UNITY_5
			Application.logMessageReceived += HandleLog;
#else
            Application.RegisterLogCallback(HandleLog);
#endif
        }

        [System.Obsolete]
        void OnDisable()
        {
#if UNITY_5
			Application.logMessageReceived -= HandleLog;
#else
            Application.RegisterLogCallback(null);
#endif
        }
 
        void Update()
        {
            if (Input.GetKeyDown(toggleKey))
            {
                visible = !visible;
            }
 
            if (shakeToOpen && Input.acceleration.sqrMagnitude > shakeAcceleration)
            {
                visible = true;
            }
        }
 
        void OnGUI()
        {
            if (!visible)
            {
                return;
            }
 
            windowRect = GUILayout.Window(123456, windowRect, DrawConsoleWindow, windowTitle);
        }

        /// 
        /// Displays a window that lists the recorded logs.
        /// 显示列出已记录日志的窗口。
        /// 
        /// Window ID.
        void DrawConsoleWindow(int windowID)
        {
            DrawLogsList();
            DrawToolbar();
 
            // Allow the window to be dragged by its title bar.
            GUI.DragWindow(titleBarRect);
        }

        /// 
        /// Displays a scrollable list of logs.
        /// 显示日志的滚动列表。
        /// 
        void DrawLogsList()
        {
            scrollPosition = GUILayout.BeginScrollView(scrollPosition);

            // Iterate through the recorded logs.
            //遍历记录的日志。
            for (var i = 0; i < logs.Count; i++)
            {
                var log = logs[i];

                // Combine identical messages if collapse option is chosen.
                //如果选择了折叠选项,则合并相同的消息。
                if (collapse && i > 0)
                {
                    var previousMessage = logs[i - 1].message;
 
                    if (log.message == previousMessage)
                    {
                        continue;
                    }
                }
 
                GUI.contentColor = logTypeColors[log.type];
                GUILayout.Label(log.message);
            }
 
            GUILayout.EndScrollView();

            // Ensure GUI colour is reset before drawing other components.
            //在绘制其他组件之前,请确保已重置GUI颜色。
            GUI.contentColor = Color.white;
        }

        /// 
        /// Displays options for filtering and changing the logs list.
        /// 显示用于过滤和更改日志列表的选项。
        /// 
        void DrawToolbar()
        {
            GUILayout.BeginHorizontal();
 
            if (GUILayout.Button(clearLabel))
            {
                logs.Clear();
            }
 
            collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
 
            GUILayout.EndHorizontal();
        }

        /// 
        /// Records a log from the log callback.
        /// 从日志回调中记录日志。
        /// 
        /// Message.
        /// Trace of where the message came from.
        /// Type of message (error, exception, warning, assert).
        void HandleLog(string message, string stackTrace, LogType type)
        {
            logs.Add(new Log
            {
                message = message,
                stackTrace = stackTrace,
                type = type,
            });
 
            TrimExcessLogs();
        }

        /// 
        /// Removes old logs that exceed the maximum number allowed.
        /// 删除超过允许的最大数量的旧日志。
        /// 
        void TrimExcessLogs()
        {
            if (!restrictLogCount)
            {
                return;
            }
 
            var amountToRemove = Mathf.Max(logs.Count - maxLogs, 0);
 
            if (amountToRemove == 0)
            {
                return;
            }
 
            logs.RemoveRange(0, amountToRemove);
        }
//#endif
    }
}

 

 

Unity Debug.log带颜色输出

 

脚本如下:

Debug.Log(string.Format("{0}", "hello world"));
 
Debug.Log(string.Format("{0}", "welcome to unity"));

 

下图中给出的可用的颜色名称:

http://https://link.fobshanghai.com/rgbcolor.htm 在线取色

自己替换颜色即可

 

 

@Liam→有用→点赞→收藏→关注 

你可能感兴趣的:(Unity)