Using editor code I would like to focus a certain folder in the Project window.
I know how to create editor buttons, menu items etc but I have not found a way to focus a folder in project window. I have focused assets, which take me to a certain folder. But if a folder is empty is there a way to focus it?
There should be a way because the Favorites system seems to use that.
I modified it a little bit. It is also important to call EditorUtility.FocusProjectWindow(); before Selection.activeObject. I did not need to call EditorGUIUtility.PingObject(obj);
Okay, never mind. While it does focus, for some reason when I add a new folder path to focus (using editor buttons), I have to click the editor button twice. With first click it takes me to one of the previous folders I focused (but I think the selection is correct), and with the second click the correct folder is focused in the project view (project view navigates to the correct path).
using UnityEngine;
using UnityEditor;
public class MenuItems {
[MenuItem("Favourites/Trees")]
private static void SelectFolder()
{
EditorUtility.FocusProjectWindow();
Object obj = AssetDatabase.LoadAssetAtPath<Object>("Assets/Scripts/Player/Okay");
Selection.activeObject = obj;
}
}
In my use case, I have an EditorWindow system and a collection of objects in a scriptable object asset I call a Datastore.
One of the properties of items in the datastore might be called âModelâ,
GUILayout.BeginHorizontal();
{
selectedItem.Model = (GameObject)EditorGUILayout.ObjectField("Model", selectedItem.Model, typeof(GameObject, true));
if (GUILayout.Button("Locate Item"))
{
EditorUtility.FocusProjectWindow();
Selection.activeObject = selectedItem.Model;
}
}
GUILayout.EndHorizontal();
``` This works perfectly fine if you need to use it. The key is, as you've said
'EditorUtility.FocusProjectWindow();
Selection.activeObject = selectedItem.Model;'
here's a pic:
![7166392--858304--pic.PNG|3840x2084](upload://oOfTfX4vI4RB2IkJw2bsg1XkS8o.png)
I know this is a really old thread, but Iâve been working on a templated script generation system within my custom editor and this thread came up first on google a lot, and I found a solution to this issue from another personâs (rorocoâs) solution posted on another thread that needed a little change to work for direct copy/pasting: http://answers.unity.com/answers/1415446/view.html
Their method uses the following code that finds the last interacted Project Browser and calls the ShowFolderContents function on the Project Browser instance, which selects and shows that folder in that Project Browser window. This ensures that if you have a two column layout or even multiple Project Browser windows open at once, the latest interacted window will correctly select the folder you wish. To ensure this works, make sure to also call EditorUtility.FocusProjectWindow() to handle issues with the Project Browser being hidden behind other windows that share the same docked area, such as when you are on the Console Log when calling this system. In my instance, I can then call my desired function after this âProjectWindowUtil.CreateScriptAssetFromTemplateFile(templatePath, template.defaultFileName);â and that all works out for me no matter which window is currently in focus. Just make sure the path you send in doesnât have a slash at the end and you should be good
Here is the functioning code:
string path = "Assets/DesiredFolderPath"
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));
EditorUtility.FocusProjectWindow();
var pt = Type.GetType("UnityEditor.ProjectBrowser,UnityEditor");
var ins = pt.GetField("s_LastInteractedProjectBrowser", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public).GetValue(null);
var showDirMeth = pt.GetMethod("ShowFolderContents", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
showDirMeth.Invoke(ins, new object[ ] { obj.GetInstanceID(), true });