Skip to content

Commit 6b0e5ba

Browse files
committed
add shortcut for toggling project browser
1 parent 97ffaba commit 6b0e5ba

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Editor/Shortcuts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
}

Editor/Shortcuts/ToggleProjectBrowser.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.kovanci.chimpeditor",
3-
"version": "1.0.6",
3+
"version": "1.0.7",
44
"displayName": "Chimp Editor",
55
"description": "Unity3D Editor Utilities",
66
"unity": "2022.3",
@@ -15,4 +15,4 @@
1515
"url": "https://emrekovanci.github.io"
1616
},
1717
"repository": "github:kovanci/Package_Unity3D_ChimpEditor"
18-
}
18+
}

0 commit comments

Comments
 (0)