What's a good global exception handling strategy for Unity3D?

前端 未结 5 1963
悲&欢浪女
悲&欢浪女 2021-02-01 04:40

I\'m looking into doing some Unity3D scripting stuff, and I\'d like to set up global exception handling system. This is not for running in the release version of the game, the i

5条回答
  •  孤街浪徒
    2021-02-01 05:14

    There is a working implementation of RegisterLogCallback that I found here: http://answers.unity3d.com/questions/47659/callback-for-unhandled-exceptions.html

    In my own implementation I use it to call my own MessageBox.Show instead of writing to a log file. I just call SetupExceptionHandling from each of my scenes.

        static bool isExceptionHandlingSetup;
        public static void SetupExceptionHandling()
        {
            if (!isExceptionHandlingSetup)
            {
                isExceptionHandlingSetup = true;
                Application.RegisterLogCallback(HandleException);
            }
        }
    
        static void HandleException(string condition, string stackTrace, LogType type)
        {
            if (type == LogType.Exception)
            {
                MessageBox.Show(condition + "\n" + stackTrace);
            }
        }
    

    I also now have the error handler email me via this routine, so I always know when my app crashes and get as much detail as possible.

            internal static void ReportCrash(string message, string stack)
        {
            //Debug.Log("Report Crash");
            var errorMessage = new StringBuilder();
    
            errorMessage.AppendLine("FreeCell Quest " + Application.platform);
    
            errorMessage.AppendLine();
            errorMessage.AppendLine(message);
            errorMessage.AppendLine(stack);
    
            //if (exception.InnerException != null) {
            //    errorMessage.Append("\n\n ***INNER EXCEPTION*** \n");
            //    errorMessage.Append(exception.InnerException.ToString());
            //}
    
            errorMessage.AppendFormat
            (
                "{0} {1} {2} {3}\n{4}, {5}, {6}, {7}x {8}\n{9}x{10} {11}dpi FullScreen {12}, {13}, {14} vmem: {15} Fill: {16} Max Texture: {17}\n\nScene {18}, Unity Version {19}, Ads Disabled {18}",
                SystemInfo.deviceModel,
                SystemInfo.deviceName,
                SystemInfo.deviceType,
                SystemInfo.deviceUniqueIdentifier,
    
                SystemInfo.operatingSystem,
                Localization.language,
                SystemInfo.systemMemorySize,
                SystemInfo.processorCount,
                SystemInfo.processorType,
    
                Screen.currentResolution.width,
                Screen.currentResolution.height,
                Screen.dpi,
                Screen.fullScreen,
                SystemInfo.graphicsDeviceName,
                SystemInfo.graphicsDeviceVendor,
                SystemInfo.graphicsMemorySize,
                SystemInfo.graphicsPixelFillrate,
                SystemInfo.maxTextureSize,
    
                Application.loadedLevelName,
                Application.unityVersion,
                GameSettings.AdsDisabled
            );
    
            //if (Main.Player != null) {
            //    errorMessage.Append("\n\n ***PLAYER*** \n");
            //    errorMessage.Append(XamlServices.Save(Main.Player));
            //}
    
            try {
                using (var client = new WebClient()) {
                    var arguments = new NameValueCollection();
                    //if (loginResult != null)
                    //    arguments.Add("SessionId", loginResult.SessionId.ToString());
                    arguments.Add("report", errorMessage.ToString());
                    var result = Encoding.ASCII.GetString(client.UploadValues(serviceAddress + "/ReportCrash", arguments));
                    //Debug.Log(result);
                }
            } catch (WebException e) {
                Debug.Log("Report Crash: " + e.ToString());
            }
        }
    

提交回复
热议问题