TheMineWay / Unity-Essential-Toolkit

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

Global exceptions handler #16

Open TheMineWay opened 1 month ago

TheMineWay commented 1 month 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
    }
}