首页 > 代码库 > Unity3D 封 Log

Unity3D 封 Log

  1 using System;  2 using System.Text;  3 using System.Text.RegularExpressions;  4 using UnityEngine;  5 #if UNITY_EDITOR  6 using System.Reflection;  7 using UnityEditor;  8 #endif  9  10 public class LogUtil   11 { 12     private static LogLevel logLv = LogLevel.INFO; 13  14     public enum LogLevel : byte 15     { 16         NONE        = 0, 17         INFO        = 1, 18         WARNING     = 2, 19         ERROR       = 3, 20         EXCEPTION   = 4, 21     } 22  23     public static void Info(object message, UnityEngine.Object sender = null) 24     { 25         if (LogLevel.INFO < logLv) 26             return; 27         Format(LogLevel.INFO,message,sender); 28     } 29  30     public static void Warning(object message, UnityEngine.Object sender = null) 31     { 32         if (LogLevel.WARNING < logLv) 33             return; 34         Format(LogLevel.WARNING, message,  sender); 35     } 36  37     public static void Error(object message, UnityEngine.Object sender = null) 38     { 39         if (LogLevel.ERROR < logLv) 40             return; 41         Format(LogLevel.ERROR, message, sender); 42     } 43  44     public static void Exception(Exception exption, UnityEngine.Object sender = null) 45     { 46         if (LogLevel.EXCEPTION < logLv) 47             return; 48         Format(LogLevel.EXCEPTION, exption, sender); 49     } 50  51     public static void Break(object message, UnityEngine.Object sender = null) 52     { 53         Info(message, sender); 54         Debug.Break(); 55     } 56  57     private static void Format(LogLevel lv, object message, UnityEngine.Object sender) 58     { 59         StringBuilder sb = new StringBuilder(); 60         sb.AppendFormat("<color={0}>[{1}] {2}</color>", GetColor(lv), lv.ToString(), message); 61         Debug.Log(sb, sender); 62     } 63  64     private static string GetColor(LogLevel lv) 65     { 66         string str = "#909090"; 67  68         if (LogLevel.WARNING == lv) 69             str = "orange"; 70         else if (LogLevel.ERROR == lv) 71             str = "red"; 72  73         return str; 74     } 75  76     #if UNITY_EDITOR 77     [UnityEditor.Callbacks.OnOpenAssetAttribute(0)] 78     static bool OnOpenAsset(int instanceID, int line) { 79         string stackTrace = GetStackTrace (); 80         if (string.IsNullOrEmpty (stackTrace)) 81             return false; 82  83         Match matches = Regex.Match (stackTrace, @"\(at Assets(.+)\)"); 84         while (matches.Success) { 85             string str = matches.Groups [1].Value; 86             if (str.Contains ("LogUtil.cs")) { 87                 matches = matches.NextMatch (); 88                 continue; 89             } 90  91             int splitIdx = str.LastIndexOf (":"); 92             string path = str.Substring (0, splitIdx); 93             Int32.TryParse (str.Substring(splitIdx + 1), out line); 94             path = Application.dataPath + path; 95             UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal (path.Replace(/, \\), line); 96             return true; 97         } 98  99         return false;100     }101 102     static string GetStackTrace() {103         // Get ConsoleWindow104         Assembly editorWinAssembly = Assembly.GetAssembly(typeof(EditorWindow));105         Type consoleWinType = editorWinAssembly.GetType("UnityEditor.ConsoleWindow");106         FieldInfo fieldInfo = consoleWinType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);107         object consoleWin = fieldInfo.GetValue(null);108         if ((null == consoleWin) || ((object)EditorWindow.focusedWindow != consoleWin))109             return null;110 111         // Get ListViewState in ConsoleWindow112         Type listViewStateType = editorWinAssembly.GetType ("UnityEditor.ListViewState");113         fieldInfo = consoleWinType.GetField("m_ListView", BindingFlags.Instance | BindingFlags.NonPublic);114         object listView = fieldInfo.GetValue (consoleWin);115 116         // Get row in ListViewState117         fieldInfo = listViewStateType.GetField("row", BindingFlags.Instance | BindingFlags.Public);118 119         // Get m_ActiveText in ConsoleWindow120         fieldInfo = consoleWinType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);121         string text = fieldInfo.GetValue (consoleWin).ToString ();122 123         return text;124     }125     #endif126 }

 

Unity3D 封 Log