zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }

  • 相关阅读:
    Zookeeper概念学习系列之分布式事务
    序列化 反序列化 输入流 输出流
    dfs常见的配置文件中的value与description(重要)
    [转]SQL Server Reporting Services
    [转]webpack4.0.1安装问题和webpack.config.js的配置变化
    [转]Vue.js 入门教程
    [转]Webpack 入门教程
    [转]使用C#调用cmd来执行sql脚本
    [转]winform利用读取xml获取webconfig
    [转]bootstrap栅格系统的属性及使用
  • 原文地址:https://www.cnblogs.com/LiuQizhong/p/12503065.html
Copyright © 2011-2022 走看看