Skip to content

Commit 2b75bbd

Browse files
added option to save unity project selection across sessions
1 parent 07dd9f1 commit 2b75bbd

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

UnityHubNative.Net/MainWindow.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MainWindow : Window
3030
static TextBox s_openInTerminalFormatText;
3131
static Key s_lastKey;
3232
private static TabControl s_tabControl;
33+
private static bool _updatingUnityProjectList;
3334

3435
public MainWindow(object data)
3536
{
@@ -107,6 +108,8 @@ public static void ReloadEverything()
107108
UpdateUnityVersionViews();
108109
UpdateUnitySearchPathViews();
109110
UpdateUnityProjectViews();
111+
if (UnityHubNativeNetApp.Config.saveProjectSelection)
112+
LoadProjectSelectedIndex();
110113
}
111114

112115
public static void OpenSelectedProjectWith()
@@ -154,10 +157,12 @@ public static void UpdateUnitySearchPathViews()
154157

155158
public static void UpdateUnityProjectViews()
156159
{
160+
_updatingUnityProjectList = true;
157161
SyncListBoxWithView<UnityProject, UnityProjectView>(s_unityProjectsParent, UnityHubUtils.UnityProjects);
158162

159163
for (int i = 0; i < UnityHubUtils.UnityProjects.Count; i++)
160164
((UnityProjectView)s_unityProjectsParent.Items[i]!).Update(UnityHubUtils.UnityProjects[i]);
165+
_updatingUnityProjectList = false;
161166
}
162167

163168
public static void MoveSelectedProjectUp()
@@ -591,6 +596,25 @@ public static void MoveSelectedProjectDown()
591596
]).SetDock(Dock.Top).SetTooltip("Whether or not to close the app after opening project in terminal"),
592597
])
593598
}.SetTooltip("Defines the process format of when opening a project in terminal. {path} will be replaced by the project path"),
599+
new SettingsExpanderItem
600+
{
601+
Content = new DockPanel
602+
{
603+
LastChildFill = false
604+
}.AddChildren
605+
([
606+
new TextBlock
607+
{
608+
Text = "Remember Unity Project selection",
609+
VerticalAlignment = VerticalAlignment.Center,
610+
}.SetDock(Dock.Left),
611+
new CheckBox
612+
{
613+
IsChecked = UnityHubNativeNetApp.Config.saveProjectSelection,
614+
VerticalAlignment = VerticalAlignment.Center,
615+
}.OnCheckChanged(OnRememberProjectSelectionChanged).SetDock(Dock.Right)
616+
])
617+
}.SetTooltip("If checked, the last selected Unity Project will be kept across sessions"),
594618
])
595619
])
596620
}
@@ -599,6 +623,14 @@ public static void MoveSelectedProjectDown()
599623
])
600624
]);
601625

626+
static void OnRememberProjectSelectionChanged()
627+
{
628+
UnityHubNativeNetApp.Config.saveProjectSelection = !UnityHubNativeNetApp.Config.saveProjectSelection;
629+
UnityHubNativeNetApp.SaveConfig(UnityHubNativeNetApp.Config);
630+
if (UnityHubNativeNetApp.Config.saveProjectSelection)
631+
SaveProjectSelectedIndex();
632+
}
633+
602634
static void OnCloseAfterOpenInTerminalChanged()
603635
{
604636
UnityHubNativeNetApp.Config.closeAfterOpenInTerminal = !UnityHubNativeNetApp.Config.closeAfterOpenInTerminal;
@@ -742,11 +774,14 @@ static void UnityInstallationSearchPathSelectedIndexChanged()
742774

743775
static void UnityProjectSelectedIndexChanged()
744776
{
745-
var index = GetUnityProjectSelectedIndex();
746-
Debug.WriteLine($"selection changed to {index}");
747-
bool isAnySelected = IsAnyProjectSelected();
777+
if (!_updatingUnityProjectList && UnityHubNativeNetApp.Config.saveProjectSelection)
778+
SaveProjectSelectedIndex();
748779
}
749780

781+
static void SaveProjectSelectedIndex() => File.WriteAllText(Paths.SelectedProject, GetUnityProjectSelectedIndex().ToString());
782+
783+
static void LoadProjectSelectedIndex() => s_unityProjectsParent.SelectedIndex = File.Exists(Paths.SelectedProject) && int.TryParse(File.ReadAllText(Paths.SelectedProject), out var result) ? result : 0;
784+
750785
static async void AddNewUnitySearchPath()
751786
{
752787
try

UnityHubNative.Net/Paths.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ static class Paths
88
public static readonly string SearchPathsPath = Path.Combine(Dir, "editorPaths.txt");
99
public static readonly string ProjectPathsPath = Path.Combine(Dir, "projects.txt");
1010
public static readonly string ConfigPath = Path.Combine(Dir, "config.txt");
11+
public static readonly string SelectedProject = Path.Combine(Dir, "selected.txt");
1112

1213
static Paths()
1314
{

UnityHubNative.Net/UnityHubNativeNetApp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static AppConfig LoadConfig()
5656
extendToTitlebar = txt.Length >= 5 && txt[4] == "true",
5757
openInTerminalFormat = txt.Length >= 6 ? txt[5] : "cmd.exe /K cd /d \"{path}\"",
5858
closeAfterOpenInTerminal = txt.Length >= 7 && txt[6] == "true",
59+
saveProjectSelection = txt.Length >= 8 && txt[7] == "true",
5960
};
6061
}
6162
catch (Exception ex)
@@ -76,6 +77,7 @@ public static void SaveConfig(AppConfig config)
7677
config.extendToTitlebar ? "true" : "false",
7778
config.openInTerminalFormat,
7879
config.closeAfterOpenInTerminal ? "true" : "false",
80+
config.saveProjectSelection ? "true" : "false",
7981
]);
8082
}
8183

@@ -88,5 +90,6 @@ public struct AppConfig
8890
public bool closeAfterProjectOpen;
8991
public string openInTerminalFormat;
9092
public bool closeAfterOpenInTerminal;
93+
public bool saveProjectSelection;
9194
}
9295
}

0 commit comments

Comments
 (0)