using\u0020System.Collections.Generic;using\u0020UnityEngine;
///\u0020


///\u0020A\u0020console\u0020to\u0020display\u0020Unity's\u0020debug\u0020logs\u0020in-game.
///\u0020
public\u0020class\u0020DebugConsole\u0020:\u0020MonoBehaviour{struct\u0020Log{public\u0020string\u0020message;public\u0020string\u0020stackTrace;public\u0020LogType\u0020type;}
///\u0020
///\u0020The\u0020hotkey\u0020to\u0020show\u0020and\u0020hide\u0020the\u0020console\u0020window.
///\u0020
public\u0020KeyCode\u0020toggleKey\u0020=\u0020KeyCode.BackQuote;List\u0020logs\u0020=\u0020new\u0020List();Vector2\u0020scrollPosition;bool\u0020show;bool\u0020collapse;public\u0020enum\u0020LogFilterType{All,\u0020Log,\u0020Warning,\u0020Error,\u0020Exception}LogFilterType\u0020currentFilterType\u0020=\u0020LogFilterType.All;//\u0020Visual\u0020elements:\nstatic\u0020readonly\u0020Dictionary<LogType,\u0020Color>\u0020logTypeColors\u0020=\u0020new\u0020Dictionary<LogType,\u0020Color>()\u0020{\u0020{\u0020LogType.Assert,\u0020Color.white\u0020},\u0020{\u0020LogType.Error,\u0020Color.red\u0020},\u0020{\u0020LogType.Exception,\u0020Color.red\u0020},\u0020{\u0020LogType.Log,\u0020Color.white\u0020},\u0020{\u0020LogType.Warning,\u0020Color.yellow\u0020},\u0020};const\u0020int\u0020margin\u0020=\u002020;Rect\u0020windowRect\u0020=\u0020new\u0020Rect(margin,\u0020margin,\u0020Screen.width\u0020-\u0020(margin\u0020*\u00202),\u0020Screen.height\u0020-\u0020(margin\u0020*\u00202));Rect\u0020titleBarRect\u0020=\u0020new\u0020Rect(0,\u00200,\u002010000,\u002020);GUIContent\u0020clearLabel\u0020=\u0020new\u0020GUIContent("ClearTempData",\u0020"ClearTempData\u0020the\u0020contents\u0020of\u0020the\u0020console.");GUIContent\u0020collapseLabel\u0020=\u0020new\u0020GUIContent("Collapse",\u0020"Hide\u0020repeated\u0020messages.");void\u0020OnEnable(){Application.RegisterLogCallback(HandleLog);}void\u0020OnDisable(){Application.RegisterLogCallback(null);}void\u0020Update(){if\u0020(Input.GetKeyDown(toggleKey)){show\u0020=\u0020!show;}}void\u0020OnGUI(){GUILayout.BeginVertical();GUI.skin.button.fontSize\u0020=\u0020(int)(24\u0020*\u0020Screen.height\u0020/\u00201280f);GUI.skin.label.fontSize\u0020=\u0020(int)(24\u0020*\u0020Screen.height\u0020/\u00201280f);GUI.skin.toggle.fontSize\u0020=\u0020(int)(24\u0020*\u0020Screen.height\u0020/\u00201280f);GUI.skin.window.fontSize\u0020=\u0020(int)(24\u0020*\u0020Screen.height\u0020/\u00201280f);if\u0020(GUI.Button(new\u0020Rect(Screen.width\u0020/\u00202f\u0020-\u0020150\u0020*\u0020Screen.height\u0020/\u00201280f,\u00200,\u0020300\u0020*\u0020Screen.height\u0020/\u00201280f,\u0020100\u0020*\u0020Screen.height\u0020/\u00201280f),\u0020"msgWin")){show\u0020=\u0020!show;}if\u0020(show){windowRect\u0020=\u0020GUILayout.Window(123456,\u0020windowRect,\u0020ConsoleWindow,\u0020"msgWin");}GUILayout.EndVertical();}
///\u0020
///\u0020A\u0020window\u0020that\u0020displayss\u0020the\u0020recorded\u0020logs.
///\u0020

///\u0020<param\u0020name="windowID">Window\u0020ID.void\u0020ConsoleWindow(int\u0020windowID){scrollPosition\u0020=\u0020GUILayout.BeginScrollView(scrollPosition);currentFilterType\u0020=\u0020(LogFilterType)GUILayout.SelectionGrid((int)currentFilterType,\u0020new\u0020string[]\u0020{\u0020"All",\u0020"Log",\u0020"Warning",\u0020"Error",\u0020"Exception"\u0020},\u00205);//\u0020Iterate\u0020through\u0020the\u0020recorded\u0020logs.\nfor\u0020(int\u0020i\u0020=\u00200;\u0020i\u0020<\u0020logs.Count;\u0020i++){var\u0020log\u0020=\u0020logs[i];if\u0020(currentFilterType\u0020==\u0020LogFilterType.All\u0020||\u0020log.type.ToString()\u0020==\u0020currentFilterType.ToString()){//\u0020Combine\u0020identical\u0020messages\u0020if\u0020collapse\u0020option\u0020is\u0020chosen.\nif\u0020(collapse){var\u0020messageSameAsPrevious\u0020=\u0020i\u0020>\u00200\u0020&&\u0020log.message\u0020==\u0020logs[i\u0020-\u00201].message;if\u0020(messageSameAsPrevious){continue;}}GUI.contentColor\u0020=\u0020logTypeColors[log.type];if\u0020(log.type\u0020==\u0020LogType.Exception){GUILayout.Label(log.message\u0020+\u0020"\n<color=while>"\u0020+\u0020log.stackTrace\u0020+\u0020"");}else{GUILayout.Label(log.message);}}}GUILayout.EndScrollView();GUI.contentColor\u0020=\u0020Color.white;GUILayout.BeginHorizontal();if\u0020(GUILayout.Button(clearLabel)){logs.Clear();}if\u0020(GUILayout.Button(collapseLabel)){collapse\u0020=\u0020!collapse;}GUILayout.EndHorizontal();//\u0020Allow\u0020the\u0020window\u0020to\u0020be\u0020dragged\u0020by\u0020its\u0020title\u0020bar.\n//GUI.DragWindow(titleBarRect);}
///\u0020
///\u0020Records\u0020a\u0020log\u0020from\u0020the\u0020log\u0020callback.
///\u0020

///\u0020<param\u0020name="message">Message.
///\u0020<param\u0020name="stackTrace">Trace\u0020of\u0020where\u0020the\u0020message\u0020came\u0020from.
///\u0020<param\u0020name="type">Type\u0020of\u0020message\u0020(error,\u0020exception,\u0020warning,\u0020assert).void\u0020HandleLog(string\u0020message,\u0020string\u0020stackTrace,\u0020LogType\u0020type){logs.Add(new\u0020Log(){message\u0020=\u0020message,stackTrace\u0020=\u0020stackTrace,type\u0020=\u0020type,});

Unity In-Game Debug Console with Log Filtering - Display and Filter Unity Logs in Your Game

原文地址: https://www.cveoy.top/t/topic/qjbG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录