How to measure the amount of time it takes for unity to compile (To compensate Mathematically)

I am trying to figure out how to get the amount of time unity has taken to recompile scripts

Heres what I am trying:

float compiletime = (float)EditorApplication.timeSinceStartup; 
Debug.Log (compiletime);
while(EditorApplication.isCompiling){  
	Debug.Log("Unity is Compiling");
}
compiletime = compiletime - (float)EditorApplication.timeSinceStartup;
Debug.Log (-compiletime);

Any help is appreciated!

paste that script Slot.cs and pate the screenshot of hierarchy that include script position

2 Answers

2

I made a version based on InitializeOnLoad and PlayerPrefs, no need to start up an editor window.

using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
class CompileTime : EditorWindow
{
    static bool isTrackingTime;
    static double startTime;

    static CompileTime()
    {
        EditorApplication.update += Update;
        startTime = PlayerPrefs.GetFloat("CompileStartTime", 0);
        if (startTime > 0)
        {
            isTrackingTime = true;
        }
    }


    static void Update()
    {
        if (EditorApplication.isCompiling && !isTrackingTime)
        {
            startTime = EditorApplication.timeSinceStartup;
            PlayerPrefs.SetFloat("CompileStartTime", (float)startTime);
            isTrackingTime = true;
        }
        else if (!EditorApplication.isCompiling && isTrackingTime)
        {
            var finishTime = EditorApplication.timeSinceStartup;
            isTrackingTime = false;
            var compileTime = finishTime - startTime;
            PlayerPrefs.DeleteKey("CompileStartTime");
            Debug.Log("Script compilation time: 

" + compileTime.ToString(“0.000”) + “s”);
}
}
}

This works great! For anyone confused like me on where to put it. If you put it in a folder called Scripts/Editor, it seems to run automatically. :) I would like to clear the console when it's run though. This can be done by adding the [ClearConsole() method by ToBeGlad][1] to this class and add it on line 34. [1]: https://answers.unity.com/answers/1386578/view.html

Stopped working in 2019.3 =( Wrote a post in forums about it

This is a neat idea and I gave it a shot. I tried to make the class run when the Editor starts with InitializeOnLoad, but the class and the variables would reset during script re-compilation. I thought about using PlayerPrefs to store the time variable, but instead I just made it an EditorWindow, not ideal, but it’s something to start testing.

using UnityEditor;
using UnityEngine;

class CompileTime : EditorWindow
{
	bool isTrackingTime;
	double startTime, finishTime, compileTime;

	[MenuItem("Window/Compile Time")]
	public static void Init()
	{
		EditorWindow.GetWindow(typeof(CompileTime));
	}

	void Update()
	{
		if (EditorApplication.isCompiling && !isTrackingTime)
		{
			startTime = EditorApplication.timeSinceStartup; 
			isTrackingTime = true;
		}
		else if (!EditorApplication.isCompiling && isTrackingTime)
		{
			finishTime = EditorApplication.timeSinceStartup; 
			isTrackingTime = false;

			compileTime = finishTime - startTime;

			Debug.Log("Script compilation time: 

" + compileTime.ToString(“0.000”) + “s”);
}
}

}