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!
Answer by Ville · Mar 22, 2017 at 09:16 AM
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: \n" + 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 to this class and add it on line 34.
Stopped working in 2019.3 =( Wrote a post in forums about it
Answer by Jessespike · Jan 22, 2016 at 08:03 PM
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: \n" + compileTime.ToString("0.000") + "s");
}
}
}
Your answer
Follow this Question
Related Questions
using several compilers, interpreter inside a game 0 Answers
Game Clock (Like in "The Escapist") 1 Answer
Trouble Navigating Pause Menu.... 0 Answers
enemy looking randomly? 0 Answers
Virtual Pet Game (Using the Bathroom) 0 Answers