Created
May 24, 2022 13:10
-
-
Save Broxzier/ae4c0e67af7e89ce8d062d960cacfe13 to your computer and use it in GitHub Desktop.
Unity Multi-Project Selection Wizard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if UNITY_EDITOR | |
using System; | |
using System.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEngine; | |
public class ProjectSelectWizard : ScriptableWizard | |
{ | |
private static readonly string ProjectsFolder = "Projects"; | |
private static readonly string CurrentProjectFile = "CurrentProject.txt"; | |
private static readonly string ProjectFilesFilter = "*.asset"; | |
private static ProjectSelectWizard wizard = null; | |
private string[] projectFolders = null; | |
private string currentProject; | |
private int selectionIndex; | |
private ProjectSelectWizard() | |
{ | |
// Build list of available projects (subfolders in the Projects folder) | |
string projectsFolder = Path.Combine(Directory.GetCurrentDirectory(), ProjectsFolder); | |
projectFolders = Directory.GetDirectories(projectsFolder).Select(path => Path.GetFileName(path)).ToArray(); | |
// Read current project from file | |
string currentProjectFile = Path.Combine(projectsFolder, CurrentProjectFile); | |
currentProject = File.Exists(currentProjectFile) ? File.ReadAllText(currentProjectFile) : "none"; | |
selectionIndex = Array.IndexOf(projectFolders, currentProject); | |
} | |
private void SwitchProject(string projectFolderName) | |
{ | |
string unityProjectFolder = Path.Combine(Directory.GetCurrentDirectory(), "ProjectSettings"); | |
string currentProjectFolder = Path.Combine(Directory.GetCurrentDirectory(), ProjectsFolder, projectFolderName); | |
string backupFolder = Path.Combine(Directory.GetCurrentDirectory(), ProjectsFolder, "Backup"); | |
string currentProjectFile = Path.Combine(Directory.GetCurrentDirectory(), ProjectsFolder, CurrentProjectFile); | |
// Backup current project settings, to prevent losing uncommited changes | |
// Ensure the directory exists before copying | |
Directory.CreateDirectory(backupFolder); | |
// Clear the backup folder | |
foreach (string file in Directory.GetFiles(backupFolder)) | |
{ | |
File.Delete(file); | |
} | |
// Copy current project settings to backup folder | |
foreach (string file in Directory.GetFiles(unityProjectFolder)) | |
{ | |
File.Copy(file, Path.Combine(backupFolder, Path.GetFileName(file))); | |
} | |
// Copy and overwrite project setting files to the ProjectSettings folder | |
foreach (string file in Directory.GetFiles(currentProjectFolder, ProjectFilesFilter)) | |
{ | |
File.Copy(file, Path.Combine(unityProjectFolder, Path.GetFileName(file)), true); | |
} | |
// Update current project name in file | |
File.WriteAllText(currentProjectFile, projectFolderName); | |
} | |
[MenuItem("Broxzier/Select Project", priority = 30)] | |
public static void OpenProjectSelectionWizard() | |
{ | |
if (wizard == null) | |
{ | |
wizard = DisplayWizard<ProjectSelectWizard>("Select Project"); | |
} | |
} | |
private void OnGUI() | |
{ | |
GUILayout.Label($"Current project: {currentProject}", EditorStyles.largeLabel); | |
GUILayout.Label("Select a project", EditorStyles.boldLabel); | |
selectionIndex = GUILayout.SelectionGrid(selectionIndex, projectFolders, 1, EditorStyles.radioButton); | |
if (selectionIndex != -1) | |
{ | |
GUILayout.FlexibleSpace(); | |
if (GUILayout.Button($"Switch to: {projectFolders[selectionIndex]}")) | |
{ | |
SwitchProject(projectFolders[selectionIndex]); | |
wizard.Close(); | |
} | |
} | |
} | |
private void OnDestroy() | |
{ | |
wizard = null; | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment