Advertisement
Guest User

unity browser navigation

a guest
May 4th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | Source Code | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.IO;
  5.  
  6. public class ProjectViewNavigationExtension : EditorWindow
  7. {
  8. private static Stack<string> backHistory = new Stack<string>();
  9. private static Stack<string> forwardHistory = new Stack<string>();
  10. private static string currentPath = "Assets";
  11. private string displayPath = "Assets";
  12.  
  13. [MenuItem("Window/Custom/Project View Navigation")]
  14. public static void ShowWindow()
  15. {
  16. GetWindow<ProjectViewNavigationExtension>("Project Navigation");
  17. }
  18.  
  19. private void OnEnable()
  20. {
  21. // Initialize with current project view folder
  22. currentPath = GetCurrentProjectViewPath();
  23. EditorApplication.projectWindowItemOnGUI += ProjectWindowItemCallback;
  24. }
  25.  
  26. private void OnDisable()
  27. {
  28. EditorApplication.projectWindowItemOnGUI -= ProjectWindowItemCallback;
  29. }
  30.  
  31. private void OnGUI()
  32. {
  33. EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
  34.  
  35. // Back button
  36. GUI.enabled = backHistory.Count > 0;
  37. if (GUILayout.Button("←", EditorStyles.toolbarButton, GUILayout.Width(30)))
  38. {
  39. GoBack();
  40. }
  41.  
  42. // Forward button
  43. GUI.enabled = forwardHistory.Count > 0;
  44. if (GUILayout.Button("→", EditorStyles.toolbarButton, GUILayout.Width(30)))
  45. {
  46. GoForward();
  47. }
  48.  
  49. // Up button
  50. GUI.enabled = currentPath != "Assets";
  51. if (GUILayout.Button("↑", EditorStyles.toolbarButton, GUILayout.Width(30)))
  52. {
  53. GoUp();
  54. }
  55.  
  56. GUI.enabled = true;
  57.  
  58. // Path field
  59. EditorGUI.BeginChangeCheck();
  60. displayPath = EditorGUILayout.TextField(displayPath, EditorStyles.toolbarTextField);
  61. if (EditorGUI.EndChangeCheck() && Event.current.keyCode == KeyCode.Return)
  62. {
  63. NavigateToPath(displayPath);
  64. }
  65.  
  66. EditorGUILayout.EndHorizontal();
  67. }
  68.  
  69. private void ProjectWindowItemCallback(string guid, Rect selectionRect)
  70. {
  71. // This callback allows us to detect when the user navigates in the project view
  72. if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
  73. {
  74. string clickedPath = AssetDatabase.GUIDToAssetPath(guid);
  75. if (Directory.Exists(clickedPath) && clickedPath != currentPath)
  76. {
  77. // Add a small delay to ensure the click is processed by Unity first
  78. EditorApplication.delayCall += () => {
  79. AddToHistory(currentPath);
  80. currentPath = clickedPath;
  81. displayPath = clickedPath;
  82. forwardHistory.Clear();
  83. };
  84. }
  85. }
  86. }
  87.  
  88. private string GetCurrentProjectViewPath()
  89. {
  90. // Try to get the current folder in project view
  91. // This is a bit hacky as Unity doesn't expose this directly
  92. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
  93. if (selection.Length > 0)
  94. {
  95. string path = AssetDatabase.GetAssetPath(selection[0]);
  96. if (Directory.Exists(path))
  97. return path;
  98. else if (File.Exists(path))
  99. return Path.GetDirectoryName(path).Replace('\\', '/');
  100. }
  101. return "Assets";
  102. }
  103.  
  104. private void GoBack()
  105. {
  106. if (backHistory.Count > 0)
  107. {
  108. forwardHistory.Push(currentPath);
  109. currentPath = backHistory.Pop();
  110. displayPath = currentPath;
  111. SelectFolder(currentPath);
  112. }
  113. }
  114.  
  115. private void GoForward()
  116. {
  117. if (forwardHistory.Count > 0)
  118. {
  119. backHistory.Push(currentPath);
  120. currentPath = forwardHistory.Pop();
  121. displayPath = currentPath;
  122. SelectFolder(currentPath);
  123. }
  124. }
  125.  
  126. private void GoUp()
  127. {
  128. if (currentPath != "Assets")
  129. {
  130. string parentPath = Path.GetDirectoryName(currentPath).Replace('\\', '/');
  131. if (string.IsNullOrEmpty(parentPath))
  132. parentPath = "Assets";
  133.  
  134. AddToHistory(currentPath);
  135. currentPath = parentPath;
  136. displayPath = parentPath;
  137. forwardHistory.Clear();
  138. SelectFolder(parentPath);
  139. }
  140. }
  141.  
  142. private void NavigateToPath(string path)
  143. {
  144. // Ensure path starts with Assets
  145. if (!path.StartsWith("Assets"))
  146. path = "Assets/" + path;
  147.  
  148. if (Directory.Exists(path) && path != currentPath)
  149. {
  150. AddToHistory(currentPath);
  151. currentPath = path;
  152. displayPath = path;
  153. forwardHistory.Clear();
  154. SelectFolder(path);
  155. }
  156. }
  157.  
  158. private void AddToHistory(string path)
  159. {
  160. backHistory.Push(path);
  161. }
  162.  
  163. private void SelectFolder(string folderPath)
  164. {
  165. // Find the folder asset
  166. Object folder = AssetDatabase.LoadAssetAtPath<Object>(folderPath);
  167. if (folder != null)
  168. {
  169. // Select and ping the folder in project view
  170. Selection.activeObject = folder;
  171. EditorGUIUtility.PingObject(folder);
  172.  
  173. // Try to navigate to the folder in project view
  174. EditorUtility.FocusProjectWindow();
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement