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
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:
paste that script Slot.cs and pate the screenshot of hierarchy that include script position
– JigneshKoradiya