Hi guys I’m looking for something like “Selection.activeObject = object;” but to select a folder not an object
Thanks
Mike
Hi guys I’m looking for something like “Selection.activeObject = object;” but to select a folder not an object
Thanks
Mike
Thanks for the reply it put me on the right path
string path = "Assets/MyFolder";
// Check the path has no '/' at the end, if it dose remove it,
// Obviously in this example it doesn't but it might
// if your getting the path some other way.
if (path[path.Length -1] == '/')
path = path.Substring(0, path.Length -1);
// Load object
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));
// Select the object in the project folder
Selection.activeObject = obj;
// Also flash the folder yellow to highlight it
EditorGUIUtility.PingObject(obj);
This works perfectly in One Column Layout, but in Two Column Layout this selects the folder, but doesn’t enter it. So if I then trigger a menu item from the Create Menu it creates it next to the folder, but not inside the folder.
Does anyone know how to enter an empty folder via script?
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 });
AssetDatabase.OpenAsset(folderAssetObject);
Works just fine