Hi zusammen!
Nahezu alle Spiele haben in irgendeiner Form eine Art von visuellem Log System. Da es mich gestört hat während des Testens in Unity immer wieder in die Konsole zu schauen und dort auch nur drei verschiedene Typen von Logs möglich sind, habe ich mir ein eigenes kleines Log System für Unitys GUI Methode geschrieben. Davon abgesehen, dass es später auch noch sehr sinnvoll in Spielen eingesetzt werden kann um dem Spieler detailierte Informationen über das zu geben, was gerade so passiert und für ihn relevant ist.
Hier also der Code.
Leicht lesbar, einfach gehalten und schnell erweiterbar.
Der Logger:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
using System; using System.Collections.Generic; using System.Linq; public struct CombatLogEntry { public string LogEntry; public string LogTimeString; public DateTime LogTime; // maybe used somewhere else in a different way, so just save it also... public CombatLogMessageType CombatLogMessageType; } public enum CombatLogMessageType { Standard, Important, System, Warning, Error, } /// <summary> /// TODO /// customize date format stuff /// </summary> public class Combat { public static List<CombatLogEntry> LogEntries = new List<CombatLogEntry>(); public static int MaxLogEntries = 300; /// <summary> /// /// </summary> /// <param name="message"></param> public static void Log(string message) { Log(CombatLogMessageType.Standard, message); } public static void LogWarning(string message) { Log(CombatLogMessageType.Warning, message); } public static void LogError(string message) { Log(CombatLogMessageType.Error, message); } public static void LogImportant(string message) { Log(CombatLogMessageType.Important, message); } public static void LogSystemInfo(string message) { Log(CombatLogMessageType.System, message); } /// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="message"></param> public static void Log(CombatLogMessageType type, string message) { var entry = new CombatLogEntry(); entry.CombatLogMessageType = type; entry.LogTime = DateTime.Now; entry.LogTimeString = GetFormatedDate(entry.LogTime); entry.LogEntry = message; LogEntries.Add(entry); LogEntries = LogEntries.OrderByDescending(x => x.LogTime).ToList(); if (LogEntries.Count > MaxLogEntries) { LogEntries.RemoveAt(MaxLogEntries); } } // change your format here // http://www.dotnetperls.com/datetime-format private static string GetFormatedDate(DateTime time) { const string format = "t"; return time.ToString(format); } } |
Das UI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public Vector2 scrollPosition = Vector2.zero; void OnGUI() { // combatLog GUILayout.BeginArea(new Rect(10, Screen.height - 175, 1000, 1000)); GUILayout.Box("", GUILayout.Width(375), GUILayout.Height(150)); GUILayout.EndArea(); GUILayout.BeginArea(new Rect(10, Screen.height - 175, 1000, 1000)); scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(375), GUILayout.Height(150)); GUILayout.BeginVertical(); foreach (var entry in Combat.LogEntries) { switch (entry.CombatLogMessageType) { case CombatLogMessageType.Standard: GUILayout.Label("<color=white>" + entry.LogTimeString + " : " + entry.LogEntry + "</color>", GUILayout.Width(350)); GUILayout.Space(-10); break; case CombatLogMessageType.Warning: GUILayout.Label("<color=yellow>" + entry.LogTimeString + " : " + entry.LogEntry + "</color>", GUILayout.Width(350)); GUILayout.Space(-10); break; case CombatLogMessageType.System: GUILayout.Label("<color=green>" + entry.LogTimeString + " [System] : " + entry.LogEntry + "</color>", GUILayout.Width(350)); GUILayout.Space(-10); break; case CombatLogMessageType.Important: GUILayout.Label("<color=orange>" + entry.LogTimeString + " : " + entry.LogEntry + "</color>", GUILayout.Width(350)); GUILayout.Space(-10); break; case CombatLogMessageType.Error: GUILayout.Label("<color=red>" + entry.LogTimeString + " : " + entry.LogEntry + "</color>", GUILayout.Width(350)); GUILayout.Space(-10); break; } } GUILayout.EndVertical(); GUILayout.EndScrollView(); GUILayout.EndArea(); } |
So wird es benutzt:
1 2 3 4 5 6 7 |
Combat.Log("Hello World!"); Combat.Log(CombatLogMessageType.System, "Game saved!"); // oder auch: Combat.LogSystemInfo("Game saved!"); // und: Combat.LogImportant("Level up!"); Combat.LogError("Something failed!!!"); |
Viel Spass damit 🙂