static void CreateMenuItems() { var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.ExportedTypes). Where(t => typeof(ScriptableSettingsBase).IsAssignableFrom(t) && !t.IsAbstract). Select(t => (type: t, name: t.Name.Replace("Settings", ""))). ToArray(); var path = "Assets/Settings/MenuItems.cs"; if (!types.Any()) { if (AssetDatabase.LoadAssetAtPath <MonoScript>(path) is MonoScript script) { AssetDatabase.DeleteAsset(path); } return; } EditorFolderUtility.EnsureFolderExists("Assets/Settings"); using (var writer = new StreamWriter(path)) { writer.WriteLine("//Automatically generated class, any changes made will be overwritten."); writer.WriteLine(""); writer.WriteLine("#if UNITY_EDITOR"); writer.WriteLine(""); writer.WriteLine("using UnityEditor;"); writer.WriteLine(""); writer.WriteLine("namespace Common.Settings.MenuItems"); writer.WriteLine("{"); writer.WriteLine(""); writer.WriteLine(" ///<summary>Automatically generated class to provide menu items for each ScriptableSettings under 'Settings' menu item."); writer.WriteLine(" public static class SettingMenuItems"); writer.WriteLine(" {"); int i = 0; foreach (var(type, name) in types) { writer.WriteLine(""); writer.WriteLine($@" [MenuItem(""Settings/{name}"")]"); writer.WriteLine($@" public static void Item{i}() => ScriptableSettingsUtility.OpenInEditor(""Assets/Settings/Resources/Settings/{type.Name}.asset"");"); i += 1; } writer.WriteLine(""); writer.WriteLine(" }"); writer.WriteLine(""); writer.WriteLine("}"); writer.WriteLine("#endif"); } if (!AssetDatabase.LoadAssetAtPath <MonoScript>(path)) { AssetDatabase.Refresh(); } }
static T Create() { #if UNITY_EDITOR EditorFolderUtility.EnsureFolderExists("Assets/Settings/Resources/Settings"); var obj = CreateInstance <T>(); AssetDatabase.DeleteAsset("Assets/Settings/Resources/Settings/" + typeof(T).Name + ".asset"); AssetDatabase.CreateAsset(obj, "Assets/Settings/Resources/Settings/" + typeof(T).Name + ".asset"); return(obj); #else return(null); #endif }