-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAxTimeTracker.cs
More file actions
317 lines (259 loc) · 10.5 KB
/
AxTimeTracker.cs
File metadata and controls
317 lines (259 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//Written by Aexadev on 16/08/2024
//ver 2.5
using UnityEditor;
using UnityEngine;
using System;
[InitializeOnLoad]
public class ProjectTimeTracker
{
public static readonly string ProjectKeyPrefix = Application.productName + "_";
private static readonly string TotalTimeKey = ProjectKeyPrefix + "ProjectTotalTime";
private static readonly string LastTrackedDateKey = ProjectKeyPrefix + "ProjectLastTrackedDate";
private static readonly string TodayTimeKey = ProjectKeyPrefix + "ProjectTodayTime";
private static DateTime startTime;
private static double totalTime;
private static double todayTime;
private static bool isEditorFocused = true;
private static Vector2 displayPosition = new Vector2(10, 10);
private static PositionPreset currentPreset = PositionPreset.BottomLeft;
private static bool forceRedrawSceneView = false;
private static bool showTotalTimeInSceneView = true;
public enum PositionPreset
{
TopLeft,
TopRight,
BottomLeft,
BottomRight,
TopCenter,
BottomCenter
}
static ProjectTimeTracker()
{
LoadTimeData();
startTime = DateTime.Now;
EditorApplication.update += UpdateTime;
EditorApplication.quitting += OnEditorQuitting;
SceneView.duringSceneGui += OnSceneGUI;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
if (forceRedrawSceneView)
{
EditorApplication.update += ForceRedrawSceneView;
}
}
[MenuItem("Tools/Time Tracker Options")]
public static void ShowWindow()
{
TimeTrackerOptionsWindow window = (TimeTrackerOptionsWindow)EditorWindow.GetWindow(typeof(TimeTrackerOptionsWindow), true, "Time Tracker Options");
window.Show();
}
private static void UpdateTime()
{
if (isEditorFocused)
{
double deltaTime = (DateTime.Now - startTime).TotalSeconds;
totalTime += deltaTime;
if (DateTime.Now.Date != DateTime.Parse(EditorPrefs.GetString(LastTrackedDateKey, DateTime.Now.ToString("yyyy-MM-dd"))).Date)
{
todayTime = 0;
EditorPrefs.SetString(LastTrackedDateKey, DateTime.Now.ToString("yyyy-MM-dd"));
}
todayTime += deltaTime;
startTime = DateTime.Now;
EditorPrefs.SetFloat(TotalTimeKey, (float)totalTime);
EditorPrefs.SetFloat(TodayTimeKey, (float)todayTime);
}
}
private static void OnSceneGUI(SceneView sceneView)
{
if (!showTotalTimeInSceneView) return;
Handles.BeginGUI();
TimeSpan timeSpan = TimeSpan.FromSeconds(totalTime);
int hours = (int)timeSpan.Hours;
string timeText = $"Time Spent: {hours}:{timeSpan.Minutes:D2}:{timeSpan.Seconds:D2}";
GUIStyle style = new GUIStyle();
style.normal.textColor = Color.white;
style.fontSize = 14;
switch (currentPreset)
{
case PositionPreset.TopLeft:
displayPosition = new Vector2(10, 10);
break;
case PositionPreset.TopRight:
displayPosition = new Vector2(sceneView.position.width - 210, 10);
break;
case PositionPreset.BottomLeft:
displayPosition = new Vector2(10, sceneView.position.height - 60);
break;
case PositionPreset.BottomRight:
displayPosition = new Vector2(sceneView.position.width - 210, sceneView.position.height - 60);
break;
case PositionPreset.TopCenter:
displayPosition = new Vector2((sceneView.position.width / 2) - 100, 10);
break;
case PositionPreset.BottomCenter:
displayPosition = new Vector2((sceneView.position.width / 2) - 100, sceneView.position.height - 60);
break;
}
GUILayout.BeginArea(new Rect(displayPosition.x, displayPosition.y, 200, 30));
GUILayout.Label(timeText, style);
GUILayout.EndArea();
Handles.EndGUI();
}
private static void OnEditorQuitting()
{
EditorPrefs.SetFloat(TotalTimeKey, (float)totalTime);
EditorPrefs.SetFloat(TodayTimeKey, (float)todayTime);
EditorPrefs.SetInt(ProjectKeyPrefix + "TimeTrackerPositionPreset", (int)currentPreset);
EditorPrefs.SetString(LastTrackedDateKey, DateTime.Now.ToString("yyyy-MM-dd"));
}
private static void LoadTimeData()
{
if (EditorPrefs.HasKey(TotalTimeKey))
{
totalTime = EditorPrefs.GetFloat(TotalTimeKey);
}
else
{
totalTime = 0.0;
}
if (EditorPrefs.HasKey(TodayTimeKey))
{
todayTime = EditorPrefs.GetFloat(TodayTimeKey);
}
else
{
todayTime = 0.0;
}
if (EditorPrefs.HasKey(ProjectKeyPrefix + "TimeTrackerPositionPreset"))
{
currentPreset = (PositionPreset)EditorPrefs.GetInt(ProjectKeyPrefix + "TimeTrackerPositionPreset");
}
forceRedrawSceneView = EditorPrefs.GetBool(ProjectKeyPrefix + "TimeTrackerForceRedrawSceneView", false);
showTotalTimeInSceneView = EditorPrefs.GetBool(ProjectKeyPrefix + "TimeTrackerShowTotalTimeInSceneView", true);
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredEditMode || state == PlayModeStateChange.EnteredPlayMode)
{
isEditorFocused = true;
startTime = DateTime.Now;
}
else if (state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode)
{
isEditorFocused = false;
UpdateTime();
}
}
public static void ResetTimer()
{
totalTime = 0.0;
todayTime = 0.0;
EditorPrefs.SetFloat(TotalTimeKey, (float)totalTime);
EditorPrefs.SetFloat(TodayTimeKey, (float)todayTime);
}
public static void ModifyTime(double seconds)
{
totalTime += seconds;
todayTime += seconds;
EditorPrefs.SetFloat(TotalTimeKey, (float)totalTime);
EditorPrefs.SetFloat(TodayTimeKey, (float)todayTime);
}
public static void SetPositionPreset(PositionPreset preset)
{
currentPreset = preset;
EditorPrefs.SetInt(ProjectKeyPrefix + "TimeTrackerPositionPreset", (int)preset);
}
public static void SetForceRedrawSceneView(bool value)
{
forceRedrawSceneView = value;
EditorPrefs.SetBool(ProjectKeyPrefix + "TimeTrackerForceRedrawSceneView", value);
if (forceRedrawSceneView)
{
EditorApplication.update += ForceRedrawSceneView;
}
else
{
EditorApplication.update -= ForceRedrawSceneView;
}
}
public static void SetShowTotalTimeInSceneView(bool value)
{
showTotalTimeInSceneView = value;
EditorPrefs.SetBool(ProjectKeyPrefix + "TimeTrackerShowTotalTimeInSceneView", value);
}
private static void ForceRedrawSceneView()
{
SceneView.RepaintAll();
}
public static TimeSpan GetTodayTimeSpan()
{
return TimeSpan.FromSeconds(todayTime);
}
public static TimeSpan GetTotalTimeSpan()
{
return TimeSpan.FromSeconds(totalTime);
}
}
public class TimeTrackerOptionsWindow : EditorWindow
{
private double customTime = 0.0;
private ProjectTimeTracker.PositionPreset selectedPreset;
private bool forceRedrawSceneView;
private bool showTotalTimeInSceneView;
void OnEnable()
{
selectedPreset = ProjectTimeTracker.PositionPreset.BottomLeft;
forceRedrawSceneView = EditorPrefs.GetBool(ProjectTimeTracker.ProjectKeyPrefix + "TimeTrackerForceRedrawSceneView", false);
showTotalTimeInSceneView = EditorPrefs.GetBool(ProjectTimeTracker.ProjectKeyPrefix + "TimeTrackerShowTotalTimeInSceneView", true);
EditorApplication.update += OnEditorUpdate;
}
void OnDisable()
{
EditorApplication.update -= OnEditorUpdate;
}
void OnEditorUpdate()
{
Repaint();
}
void OnGUI()
{
GUILayout.Label("Time Tracker Options", EditorStyles.boldLabel);
if (GUILayout.Button("Reset Timer"))
{
ProjectTimeTracker.ResetTimer();
}
GUILayout.Space(10);
GUILayout.Label("Add/Subtract Time (in seconds):");
customTime = EditorGUILayout.DoubleField("Time:", customTime);
if (GUILayout.Button("Modify Time"))
{
ProjectTimeTracker.ModifyTime(customTime);
}
GUILayout.Space(10);
GUILayout.Label("Select Display Position:");
selectedPreset = (ProjectTimeTracker.PositionPreset)EditorGUILayout.EnumPopup("Position Preset:", selectedPreset);
if (GUILayout.Button("Set Position"))
{
ProjectTimeTracker.SetPositionPreset(selectedPreset);
}
GUILayout.Space(10);
GUILayout.Label("Scene View Settings:", EditorStyles.boldLabel);
forceRedrawSceneView = EditorGUILayout.Toggle("Force Redraw Scene View", forceRedrawSceneView);
showTotalTimeInSceneView = EditorGUILayout.Toggle("Show Total Time in Scene View", showTotalTimeInSceneView);
if (GUILayout.Button("Apply Settings"))
{
ProjectTimeTracker.SetForceRedrawSceneView(forceRedrawSceneView);
ProjectTimeTracker.SetShowTotalTimeInSceneView(showTotalTimeInSceneView);
}
GUILayout.Space(20);
GUILayout.Label("Today's Time Spent:", EditorStyles.boldLabel);
TimeSpan todayTimeSpan = ProjectTimeTracker.GetTodayTimeSpan();
GUILayout.Label($"Hours: {todayTimeSpan.Hours:D2} | Minutes: {todayTimeSpan.Minutes:D2} | Seconds: {todayTimeSpan.Seconds:D2}");
GUILayout.Space(10);
GUILayout.Label("Total Time Spent:", EditorStyles.boldLabel);
TimeSpan totalTimeSpan = ProjectTimeTracker.GetTotalTimeSpan();
GUILayout.Label($"Hours: {totalTimeSpan.Hours:D2} | Minutes: {totalTimeSpan.Minutes:D2} | Seconds: {totalTimeSpan.Seconds:D2}");
GUILayout.FlexibleSpace();
GUILayout.Label("© 2024 Aexadev ver 2.5", EditorStyles.centeredGreyMiniLabel);
}
}