• Unity异常捕获


    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() + "
    " );
                    File.AppendAllText( logPath, "[type]:" + type.ToString() + "
    " );
                    File.AppendAllText( logPath, "[exception message]:" + logString + "
    " );
                    File.AppendAllText( logPath, "[stack trace]:" + stackTrace + "
    " );
                }
                //启动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();
                }
            }
        }
    }

  • 相关阅读:
    oracle登录错误(ORA-01033:ORACLE initialization or shutdown in progress
    mssql 判断sql语句的执行效率语句
    关于 knockout js 学习中的疑问 (1)
    JS 根据Url参数名称来获取对应的值 方法封装
    账户
    windows库
    CentOS的Qt3和Qt4问题
    c/c++调用dll
    CentOS 安装g++
    输入法不见了
  • 原文地址:https://www.cnblogs.com/LiuQizhong/p/12503065.html
Copyright © 2020-2023  润新知