private static void AddPreset() { BuildSettingsPreset preset = BuildSettingsPreset.FromCurrentSettings(); string dirname = GetCurrentDirectory(preset); if (dirname != RootFolder) { dirname = Path.Combine(Directory.GetParent(dirname).ToString(), "Presets"); } // Don't overwrite an existing preset string path; int suffix = 0; do { string fileName = suffix == 0 ? DefaultName : DefaultName + "(" + suffix + ")"; path = Path.Combine(dirname, fileName + ".asset"); ++suffix; } while (File.Exists(path)); AssetDatabase.CreateAsset(preset, path); Selection.activeObject = preset; RefreshPresetsList(); }
public static BuildSettingsPreset FromCurrentSettings() { BuildSettingsPreset preset = CreateInstance <BuildSettingsPreset>(); preset.OverwriteWithCurrentBuildSettings(); return(preset); }
private static string GetCurrentDirectory(BuildSettingsPreset preset = null) { if (preset == null) { preset = ScriptableObject.CreateInstance <BuildSettingsPreset>(); } return(Path.GetDirectoryName(AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(preset))) ?? RootFolder); }
public static void RefreshPresetsList() { // Get all the presets assets string[] guids = AssetDatabase.FindAssets("t:" + nameof(BuildSettingsPreset)); Dictionary <string, string> presets = new Dictionary <string, string>(); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); BuildSettingsPreset preset = AssetDatabase.LoadAssetAtPath <BuildSettingsPreset>(path); presets.Add(guid, preset.name); } // Don't refresh if nothing changed if (!presets.Except(BuildSettingsPresetsMenuItems.Presets).Any() && !BuildSettingsPresetsMenuItems.Presets.Except(presets).Any()) { return; } string scriptPath = Path.Combine(Directory.GetCurrentDirectory(), GetCurrentDirectory(), nameof(BuildSettingsPresetsMenuItems) + ".cs"); StreamReader reader = File.OpenText(scriptPath); string newFileContent = ""; string presetsGenerated = String.Join("", presets.Select(entry => { string presetGuid = entry.Key; string presetName = entry.Value; return("\t\t\t{\"" + presetGuid + "\", \"" + presetName + "\"},\n"); })); newFileContent = InsertInRegion(reader, newFileContent, presetsGenerated, "GeneratedPresets"); string menuItemsGenerated = String.Join("\n", presets.Select(entry => { string presetGuid = entry.Key; string presetName = entry.Value; return("\t\t[MenuItem(\"Build presets/" + presetName + "\")]\n" + "\t\tpublic static void Apply" + presetGuid + "()\n" + "\t\t{\n" + "\t\t\t" + typeof(BuildSettingsPresetsManager).Name + "." + nameof(ApplyPreset) + "(\"" + presetGuid + "\");\n" + "\t\t}\n"); })); newFileContent = InsertInRegion(reader, newFileContent, menuItemsGenerated, "GeneratedMenuItems"); // Read the rest of the file string str; while ((str = reader.ReadLine()) != null) { newFileContent += str + "\n"; } reader.Close(); File.WriteAllText(scriptPath, newFileContent); AssetDatabase.Refresh(); }