Skip to content

Instantly share code, notes, and snippets.

@sassembla
Last active June 7, 2020 02:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sassembla/959f8e5459e2b3d80d5869ab89dac174 to your computer and use it in GitHub Desktop.
Save sassembla/959f8e5459e2b3d80d5869ab89dac174 to your computer and use it in GitHub Desktop.
/*
kill Unity Hub process continuously after UnityEditor is started.
they have no reason to keep living after starting UnityEditor.
To:Unity, please stop UnityHub process until user need to use that. and never use macOS's status bar. please add "quit Hub after UnityEditor startd" option.
or, please design more small UnityInstaller app and UnityProjectViewer app separately.
*/
using System.Diagnostics;
using UnityEditor;
[InitializeOnLoad]
public class HubANiceDay
{
static HubANiceDay()
{
#if !UNITY_EDITOR_OSX
// if not macOS, we do nothing. please write your platform's code for killing unnecessary UnityHub process.
return;
#endif
EditorApplication.CallbackFunction killAct = null;
killAct = () =>
{
var foundHubID = FindHubID();
if (!string.IsNullOrEmpty(foundHubID))
{
KillUnityHub(foundHubID);
// waiting the raise of another Unity Hub. We never allow them to live after UnityEditor running.
return;
}
// no hub found. stop hub detecting action finally.
if (killAct != null)
{
EditorApplication.update -= killAct;
}
};
EditorApplication.update += killAct;
}
private static string FindHubID()
{
var escapedArgs = "ps -ax | grep /Applications/Unity.Hub.app/Contents/MacOS/Unity.Hub";
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/zsh",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
var result = process.StandardOutput.ReadToEnd();
var lines = result.Split('\n');
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
if (line.Contains("/Applications/Unity Hub.app/Contents/MacOS/Unity Hub"))
{
var processIDAndOthers = line.Split(' ');
if (0 < processIDAndOthers.Length)
{
var processID = processIDAndOthers[0];
return processID;
}
}
}
return string.Empty;
// example result output.
// 58618 ?? 0:04.28 /Applications/Unity Hub.app/Contents/MacOS/Unity Hub -- --silent
// or
// 58618 ?? 0:04.28 /Applications/Unity Hub.app/Contents/MacOS/Unity Hub
}
private static void KillUnityHub(string code)
{
var escapedArgs = "kill -9 " + code;
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/zsh",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
process.StandardOutput.ReadToEnd();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment