Unity 自定义Log日志 错误跳转

         开发过程中,很多时候都需要用到自定义的日志来测试维护项目,希望可以帮到有需要的人!

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using Utility;

public class LogEditor
{    
    private static LogEditor m_Instance;    
    public static LogEditor GetInstacne()
    {
        if (m_Instance == null)
        {
            m_Instance = new LogEditor();
        }        return m_Instance;
    }
    private const string Debugerfilepath = "Assets/Scripts/Core/Utility/LogTool/LogTool.cs";//封装日志脚本路径
    private int m_DebugerFileInstanceId;
    private Type m_ConsoleWindowType = null;
    private FieldInfo m_ActiveTextInfo;
    private FieldInfo m_ConsoleWindowFileInfo;    

    private LogEditor()
    {
        UnityEngine. Object debuggerFile = AssetDatabase.LoadAssetAtPath(Debugerfilepath,typeof(UnityEngine.Object));
        m_DebugerFileInstanceId = debuggerFile.GetInstanceID();        
        m_ConsoleWindowType = Type.GetType("UnityEditor.ConsoleWindow,UnityEditor");//获取consoleWindow
        m_ActiveTextInfo = m_ConsoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);//是当前被选中的Log的详细信息
        m_ConsoleWindowFileInfo = m_ConsoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);//consoleWindow 界面实例

    }
    
    [UnityEditor.Callbacks.OnOpenAssetAttribute(-1)]
    private static bool OnOpenAsset(int instanceID, int line)
    {
        if (instanceID == LogEditor.GetInstacne().m_DebugerFileInstanceId)//双击判断对应得log封装类是否是指定封装类
        {
            return LogEditor.GetInstacne().FindCode();
        }
        return false;
    }    
    
    public bool FindCode()
    {
        var windowInstance = m_ConsoleWindowFileInfo.GetValue(null);
        var activeText = m_ActiveTextInfo.GetValue(windowInstance);        
        string[] contentStrings = activeText.ToString().Split('\n');
        List filePath = new List();
        for (int index = 0; index < contentStrings.Length; index++)
        {
            if (contentStrings[index].Contains("at "))
            {
                filePath.Add(contentStrings[index]);
            }
        }        
        bool success = PingAndOpen(filePath[1]);
        return success;
    }    
    
    public bool PingAndOpen(string fileContext)
    {   
        string regexRule = @"Assets\b([\w\W]*):(\d+)";   // 正则表达格式     
        Match match = Regex.Match(fileContext, regexRule);
        if (match.Groups.Count > 1)
        {
            string path ="Assets"+ match.Groups[1].Value;
            string line = match.Groups[2].Value;
            UnityEngine.Object codeObject = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));
            if (codeObject == null)
            {
                return false;
            }            
            EditorGUIUtility.PingObject(codeObject);
            AssetDatabase.OpenAsset(codeObject, int.Parse(line)); //打开指定脚本 并跳转对应行数           
            return true;
        }        
        return false;
    }
}

 

你可能感兴趣的:(游戏编程,Unity,c#,游戏引擎)