Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Last active October 7, 2024 12:27
Show Gist options
  • Save yasirkula/2a7b5c3a678215a7e819144f63964806 to your computer and use it in GitHub Desktop.
Save yasirkula/2a7b5c3a678215a7e819144f63964806 to your computer and use it in GitHub Desktop.
Calculate the time it takes to enter/exit play mode in Unity editor
using UnityEditor;
using UnityEngine;
public class BenchmarkEnterPlayMode
{
[InitializeOnLoadMethod]
private static void Initialize()
{
void OnPlayModeStateChanged( PlayModeStateChange state )
{
if( state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode )
SessionState.SetFloat( "state_exit_time", (float) EditorApplication.timeSinceStartup );
else
Debug.Log( state + " in: " + ( (float) EditorApplication.timeSinceStartup - SessionState.GetFloat( "state_exit_time", 0f ) ) );
}
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
}
@yasirkula
Copy link
Author

How To

Simply create a folder called Editor inside your Project window and add this script inside it. Now, every time you enter/exit play mode in the editor, a message will be logged to the Console, showing how long it took to enter/exit play mode.

This is mostly useful to quickly benchmark the effectiveness of different Configurable Enter Play Mode options: https://docs.unity3d.com/Manual/ConfigurableEnterPlayMode.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment