Post navigation

How to nicely select item in the Unity’s two-column Project Browser

Hey, I’ll try to be short.
I’m working on the Maintainer‘s Issues Finder improvements and one of such improvement was to select (show) the scene file at the Project Browser along with target GameObject in the Hierarchy when user press “Show” next to the found issue.

At the first iteration, I did the selection using Selection.objects and it worked great with One-column view mode of the Project Browser.
But things get weird when I switched to the two-column mode: target scene file still get selected, but it didn’t show up until I manually navigated to the folder with that file.

And surprisingly, I faced two common issues here:
1. Unity has no API to let you know in which mode the Project Browser is.
2. Unity has no API to select specific folder in the first column of the Project Browser.

But thankfully, we have ILSpy and reflection! 😉
So, using ILSpy I managed to find all what I need;
– UnityEditor.ProjectBrowser class
– static s_LastInteractedProjectBrowser field to access last used ProjectBrowser window (we can have lot of ProjectBrowsers in Unity layout).
– private field m_ViewMode of the ViewMode enum type which has two values – OneColumn (0) and TwoColumns(1)
– private ShowFolderContents() method which expands first column of the TreeView to the specified folder and selects it

And here is a resulting code which selects both object in scene hierarchy and scene file in Project Browser in any column mode (one or two columns):

This code might be not elegant and clean since it’s a quick try to leave snippet in the blog. Feel free to correct my where I’m wrong.

P.S.: if you wish only show some file in the Project Browser without selection, just use EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(scenePath, typeof(Object))); It will work fine with one-column layout and will work with two-column layout if you don’t select anything else.

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!