TheMineWay / Unity-Essential-Toolkit

Interconnected Unity packages that helps developers build games faster
https://themineway.github.io/Unity-Essential-Toolkit/
MIT License
2 stars 0 forks source link

Global exceptions handler #16

Open TheMineWay opened 3 months ago

TheMineWay commented 3 months ago
using UnityEngine;

public class GlobalExceptionHandler : MonoBehaviour
{
    private void OnEnable()
    {
        Application.logMessageReceived += HandleLog;
Application.logMessageReceivedThreaded += HandleLog;
    }

    private void OnDisable()
    {
        Application.logMessageReceived -= HandleLog;
Application.logMessageReceivedThreaded -= HandleLog;
    }

    private void HandleLog(string logString, string stackTrace, LogType type)
    {
        if (type == LogType.Exception)
        {
            // Call your custom method here
            OnExceptionCaught(logString, stackTrace);
        }
    }

    private void OnExceptionCaught(string exceptionMessage, string stackTrace)
    {
        Debug.LogError("Exception Caught: " + exceptionMessage);
        Debug.LogError("Stack Trace: " + stackTrace);
        // Add custom logic to handle exceptions here
    }
}