Can i make a gui button in editor script that open a specific folder in assets folder in Project Window ?
The only solution not involving reflection is to ping an object in the folder. I came up with two ways to do this.
Ping only the folder. The Project window will not show the contents of the folder.
If you know an asset path inside the folder, you can instead ping that to make the Project window show the contents of the folder.
string folderPath = "Assets/MyFolder";
var obj = AssetDatabase.LoadAssetAtPath<DefaultAsset>(folderPath);
EditorGUIUtility.PingObject(obj);
Automatically find an asset inside the folder and ping that. The Project window will show the contents of the folder if it’s not empty.
public static void PingFolderOrFirstAsset(string folderPath)
{
string path = GetFirstAssetPathInFolder(folderPath);
if (string.IsNullOrEmpty(path))
path = folderPath;
var obj = AssetDatabase.LoadAssetAtPath<Object>(path);
EditorGUIUtility.PingObject(obj);
}
public static string GetFirstAssetPathInFolder(string folder, bool includeFolders = true)
{
if (includeFolders)
{
string path = GetFirstValidAssetPath(System.IO.Directory.GetDirectories(folder));
if (path != null)
return path;
}
return GetFirstValidAssetPath(System.IO.Directory.GetFiles(folder));
}
private static string GetFirstValidAssetPath(string[] paths)
{
for (int i = 0; i < paths.Length; ++i)
{
if (!string.IsNullOrEmpty(AssetDatabase.AssetPathToGUID(paths*)))*
_ return paths*;_
_ }_
_ return null;_
_}*_
Here’s a method using reflection to call ProjectBrowser.ShowFolderContents. Tested in Unity 5.6. This will not work if the Project window is set to one column layout.
string folderPath = “Assets/MyFolder”;
var obj = AssetDatabase.LoadAssetAtPath(folderPath);
var projectBrowserType = typeof(UnityEditor.Editor).Assembly.GetType(“UnityEditor.ProjectBrowser”);
var projectBrowser = EditorWindow.GetWindow(projectBrowserType);
//private void ShowFolderContents(int folderInstanceID, bool revealAndFrameInFolderTree)
var ShowFolderContents = projectBrowserType.GetMethod(“ShowFolderContents”, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, new System.Type[] { typeof(int), typeof(bool) }, null);
ShowFolderContents.Invoke(projectBrowser, new object[] { obj.GetInstanceID(), true });