- Home /
Create a dropdown with folder depth structure in Editor
I want to create a tool which will traverse as shown in below.
Dropdown1 - {should have level 1 directories, on selection this should generate a dropdown2 with the Second level folders and files}
Dropdown2 - {This will only get generate based on above parent selected value and will generate if there are any subfolders or files present in it.}
This should happen in Editor not in runtime.
and so on, this dynamic EditorGUILayout.PopUp generation need to happen until i reach a file in depth, If any intermediate Folder selection is been changed then respctive dropdown's should be updated.
I am not able to think of any solution, any leads/Hints will be helpful.
Regards, Jithendra.
Just a clarification: folders are inside the "Project" pane showing all folders under the "Assets" folder. This is what you want to traverse at runtime? Or maybe you are looking at the "tree structure" in the "Hierarchy" pane, and you refer to that hierarchy as "folders"? Because these are not folders, they are GameObjects in the scene, presented as a tree structure in the Hierarchy pane.
Yeah something like in Project Pane but it should be populated as dropdowns, each level of directory may have mor than one folder so i want to display them in a dropdown so that user can easily traverse. I have achieved this solution in Windows Forms but not able to find a way to implement in Editor Scripts.
Sorry but i can't really follow your description. I have no idea what the final thing should look like. A single drop down menu is a linear list of menu items and can optionally have sub menus, however you should keep the nesting of menu items small. Do you want a dropdown with submenus for the whole file structure? That would be extremely inefficient. Also keep in $$anonymous$$d that a folder could contain many (many) other folders or files. It's very impractical to show such lists as dropdowns. You see the problem when you try to show a dropdown for $$anonymous$$eyCode. $$anonymous$$ost the time the whole dropdown doesn't fit onto the screen.
Finally your question is confusing. You know that "EditorGUILayout.PopUp" is a pure editor feature which can not be used at runtime. Also that the asset folder and it's structure doesn't exist at runtime in a build. You should be more clear what you want to achieve and where (runtime or editor). $$anonymous$$aybe have some schemaric / drawing of what you want it to look like?
I have edited the question for better understanding, each level in the folder structure should depect by a dropdown in editor.
Please let me know if you have any questions further.
Answer by stektpotet · Aug 04, 2018 at 03:53 PM
I'm not entirely sure if this is quite what you wanted, the EditorGUILayout.Popup function, I find is a bit limited, so I instead took use of GenericMenus. Looks mostly the same when used:
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System.Linq;
public class ProjectAssetDropdowns: ScriptableWizard
{
[MenuItem("Tools/Testing")]
static void CreateWizard()
{
DisplayWizard<ProjectAssetDropDowns>("Testing...");
}
private int _selected = -1;
public string SelectedDisplay
{
get
{
if(_selected < 0 || _selected >= paths.Length)
{
return "NOTHING SELECTED";
}
return Selected.Split(new char[] { '/', '\\' }).Last();
}
}
public string Selected
{
get
{
if (_selected < 0 || _selected >= paths.Length)
{
return string.Empty;
}
return paths[_selected];
}
}
string[] paths;
private void OnFocus()
{
paths = AssetDatabase.GetAllAssetPaths();
}
protected override bool DrawWizardGUI()
{
if (GUILayout.Button(SelectedDisplay))
{
GenericMenu dropdown = new GenericMenu();
for (int i = 0; i < paths.Length; i++)
{
if (paths[i].StartsWith("Assets/"))
{
dropdown.AddItem(
//Add the assetpath minus the "Asset/"-part
new GUIContent(paths[i].Remove(0, 7)),
//show the currently selected item as selected
i == _selected,
//lambda to set the selected item to the one being clicked
selectedIndex => _selected = (int)selectedIndex,
//index of this menu item, passed on to the lambda when pressed.
i
);
}
}
dropdown.ShowAsContext(); //finally show the dropdown
}
if (_selected >= 0 && _selected < paths.Length)
{
Object selectedAsset = AssetDatabase.LoadAssetAtPath(Selected, typeof(Object));
Texture t = AssetPreview.GetAssetPreview(selectedAsset);
if(t != null)
GUI.DrawTexture(GUILayoutUtility.GetRect(200, 200), t);
}
//Do stuff with the selected item
return (_selected >= 0 && _selected < paths.Length);
}
}
#endif
Here's how it looks in the end:
Your answer
