Unity异常捕获

安稳与你 提交于 2020-03-16 12:59:17
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Diagnostics;

public class ExceptionHandler : MonoBehaviour
{
     //是否作为异常处理者
    public bool IsHandler = false;
    //是否退出程序当异常发生时
    public bool IsQuitWhenException = true;
    //异常日志保存路径(文件夹)
    private string LogPath;
    //Bug反馈程序的启动路径
    private string BugExePath;
 
    void Awake()
    {
        LogPath = Application.dataPath.Substring( 0, Application.dataPath.LastIndexOf( "/" ) );
        BugExePath = Application.dataPath.Substring( 0, Application.dataPath.LastIndexOf( "/" ) ) + "\\Bug.exe";
 
        //注册异常处理委托
        if( IsHandler )
        {
            Application.RegisterLogCallback( Handler );
        }
    }
 
    void OnDestory() 
    {
        //清除注册
        Application.RegisterLogCallback( null );
    }
 
    void Handler( string logString, string stackTrace, LogType type )
    {
        if( type == LogType.Error || type == LogType.Exception || type == LogType.Assert )
        {
            string logPath = LogPath + "\\" + DateTime.Now.ToString( "yyyy_MM_dd HH_mm_ss" ) + ".log";
            //打印日志
            if( Directory.Exists( LogPath ) )
            {
                File.AppendAllText( logPath, "[time]:" + DateTime.Now.ToString() + "\r\n" );
                File.AppendAllText( logPath, "[type]:" + type.ToString() + "\r\n" );
                File.AppendAllText( logPath, "[exception message]:" + logString + "\r\n" );
                File.AppendAllText( logPath, "[stack trace]:" + stackTrace + "\r\n" );
            }
            //启动bug反馈程序
            if( File.Exists( BugExePath ) )
            {
                ProcessStartInfo pros = new ProcessStartInfo();
                pros.FileName = BugExePath;
                pros.Arguments = "\"" + logPath + "\"";
                Process pro = new Process();
                pro.StartInfo = pros;
                pro.Start();
            }
            //退出程序,bug反馈程序重启主程序
            if( IsQuitWhenException )
            {
                Application.Quit();
            }
        }
    }
}

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!