-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFastCompileManager.cs
More file actions
146 lines (123 loc) · 3.76 KB
/
FastCompileManager.cs
File metadata and controls
146 lines (123 loc) · 3.76 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
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
[InitializeOnLoad]
public class FastCompileManager : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
private const string PrefKey = "FastCompileManager_Enabled";
private const string MenuPathToggle = "Tools/Fast Compile/Enable Manual Compile Mode";
private const string MenuPathCompile = "Tools/Fast Compile/Compile Now";
static bool locked;
public static bool IsEnabled
{
get => EditorPrefs.GetBool(PrefKey, false);
set
{
EditorPrefs.SetBool(PrefKey, value);
ApplyState(value);
}
}
static FastCompileManager()
{
ApplyState(IsEnabled);
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
}
private static void OnAfterAssemblyReload()
{
if (IsEnabled)
{
Lock();
}
}
[MenuItem(MenuPathCompile + " _F5")]
public static void CompileNow()
{
Debug.Log("[KF_FastCompile] Trying to force compile...");
if (!IsEnabled)
{
Debug.Log("[KF_FastCompile] Manual Mode is disabled. Unity already compiles automatically.");
return;
}
Debug.Log("[KF_FastCompile] Forcing Compilation, Domain Reload and Scene Reload...");
Unlock();
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
{
string currentScenePath = SceneManager.GetActiveScene().path;
if (!string.IsNullOrEmpty(currentScenePath))
{
EditorSceneManager.OpenScene(currentScenePath);
}
}
AssetDatabase.Refresh();
EditorUtility.RequestScriptReload();
}
[MenuItem(MenuPathToggle)]
public static void ToggleManualCompile()
{
IsEnabled = !IsEnabled;
Debug.Log($"[KF_FastCompile] Manual Compile Mode is now: {(IsEnabled ? "ENABLED" : "DISABLED")}");
}
[MenuItem(MenuPathToggle, true)]
public static bool ToggleManualCompileValidate()
{
Menu.SetChecked(MenuPathToggle, IsEnabled);
return true;
}
private static void ApplyState(bool enabled)
{
if (enabled)
{
Lock();
}
else
{
Unlock();
}
}
private static void Lock()
{
if (locked) return;
EditorApplication.LockReloadAssemblies();
locked = true;
}
private static void Unlock()
{
if (!locked) return;
EditorApplication.UnlockReloadAssemblies();
locked = false;
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (!IsEnabled) return;
if (state == PlayModeStateChange.ExitingEditMode)
{
Unlock();
AssetDatabase.Refresh();
}
else if (state == PlayModeStateChange.EnteredEditMode)
{
Lock();
}
}
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
if (IsEnabled)
{
Debug.Log("[KF_FastCompile] Disabling Manual Compile Mode for Build. Will re-enable after build completes.");
Unlock();
}
}
public void OnPostprocessBuild(BuildReport report)
{
if (IsEnabled)
{
Debug.Log("[KF_FastCompile] Build completed. Restoring Manual Compile Mode.");
Lock();
}
}
}