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 ConsoleWindow
104         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 ConsoleWindow
112         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 ListViewState
117         fieldInfo = listViewStateType.GetField("row", BindingFlags.Instance | BindingFlags.Public);
118
119         // Get m_ActiveText in ConsoleWindow
120         fieldInfo = consoleWinType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
121         string text = fieldInfo.GetValue (consoleWin).ToString ();
122
123         return text;
124     }
125     #endif
126 }
时间: 2024-12-11 22:15:01

Unity3D 封 Log的相关文章

unity3d log管理

unity3d的log管理,主要区分log的重要程度,有时候习惯在项目开发过程中随意打log,到后期出现log比较多且混乱的情况,在真机上调试的时候也会很不方便. 这里记录下最近写的一个Log管理类,还有很大的优化空间,以后有时间在详细弄下. public enum LogLevel //Log等级 { Everything, Normal, Important, Emergy } public enum LogColor //Log颜色 { White, Red, Yellow, Blue,

unity3d 资源打包加密

资源打包脚本,放到Assets\Editor 文件夹下 using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class assetPack : Editor { [MenuItem("Custom Editor/Save Scene2")] static void ExportResource() { // Bring up save panel string p

Unity3D Android动态反射加载程序集

这种办法在iOS下是不让用的,只能在Android下用.用起来也很方便了. 1.先创建一个c#工程,引用到的UnityEngine.dll在Unity的安装目录里找吧 2.将编译的dll放入Unity工程,并打成assetBundle.(要把缀名改成.bytes,这个类型Unity才识别,才能打成assetbundle) 打bundle代码 #if UNITY_EDITOR [MenuItem("GameObject/BuildAsset")] static void BuildAss

.unity3d格式的导出与加载

1.单一文件创建unity3d using UnityEngine; using UnityEditor; using System.IO; public class BuildAssetBundlesFromDirectory { [@MenuItem("Asset/Build AssetBundles From Directory of Files")] //生成菜单 static void ExportAssetBundles () { // Get the selected d

unity3d 资源打包加密 整理

资源打包脚本,放到Assets\Editor 文件夹下 using UnityEngine; using System.Collections; using UnityEditor; using System.IO; public class assetPack : Editor { /* [MenuItem("Custom Editor/Build AssetBundle From Selection - Track dependencies")] static void Expor

unity 打包AssetBundle

unity提供了assetbunlde机制,下面介绍一种方法将指定目录下的所有文件打包成AssetBundle 先说明步骤,再上代码. 步骤一.选择要打包成assetbundle的目录,本案例使用assetbundle_raw 步骤二.把要打包的资源或者目录都放到assetbundle_raw目录,资源可以是 prefab,png,fbx,font,audio,txt等. 步骤三.给assetbundle_raw目录下所有的资源文件设置assetbundle名称,如果是目录,会递归. 名称格式:

Unity资源热更

一.创建Bundle打包工具ExportAssetBundles using UnityEditor; using UnityEngine; public class ExportAssetBundles : EditorWindow { [MenuItem("Assets/Build AssetBundle From Selection")] static void ExportResourceRespective() { // 打开保存面板,获得用户选择的路径 string pat

Unity5 AssetBundle 打包和下载

打包 using UnityEngine; using System.Collections; using UnityEditor; using System.IO; /// <summary> /// 把Resource下的资源打包成.unity3d 到StreamingAssets目录下 /// </summary> public class Builder : Editor { public static string sourcePath = Application.dat

Unity5自动命名Assetbundle并打包

using UnityEngine; using System.Collections; using UnityEditor; using System.IO; /// <summary> /// 把Resource下的资源打包成.unity3d 到StreamingAssets目录下 /// </summary> public class Builder : Editor { public static string sourcePath = Application.dataPa