|
1 | 1 | using System.IO; |
2 | 2 | using System.Diagnostics; |
| 3 | +using System; |
3 | 4 |
|
4 | 5 | namespace UnityProjectLauncher |
5 | 6 | { |
6 | 7 | class Program |
7 | 8 | { |
8 | 9 | static void Main() |
9 | 10 | { |
10 | | - if (Directory.Exists(@".\Assets")) |
| 11 | + if (File.Exists(@".\ProjectSettings\ProjectVersion.txt")) |
11 | 12 | { |
12 | | - string[] paths = Directory.GetFiles(@".\Assets", "*.unity", SearchOption.AllDirectories); |
13 | | - if(paths.Length > 0) |
| 13 | + string[] lines = File.ReadAllLines(@".\ProjectSettings\ProjectVersion.txt"); |
| 14 | + string version = lines[0].Substring(lines[0].IndexOf(":") + 2); |
| 15 | + string changeset = lines[1].Substring(lines[1].IndexOf("(") + 1).Replace(")",""); |
| 16 | + |
| 17 | + string path = @$"C:\Program Files\Unity\Hub\Editor\{version}\Editor\"; |
| 18 | + Console.WriteLine(path); |
| 19 | + string argument = $"-projectPath \"{Directory.GetCurrentDirectory()}\"\\"; |
| 20 | + Console.WriteLine(argument); |
| 21 | + |
| 22 | + |
| 23 | + ProcessStartInfo processStartInfo = new ProcessStartInfo("Unity.exe", argument); |
| 24 | + processStartInfo.WorkingDirectory = path; |
| 25 | + processStartInfo.UseShellExecute = true; |
| 26 | + try |
14 | 27 | { |
15 | | - ProcessStartInfo processStartInfo = new ProcessStartInfo(paths[0]); |
16 | | - processStartInfo.UseShellExecute = true; |
17 | | - Process.Start(processStartInfo); |
| 28 | + Process process = Process.Start(processStartInfo); |
18 | 29 | } |
19 | | - else |
| 30 | + catch (Exception exception) |
20 | 31 | { |
21 | | - File.AppendAllLines("LauncherLog.txt", new string[]{ "Error: Could not find a scene file. Make sure your project does have a scene file within the Assets or one of its subfolders!"}); |
| 32 | + Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs"); |
| 33 | + File.AppendAllLines(Directory.GetCurrentDirectory() + "\\Logs\\LauncherLog.txt", new string[] { exception.Message, "If it can't find the file, it's most likely a missing unity version. Install the correct Unity Version and try again." }); |
| 34 | + |
| 35 | + ProcessStartInfo hubProcess = new ProcessStartInfo("Unity Hub.exe", $"-- --headless install --version {version} --changeset {changeset}"); |
| 36 | + hubProcess.WorkingDirectory = @$"C:\Program Files\Unity Hub\"; |
| 37 | + hubProcess.UseShellExecute = true; |
| 38 | + try |
| 39 | + { |
| 40 | + Process hub = Process.Start(hubProcess); |
| 41 | + hub.Exited += Hub_Exited; |
| 42 | + } |
| 43 | + catch (Exception) |
| 44 | + { |
| 45 | + Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs"); |
| 46 | + File.AppendAllLines(Directory.GetCurrentDirectory() + "\\Logs\\LauncherLog.txt", new string[] { exception.Message, "If it can't find the file, it's most likely a missing unity version. Install the correct Unity Version and try again." }); |
| 47 | + |
| 48 | + throw; |
| 49 | + } |
22 | 50 | } |
23 | 51 | } |
24 | 52 | else |
25 | 53 | { |
26 | | - File.AppendAllLines("LauncherLog.txt", new string[] { "Error: Could not find a scene file because there was no Asset folder present. Make sure this Executable is placed in a Unity Project Folder (Not inside the Asset folder)!" }); |
| 54 | + Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs"); |
| 55 | + File.AppendAllLines(Directory.GetCurrentDirectory() + "\\Logs\\LauncherLog.txt", new string[] { "Error: Could not find a scene file. Make sure your project does have a scene file within the Assets or one of its subfolders!" }); |
27 | 56 | } |
28 | 57 | } |
| 58 | + |
| 59 | + private static void Hub_Exited(object sender, EventArgs e) |
| 60 | + { |
| 61 | + Main(); |
| 62 | + } |
| 63 | + |
| 64 | + private static void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 65 | + { |
| 66 | + Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs"); |
| 67 | + File.AppendAllText(Directory.GetCurrentDirectory()+"\\Logs\\LauncherLog.txt", "\n"+e.Data); |
| 68 | + } |
29 | 69 | } |
30 | 70 | } |
0 commit comments