|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +using UnityEditor; |
| 7 | +using UnityEditor.ShortcutManagement; |
| 8 | + |
| 9 | +namespace Chimp.Editor |
| 10 | +{ |
| 11 | + public static class ToggleProjectBrowser |
| 12 | + { |
| 13 | + // editor pref prefix |
| 14 | + private const string _prefix = "Chimp_ProjectBrowser"; |
| 15 | + |
| 16 | + // editor prefs |
| 17 | + private const string _positionX = _prefix + "PositionX"; |
| 18 | + private const string _positionY = _prefix + "PositionY"; |
| 19 | + private const string _width = _prefix + "Width"; |
| 20 | + private const string _height = _prefix + "Height"; |
| 21 | + |
| 22 | + private static Rect s_defaultPosition = new(0.0f, 0.0f, 200.0f, 200.0f); |
| 23 | + private static bool s_toggled; |
| 24 | + |
| 25 | + [Shortcut( |
| 26 | + id: "Chimp_ToggleProjectWindow", |
| 27 | + context: null, |
| 28 | + defaultKeyCode: KeyCode.Space, |
| 29 | + defaultShortcutModifiers: ShortcutModifiers.Control, |
| 30 | + displayName = "Toggle Project Window" |
| 31 | + )] |
| 32 | + public static void ToggleProjectWindow() |
| 33 | + { |
| 34 | + // "UnityEditor.ProjectBrowser" is internal class, reflection required |
| 35 | + Type type = Assembly.GetAssembly(typeof(UnityEditor.Editor)).GetType("UnityEditor.ProjectBrowser"); |
| 36 | + EditorWindow projectBrowser = EditorWindow.GetWindow(type, false, "Project", false); |
| 37 | + |
| 38 | + if (s_toggled) |
| 39 | + { |
| 40 | + SaveProjectBrowserPosition(projectBrowser); |
| 41 | + projectBrowser.Close(); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + projectBrowser.position = GetProjectBrowserPosition(); |
| 46 | + projectBrowser.Show(); |
| 47 | + projectBrowser.Repaint(); |
| 48 | + } |
| 49 | + |
| 50 | + s_toggled = !s_toggled; |
| 51 | + } |
| 52 | + |
| 53 | + private static void SaveProjectBrowserPosition(EditorWindow projectBrowser) |
| 54 | + { |
| 55 | + EditorPrefs.SetFloat(_positionX, projectBrowser.position.x); |
| 56 | + EditorPrefs.SetFloat(_positionY, projectBrowser.position.y); |
| 57 | + EditorPrefs.SetFloat(_width, projectBrowser.position.width); |
| 58 | + EditorPrefs.SetFloat(_height, projectBrowser.position.height); |
| 59 | + } |
| 60 | + |
| 61 | + private static Rect GetProjectBrowserPosition() |
| 62 | + { |
| 63 | + float x = !EditorPrefs.HasKey(_positionX) ? s_defaultPosition.x : EditorPrefs.GetFloat(_positionX); |
| 64 | + float y = !EditorPrefs.HasKey(_positionY) ? s_defaultPosition.y : EditorPrefs.GetFloat(_positionY); |
| 65 | + float width = !EditorPrefs.HasKey(_width) ? s_defaultPosition.width : EditorPrefs.GetFloat(_width); |
| 66 | + float height = !EditorPrefs.HasKey(_height) ? s_defaultPosition.height : EditorPrefs.GetFloat(_height); |
| 67 | + |
| 68 | + return new(x, y, width, height); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments