Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Collections.Generic;
- using System.IO;
- public class ProjectViewNavigationExtension : EditorWindow
- {
- private static Stack<string> backHistory = new Stack<string>();
- private static Stack<string> forwardHistory = new Stack<string>();
- private static string currentPath = "Assets";
- private string displayPath = "Assets";
- [MenuItem("Window/Custom/Project View Navigation")]
- public static void ShowWindow()
- {
- GetWindow<ProjectViewNavigationExtension>("Project Navigation");
- }
- private void OnEnable()
- {
- // Initialize with current project view folder
- currentPath = GetCurrentProjectViewPath();
- EditorApplication.projectWindowItemOnGUI += ProjectWindowItemCallback;
- }
- private void OnDisable()
- {
- EditorApplication.projectWindowItemOnGUI -= ProjectWindowItemCallback;
- }
- private void OnGUI()
- {
- EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
- // Back button
- GUI.enabled = backHistory.Count > 0;
- if (GUILayout.Button("←", EditorStyles.toolbarButton, GUILayout.Width(30)))
- {
- GoBack();
- }
- // Forward button
- GUI.enabled = forwardHistory.Count > 0;
- if (GUILayout.Button("→", EditorStyles.toolbarButton, GUILayout.Width(30)))
- {
- GoForward();
- }
- // Up button
- GUI.enabled = currentPath != "Assets";
- if (GUILayout.Button("↑", EditorStyles.toolbarButton, GUILayout.Width(30)))
- {
- GoUp();
- }
- GUI.enabled = true;
- // Path field
- EditorGUI.BeginChangeCheck();
- displayPath = EditorGUILayout.TextField(displayPath, EditorStyles.toolbarTextField);
- if (EditorGUI.EndChangeCheck() && Event.current.keyCode == KeyCode.Return)
- {
- NavigateToPath(displayPath);
- }
- EditorGUILayout.EndHorizontal();
- }
- private void ProjectWindowItemCallback(string guid, Rect selectionRect)
- {
- // This callback allows us to detect when the user navigates in the project view
- if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
- {
- string clickedPath = AssetDatabase.GUIDToAssetPath(guid);
- if (Directory.Exists(clickedPath) && clickedPath != currentPath)
- {
- // Add a small delay to ensure the click is processed by Unity first
- EditorApplication.delayCall += () => {
- AddToHistory(currentPath);
- currentPath = clickedPath;
- displayPath = clickedPath;
- forwardHistory.Clear();
- };
- }
- }
- }
- private string GetCurrentProjectViewPath()
- {
- // Try to get the current folder in project view
- // This is a bit hacky as Unity doesn't expose this directly
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
- if (selection.Length > 0)
- {
- string path = AssetDatabase.GetAssetPath(selection[0]);
- if (Directory.Exists(path))
- return path;
- else if (File.Exists(path))
- return Path.GetDirectoryName(path).Replace('\\', '/');
- }
- return "Assets";
- }
- private void GoBack()
- {
- if (backHistory.Count > 0)
- {
- forwardHistory.Push(currentPath);
- currentPath = backHistory.Pop();
- displayPath = currentPath;
- SelectFolder(currentPath);
- }
- }
- private void GoForward()
- {
- if (forwardHistory.Count > 0)
- {
- backHistory.Push(currentPath);
- currentPath = forwardHistory.Pop();
- displayPath = currentPath;
- SelectFolder(currentPath);
- }
- }
- private void GoUp()
- {
- if (currentPath != "Assets")
- {
- string parentPath = Path.GetDirectoryName(currentPath).Replace('\\', '/');
- if (string.IsNullOrEmpty(parentPath))
- parentPath = "Assets";
- AddToHistory(currentPath);
- currentPath = parentPath;
- displayPath = parentPath;
- forwardHistory.Clear();
- SelectFolder(parentPath);
- }
- }
- private void NavigateToPath(string path)
- {
- // Ensure path starts with Assets
- if (!path.StartsWith("Assets"))
- path = "Assets/" + path;
- if (Directory.Exists(path) && path != currentPath)
- {
- AddToHistory(currentPath);
- currentPath = path;
- displayPath = path;
- forwardHistory.Clear();
- SelectFolder(path);
- }
- }
- private void AddToHistory(string path)
- {
- backHistory.Push(path);
- }
- private void SelectFolder(string folderPath)
- {
- // Find the folder asset
- Object folder = AssetDatabase.LoadAssetAtPath<Object>(folderPath);
- if (folder != null)
- {
- // Select and ping the folder in project view
- Selection.activeObject = folder;
- EditorGUIUtility.PingObject(folder);
- // Try to navigate to the folder in project view
- EditorUtility.FocusProjectWindow();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement