Skip to content

Commit e84b003

Browse files
committed
Changed to a more robust way using the Hub as fallback if a version isn't installed
1 parent 6951021 commit e84b003

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

UnityProjectLauncher/Program.cs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,70 @@
11
using System.IO;
22
using System.Diagnostics;
3+
using System;
34

45
namespace UnityProjectLauncher
56
{
67
class Program
78
{
89
static void Main()
910
{
10-
if (Directory.Exists(@".\Assets"))
11+
if (File.Exists(@".\ProjectSettings\ProjectVersion.txt"))
1112
{
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
1427
{
15-
ProcessStartInfo processStartInfo = new ProcessStartInfo(paths[0]);
16-
processStartInfo.UseShellExecute = true;
17-
Process.Start(processStartInfo);
28+
Process process = Process.Start(processStartInfo);
1829
}
19-
else
30+
catch (Exception exception)
2031
{
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+
}
2250
}
2351
}
2452
else
2553
{
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!" });
2756
}
2857
}
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+
}
2969
}
3070
}

UnityProjectLauncher/UnityProjectLauncher.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0-windows</TargetFramework>
66
<ApplicationIcon>unityplayer64.ico</ApplicationIcon>
77
<Authors>Michael Meier</Authors>
88
<Company>Meier-Digital</Company>
@@ -11,6 +11,7 @@
1111
<PackageProjectUrl>https://www.meier-digital.com</PackageProjectUrl>
1212
<PackageIcon>UnityPlayerIcon.png</PackageIcon>
1313
<SignAssembly>true</SignAssembly>
14+
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
1415
</PropertyGroup>
1516

1617
<ItemGroup>

0 commit comments

Comments
 (0)