1
+ using System ;
2
+ using System . Diagnostics ;
3
+ using System . IO ;
4
+ using UnityEditor ;
5
+ using UnityEngine ;
6
+
7
+ public class QuickFixerEditor : EditorWindow
8
+ {
9
+ #region regions you need to be for adding your own editor
10
+
11
+ #region Editors
12
+
13
+ public struct EditorInfo
14
+ {
15
+ public string name ;
16
+ public string path ;
17
+ public string downloadLink ;
18
+ }
19
+
20
+ public enum EditorType
21
+ {
22
+ VisualStudioCode ,
23
+ NotepadPlusPlus
24
+ }
25
+
26
+ public static EditorInfo [ ] editors = new EditorInfo [ ]
27
+ {
28
+ new EditorInfo { name = "VS Code" , path = @"C:\Users\" + Environment . UserName + @"\AppData\Local\Programs\Microsoft VS Code\Code.exe" , downloadLink = "https://code.visualstudio.com/Download" } ,
29
+
30
+ new EditorInfo { name = "NotePad++" , path = @"C:\Program Files\Notepad++\notepad++.exe" , downloadLink = "https://notepad-plus-plus.org/downloads/" }
31
+ } ;
32
+
33
+ //These menu strings provide support for when you right click on a component in the Project window
34
+ //The context commands provide support when right clicking a monobehaviour component in the inspector window
35
+ const string OpenVSCodeMenu = "Assets/Open With VSCode" ;
36
+ const string openVSCodeContext = "CONTEXT/MonoBehaviour/Edit Script With VSCode" ;
37
+
38
+ const string OpenNotepadMenu = "Assets/Open With Notepad++" ;
39
+ const string openNotepadContext = "CONTEXT/MonoBehaviour/Edit Script With Notepad++" ;
40
+
41
+ private MonoScript scriptToEdit ;
42
+
43
+ #endregion
44
+
45
+
46
+ #region Menu Commands
47
+
48
+ //You can delete the editors you don't want to use here
49
+
50
+ #region VSCode
51
+ [ MenuItem ( OpenVSCodeMenu , false , 50 ) ]
52
+ private static void OpenWithVSCode ( )
53
+ {
54
+ OpenEditor ( EditorType . VisualStudioCode ) ;
55
+ }
56
+
57
+ [ MenuItem ( OpenVSCodeMenu , true ) ]
58
+ private static bool ValidateOpenWithVSCode ( )
59
+ {
60
+ return ValidateOpenWithPath ( EditorType . VisualStudioCode ) ;
61
+ }
62
+
63
+ [ MenuItem ( openVSCodeContext , false , priority = - 100 ) ]
64
+ private static void OpenWithVSCodeContext ( MenuCommand command )
65
+ {
66
+ OpenEditor ( EditorType . VisualStudioCode , command ) ;
67
+ }
68
+
69
+ [ MenuItem ( openVSCodeContext , true , priority = - 100 ) ]
70
+ private static bool ValidateOpenWithVSCodeContext ( )
71
+ {
72
+ return ValidateOpenWithContext ( EditorType . VisualStudioCode ) ;
73
+ }
74
+ #endregion
75
+
76
+ #region Notepad++
77
+
78
+ [ MenuItem ( OpenNotepadMenu , false , 50 ) ]
79
+ private static void OpenWithNotepad ( )
80
+ {
81
+ OpenEditor ( EditorType . NotepadPlusPlus ) ;
82
+ }
83
+
84
+ [ MenuItem ( OpenNotepadMenu , true ) ]
85
+ private static bool ValidateOpenWithNotepadPlusPlus ( )
86
+ {
87
+ return ValidateOpenWithPath ( EditorType . NotepadPlusPlus ) ;
88
+ }
89
+
90
+
91
+ [ MenuItem ( openNotepadContext , false , priority = - 100 ) ]
92
+ private static void OpenWithNotepadContext ( MenuCommand command )
93
+ {
94
+ OpenEditor ( EditorType . NotepadPlusPlus , command ) ;
95
+ }
96
+
97
+ [ MenuItem ( openNotepadContext , true , priority = - 100 ) ]
98
+ private static bool ValidateOpenWithNotepadContext ( )
99
+ {
100
+ return ValidateOpenWithContext ( EditorType . NotepadPlusPlus ) ;
101
+
102
+ }
103
+ #endregion
104
+
105
+ #endregion
106
+
107
+ #endregion
108
+
109
+
110
+
111
+ #region regions you shouldn't be for adding your own editor
112
+ #region validate functions
113
+
114
+
115
+ // Validates if the selected asset in the project window is a script
116
+ // and checks if the editor path exists.
117
+ // If one of those returns false, the button will be greyed out
118
+ private static bool ValidateOpenWithPath ( EditorType editorType )
119
+ {
120
+ if ( IsSelectedScript ( ) == false ) return false ;
121
+
122
+ EditorInfo editor = editors [ ( int ) editorType ] ;
123
+
124
+ bool editorExists = File . Exists ( editor . path . Replace ( "\" " , "" ) ) ;
125
+
126
+ if ( editorExists )
127
+ {
128
+ return true ;
129
+ }
130
+ else
131
+ {
132
+ SetMissingEditorPopup ( editorType ) ;
133
+ return false ;
134
+ }
135
+ }
136
+
137
+ private static bool ValidateOpenWithContext ( EditorType editorType )
138
+ {
139
+ EditorInfo editor = editors [ ( int ) editorType ] ;
140
+
141
+ bool editorExists = File . Exists ( editor . path . Replace ( "\" " , "" ) ) ;
142
+
143
+ if ( editorExists )
144
+ {
145
+ return true ;
146
+ }
147
+ else
148
+ {
149
+ SetMissingEditorPopup ( editorType ) ;
150
+ return false ;
151
+ }
152
+ }
153
+
154
+
155
+ private static bool IsSelectedScript ( )
156
+ {
157
+ return Selection . activeObject is MonoScript ;
158
+ }
159
+ #endregion
160
+
161
+
162
+ #region Open Editor functions and arguments
163
+ public static void OpenEditor ( EditorType editorType )
164
+ {
165
+ OpenActualEditor ( editorType , Selection . activeObject as MonoScript ) ;
166
+ }
167
+
168
+ public static void OpenEditor ( EditorType editorType , MonoScript scriptPath )
169
+ {
170
+ OpenActualEditor ( editorType , scriptPath ) ;
171
+ }
172
+
173
+ private static void OpenEditor ( EditorType editorType , MenuCommand command )
174
+ {
175
+ OpenActualEditor ( editorType , MonoScript . FromMonoBehaviour ( command . context as MonoBehaviour ) ) ;
176
+ }
177
+
178
+ private static void OpenActualEditor ( EditorType editorType , MonoScript scriptPath )
179
+ {
180
+ QuickFixerEditor window = EditorWindow . GetWindow < QuickFixerEditor > ( ) ;
181
+ window . scriptToEdit = scriptPath ;
182
+ window . OpenEditorType ( editorType ) ;
183
+ window . Close ( ) ;
184
+ }
185
+
186
+ public void OpenEditorType ( EditorType editorType )
187
+ {
188
+ string scriptPath = AssetDatabase . GetAssetPath ( scriptToEdit ) ;
189
+ EditorInfo editor = editors [ ( int ) editorType ] ;
190
+
191
+ // Added quotes around the script path so The process sees it as a single argument in case there are spaces in the path
192
+ Process . Start ( editor . path , $ "\" { scriptPath } \" ") ;
193
+ }
194
+
195
+ #endregion
196
+
197
+
198
+ #region GUI
199
+
200
+ public static void ShowWindow ( )
201
+ {
202
+ GetWindow ( typeof ( QuickFixerEditor ) ) ;
203
+ }
204
+ private void OnGUI ( )
205
+ {
206
+ Rect popupRect = new ( position . x + 20f , position . y + 60f , 260f , 100f ) ;
207
+
208
+ EditorWindow popup = GetWindowWithRect < EditorWindow > ( popupRect , true , "Popup" ) ;
209
+ popup . titleContent = new GUIContent ( "Quick Fixer" ) ;
210
+ popup . ShowPopup ( ) ;
211
+
212
+ GUIContent missingEditorContent = new ( missingEditorInfo ) ;
213
+
214
+ GUILayout . Label ( missingEditorContent , EditorStyles . boldLabel ) ;
215
+
216
+ GUILayout . Space ( 5 ) ;
217
+
218
+ GUIContent linkContent = new ( infoURL ) ;
219
+
220
+ if ( GUILayout . Button ( linkContent , EditorStyles . linkLabel ) )
221
+ {
222
+ Application . OpenURL ( linkURL ) ;
223
+ popup . Close ( ) ;
224
+ }
225
+
226
+ if ( GUILayout . Button ( "Don't show these popups again" ) )
227
+ {
228
+ EditorPrefs . SetBool ( "ShowQuickFixerPopupOption" , false ) ;
229
+ UnityEngine . Debug . Log ( "<b>QuickFixer</b> Popups won't show up anymore. In case you'd like to see them again, you can go to preferences -> QuickFixer." ) ;
230
+ popup . Close ( ) ;
231
+ }
232
+ }
233
+ #endregion
234
+
235
+
236
+ #region Missing editor popups
237
+ private static string missingEditorInfo ;
238
+ private static string linkURL ;
239
+ private static string infoURL ;
240
+
241
+ private static void SetMissingEditorPopup ( EditorType editorType )
242
+ {
243
+ //In Preferences you can set this to true in case you'd like to get a popup with the missing script editor
244
+
245
+ if ( QuickFixerEditorSettingsProvider . ShowMissingEditorPopup == false ) return ;
246
+
247
+ EditorInfo editor = editors [ ( int ) editorType ] ;
248
+
249
+ missingEditorInfo = "It seems you don't have " + editor . name + " installed" ;
250
+ infoURL = "Click me to go to the " + editor . name + " download page" ;
251
+ linkURL = editor . downloadLink ;
252
+
253
+ QuickFixerEditor window = EditorWindow . GetWindow < QuickFixerEditor > ( ) ;
254
+ window . Show ( ) ;
255
+ }
256
+ #endregion
257
+ #endregion
258
+ }
0 commit comments