Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Flavelius/07f3b9fec08c5508b8ca1d7f9337549a to your computer and use it in GitHub Desktop.
Save Flavelius/07f3b9fec08c5508b8ca1d7f9337549a to your computer and use it in GitHub Desktop.
Unity Editor - Create folders from enum
#if UNITY_EDITOR
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
class EnumFolderCreator: EditorWindow
{
[MenuItem("Edit/CreateEnumFolders")]
static void Open()
{
GetWindow<EnumFolderCreator>(true);
}
Type[] enumTypes;
string[] enumTypeNames;
int selectedIndex = 0;
void OnEnable()
{
enumTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsEnum).ToArray();
enumTypeNames = new string[enumTypes.Length];
for (int i = 0; i < enumTypes.Length; i++)
{
enumTypeNames[i] = enumTypes[i].Name;
}
}
void OnGUI()
{
if (enumTypes.Length == 0)
{
EditorGUILayout.HelpBox("No enums found", MessageType.Info);
return;
}
selectedIndex = EditorGUILayout.Popup("Select Enum", selectedIndex, enumTypeNames);
var enumNames = Enum.GetNames(enumTypes[selectedIndex]);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.HelpBox("Folders created:", MessageType.None);
for (int i = 0; i < enumNames.Length; i++)
{
EditorGUILayout.LabelField(enumNames[i]);
}
EditorGUILayout.EndVertical();
if (GUILayout.Button("Select parent and create"))
{
var path = EditorUtility.OpenFolderPanel("Select Parent folder", "Assets", "");
if (path.StartsWith(Application.dataPath))
{
path = path.Replace(Application.dataPath, "Assets");
}
if (!string.IsNullOrEmpty(path))
{
for (int i = 0; i < enumNames.Length; i++)
{
AssetDatabase.CreateFolder(path, enumNames[i]);
}
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment