1

I created a simple Custom Editor that shows how much time I spent on Unity. When the button on it is pressed, it records the start time in a Scriptable Object (It's dirty). When the button is pressed again, it records the end time. If the window is closed before the button is pressed, I use the OnDestroy() method to complete the recording. It works (I also use "ExecuteInEditMode"). Here is the problem: If I close Unity without pressing the button, the OnDestroy() method does not work this time. Is there any way to fix this?

sadrag
  • 555
  • 2
  • 7
  • 19
  • [this might help](https://docs.unity3d.com/ScriptReference/EditorApplication-quitting.html) – Ruzihm Nov 03 '20 at 18:41

1 Answers1

0

You could add a callback to EditorApplication.quitting

Unity raises this event when the editor application is quitting.

Add an event handler to this event to receive a notification that the application is quitting.

Note that this will not fire if the Editor is forced to quit or if there is a crash. This event is raised when the quitting process cannot be cancelled.

Note btw that you could do it completely automated without a button or a ScriptableObject:

using UnityEngine;
using UnityEditor;
using System.Diagnostics;

public static class LogEditorTime
{
    private static bool isLogging;
    private static readonly StopWatch sw = new StopWatch ();

    static void OnQuit()
    {
        sw.Stop();
        var elapsedTime = sw.Elapsed;

        Debug.Log($"Quitting the Editor after {elapsedTime.Hours}h {elapsedTime.Minutes}m {elapsedTime.Seconds}.{elapsedTime.Milliseconds / 10}s");
    }

    [InitializeOnLoadMethod]
    static void OnLoad()
    {
        if(isLogging) return;

        sw.Restart();

        EditorApplication.quitting -= OnQuit;
        EditorApplication.quitting += OnQuit;

        isLogging = true;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Thanks for code :) I tried for a while and it's working. StopWatch is good.. But I used EditorApplication.timeSinceStartup.. Anyway, Thank again for explanation. (Btw. Why are my questions getting down votes? I am definitely not writing here without doing a serious research on the internet. And I trying to write clearly. Am I doing something wrong?) – sadrag Nov 04 '20 at 09:55