- Home /
Can you change the current folder of the project view via script?
Seems like a simple enough question. If I have a folder for prefabs like "Assets/Resources/Prefabs/Area/Tiles" and a folder for textures like "Assets/Textures/Area/Tiles" I'd like to make an editor script that can jump back and forth between them. I know how to do all that stuff except for actually changing the project view's folder. I looked at the ProjectWindowUtil class and there doesn't seem to be anything helpful there, so if there's not a way to do this is there any way I can request it? Thanks.
I know this is old and answered, but I came across it when searching for how to change the project browser's folder from code. The provided answer relies on knowing specific assets within the folder, which I didn't like, so I used reflection and Unity's internal methods to show the folder. See my thread here, if anyone is interested: https://forum.unity.com/threads/tutorial-how-to-to-show-specific-folder-content-in-the-project-window-via-editor-scripting.508247/
Answer by dustxx · Jan 26, 2015 at 07:04 AM
I solved my own problem. You can't directly change the project view, but if you change the "Selection.activeObject" to an asset in a folder, the project view will jump to the folder in which that asset is stored. In my case, I simply used the path of the current "Selection.activeObject" to replace the needed parts to find the asset I wanted and set that to the selection. Simple enough!
//Draw the 'Switch Selection Between Tile and Texture' tool
private void DrawSwitchSelectionBetweenTileAndTextureTool()
{
if ( GUILayout.Button("Switch Selection Between Tile and Texture") )
{
Object mirroredAsset = GetMirroringTileOrTexture(Selection.activeObject);
if ( mirroredAsset != null ) { Selection.activeObject = mirroredAsset; }
else { CancelOperation("Could not find mirroring asset"); }
}
}
//Get the Tile mirroring a texture or the texture mirroring a tile
private Object GetMirroringTileOrTexture(Object asset)
{
string activeSelectionPath = AssetDatabase.GetAssetPath(asset);
string mirroredActiveSelectionPath = null;
//Convert the path
if ( activeSelectionPath.Contains("Resources/Prefabs/Overworld") )
{
mirroredActiveSelectionPath = new StringBuilder(activeSelectionPath).Replace("Resources/Prefabs/Overworld", "Textures").Replace(Constants.PREFAB_EXTENSION, Constants.PNG_EXTENSION).ToString();
}
else if ( activeSelectionPath.Contains("Textures") )
{
mirroredActiveSelectionPath = new StringBuilder(activeSelectionPath).Replace("Textures", "Resources/Prefabs/Overworld").Replace(Constants.PNG_EXTENSION, Constants.PREFAB_EXTENSION).ToString();
}
return AssetDatabase.LoadAssetAtPath(mirroredActiveSelectionPath, typeof(Object));
}
Plus 1 for co$$anonymous$$g back and providing the solution :)
Answer by Hauthorn · Jul 31, 2019 at 12:36 AM
For people looking for exactly what is needed to do this,
EditorUtility.FocusProjectWindow();
Selection.activeObject = myObject;
Your answer
Follow this Question
Related Questions
Project backup save 3 Answers
Moving from Project to Hierarchy 3 Answers
Which folders in a Unity project are critical folders? 4 Answers
Unity Folder way too big - bigger than inside Files 1 Answer
Custom editor script for folders? 1 Answer