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

前端 未结 5 1961
悲&欢浪女
悲&欢浪女 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:10

    Create an empty GameObject in your scene and attach this script to it:

    using UnityEngine;
    public class ExceptionManager : MonoBehaviour
    {
        void Awake()
        {
            Application.logMessageReceived += HandleException;
            DontDestroyOnLoad(gameObject);
        }
    
        void HandleException(string logString, string stackTrace, LogType type)
        {
            if (type == LogType.Exception)
            {
                 //handle here
            }
        }
    }
    

    make sure there is one instance.

    The rest is up to you. You can also store the logs in file system, web server or cloud storage.


    Note that DontDestroyOnLoad(gameObject) makes this GameObject persistent, by preventing it from being destroyed in case of scene change.

提交回复
热议问题