Skip to content

Commit 7248084

Browse files
authored
Create CompileTime.cs
1 parent 453fbb0 commit 7248084

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
// compile time tracking tool (and quickly enable disable settings, and scene autosave option)
2+
// original source: https://gist.github.com/spajus/72a74146b1bbeddd44a66e1b8a3c829c
3+
// created by https://github.com/spajus twitch https://www.twitch.tv/dev_spajus
4+
// 02.07.2020 added unity_2018_4 check and set autosave off by default - unitycoder.com
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using UnityEditor;
9+
using UnityEditor.Compilation;
10+
using UnityEditor.SceneManagement;
11+
using UnityEngine;
12+
13+
namespace UnityLibrary
14+
{
15+
class CompileTime : EditorWindow
16+
{
17+
bool allowProfiler = false;
18+
bool isTrackingTime;
19+
bool isLockReload;
20+
bool isAutoSave = false; // autosave default off
21+
bool isLocked;
22+
bool restartAfterCompile;
23+
bool memoryWarned;
24+
string lastReloadTime = "";
25+
string lastCompileTime = "";
26+
string lastAssCompileTime = "";
27+
double startTime, finishTime, compileTime, reloadTime;
28+
double assStartTime, assFinishTime, assCompileTime;
29+
Dictionary<string, DateTime> startTimes = new Dictionary<string, DateTime>();
30+
List<string> lastAssCompile;
31+
32+
[MenuItem("Tools/UnityLibrary/Compile Time")]
33+
public static void Init()
34+
{
35+
EditorWindow.GetWindow(typeof(CompileTime));
36+
}
37+
38+
void Update()
39+
{
40+
if (isLockReload) { return; }
41+
if (EditorApplication.isCompiling && !isTrackingTime)
42+
{
43+
if (EditorApplication.isPlaying)
44+
{
45+
restartAfterCompile = true;
46+
EditorApplication.isPlaying = false;
47+
}
48+
startTime = EditorApplication.timeSinceStartup;
49+
lastReloadTime = "Reloading now";
50+
lastCompileTime = "Compiling now";
51+
lastAssCompile = new List<string>();
52+
Debug.Log("Started compiling scripts");
53+
isTrackingTime = true;
54+
}
55+
else if (!EditorApplication.isCompiling && isTrackingTime)
56+
{
57+
finishTime = EditorApplication.timeSinceStartup;
58+
isTrackingTime = false;
59+
EditorApplication.Beep();
60+
reloadTime = finishTime - startTime;
61+
lastReloadTime = reloadTime.ToString("0.000") + "s";
62+
compileTime = reloadTime - assCompileTime;
63+
lastCompileTime = compileTime.ToString("0.000") + "s";
64+
if (isAutoSave && !EditorApplication.isPlaying)
65+
{
66+
Debug.Log("Auto Saving Scene");
67+
EditorSceneManager.SaveOpenScenes();
68+
}
69+
if (restartAfterCompile)
70+
{
71+
restartAfterCompile = false;
72+
EditorApplication.isPlaying = true;
73+
}
74+
}
75+
}
76+
77+
void OnGUI()
78+
{
79+
#if UNITY_2018_4_OR_NEWER
80+
// Toggle domain reload
81+
var playModeOptsEnabled = EditorSettings.enterPlayModeOptionsEnabled;
82+
playModeOptsEnabled = EditorGUILayout.Toggle("Disable Domain Reload", playModeOptsEnabled);
83+
EditorSettings.enterPlayModeOptionsEnabled = playModeOptsEnabled;
84+
#endif
85+
86+
if (UnityEngine.Profiling.Profiler.enabled)
87+
{
88+
EditorGUILayout.LabelField("PROFILER ENABLED");
89+
}
90+
allowProfiler = EditorGUILayout.Toggle("Allow profiler", allowProfiler);
91+
if (!allowProfiler && UnityEngine.Profiling.Profiler.enabled)
92+
{
93+
UnityEngine.Profiling.Profiler.enabled = false;
94+
}
95+
EditorGUILayout.LabelField("Time", Time.realtimeSinceStartup.ToString());
96+
float m1 = (UnityEngine.Profiling.Profiler.GetTotalAllocatedMemoryLong() / 1024f / 1024f);
97+
float m2 = (UnityEngine.Profiling.Profiler.GetAllocatedMemoryForGraphicsDriver() / 1024f / 1024f);
98+
float m3 = (UnityEngine.Profiling.Profiler.GetTotalReservedMemoryLong() / 1024f / 1024f);
99+
float m = m1 + m2 + m3;
100+
if (m > 10000 && !memoryWarned)
101+
{
102+
memoryWarned = true;
103+
EditorApplication.Beep();
104+
Debug.LogError("Memory over 10000MB!");
105+
allowProfiler = false;
106+
UnityEngine.Profiling.Profiler.enabled = false;
107+
}
108+
if (m < 8000 && memoryWarned)
109+
{
110+
memoryWarned = false;
111+
}
112+
EditorGUILayout.LabelField("Memory used:", m.ToString("0.00") + " MB");
113+
114+
isLockReload = EditorGUILayout.Toggle("Lock assembly reload", isLockReload);
115+
isAutoSave = EditorGUILayout.Toggle("Auto Save", isAutoSave);
116+
EditorGUILayout.LabelField("Full reload", lastReloadTime);
117+
EditorGUILayout.LabelField("Compile", lastCompileTime);
118+
if (lastAssCompileTime != null)
119+
{
120+
EditorGUILayout.LabelField("Assembly reload", lastAssCompileTime);
121+
}
122+
// For mysterious reason, iterating over a dictionary doesn't work, it gets empty!
123+
if (lastAssCompile != null)
124+
{
125+
foreach (string s in lastAssCompile)
126+
{
127+
var ss = s.Split(':');
128+
EditorGUILayout.LabelField(ss[0], ss[1]);
129+
}
130+
}
131+
132+
if (isLockReload)
133+
{
134+
if (!isLocked)
135+
{
136+
Debug.Log("Locking reload of assemblies");
137+
EditorApplication.LockReloadAssemblies();
138+
isLocked = true;
139+
}
140+
}
141+
else
142+
{
143+
if (isLocked)
144+
{
145+
Debug.Log("Unlocking reload of assemblies");
146+
EditorApplication.UnlockReloadAssemblies();
147+
isLocked = false;
148+
}
149+
}
150+
}
151+
152+
void OnBeforeAssemblyReload()
153+
{
154+
assStartTime = EditorApplication.timeSinceStartup;
155+
this.ShowNotification(new GUIContent("Started assembly reload"));
156+
}
157+
158+
void OnAfterAssemblyReload()
159+
{
160+
assFinishTime = EditorApplication.timeSinceStartup;
161+
assCompileTime = assFinishTime - assStartTime;
162+
lastAssCompileTime = assCompileTime.ToString("0.000") + "s";
163+
}
164+
165+
void CompilationPipelineOnAssemblyCompilationStarted(string assembly)
166+
{
167+
Debug.Log("Assembly compile started: " + assembly);
168+
startTimes[assembly] = DateTime.UtcNow;
169+
}
170+
171+
void CompilationPipelineOnAssemblyCompilationFinished(string assembly, CompilerMessage[] arg2)
172+
{
173+
var time = startTimes[assembly];
174+
var timeSpan = DateTime.UtcNow - startTimes[assembly];
175+
var bt = string.Format("{0:0.00}s", timeSpan.TotalMilliseconds / 1000f);
176+
var cleanAss = assembly.Replace("Library/ScriptAssemblies/", "");
177+
178+
if (lastAssCompile != null)
179+
{
180+
lastAssCompile.Add(cleanAss + ":" + bt);
181+
}
182+
Debug.Log("Assembly compile ended: " + assembly + " in " + bt);
183+
}
184+
185+
void OnEnable()
186+
{
187+
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
188+
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
189+
CompilationPipeline.assemblyCompilationStarted += CompilationPipelineOnAssemblyCompilationStarted;
190+
CompilationPipeline.assemblyCompilationFinished += CompilationPipelineOnAssemblyCompilationFinished;
191+
}
192+
193+
void OnDisable()
194+
{
195+
AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
196+
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
197+
CompilationPipeline.assemblyCompilationStarted -= CompilationPipelineOnAssemblyCompilationStarted;
198+
CompilationPipeline.assemblyCompilationFinished -= CompilationPipelineOnAssemblyCompilationFinished;
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)