Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Last active February 27, 2024 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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