UnityTerminal is a tool that allows you to start standard or administrator instance of Command Prompt
and/or the Powershell within Unity Editor.
You can also create and add your own System Diagnostic Processes to a CustomProcesses script that
can be executed within the editor via the Menubar.
This is a simple script that currently has 2 static methods.
StartProcess(string filename, bool runAsAdmin = false);
StartAdvProcess(string filename, string argument, bool hideWindow = false, bool runAsAdmin = false)
StartProcess - allows you to start any process without arguments as a standard user or administrator.
StartAdvProcess - allows you start any process with arguments as a standard user or administrator with additional options to hide the
console window.
The Windows Command Prompt Script contains methods that allow you to open a Command Prompt (CMD) or Powershell window from within Unity.
To open a Command Prompt window you can navigate to "System/Command Prompt" and select one of the CMD instances you wish to open.
Alternatively you can open a CMD instance with the "ctrl+shift+l" (standard instance) or
"ctrl+alt+l" (admin instance) keyboard shortcuts.
To open a Powershell window you can navigate to "System/Powershell" and select on of the Powershell instances you wish to open.
Alternatively you can open a Powershell instance with the "ctrl+shift+h" (standard instance) or
"ctrl+alt+h" (admin instance) keyboard shortcuts.
To create your own Custom Processes, create a Custom Processes Script where you code your own custom Processes.
Your custom processes can be used with the unity [MenuItem] attribute to allow the method to be called from within
the Unity Editor via the Menubar or Inspector.
To create a process in your Custom Process Script you need to do the following:
- Use one of the predefined methods (StartProcess or StartAdvProcess) in the ProcessUtilities class, or
- Code your own custom Process using System.Diagnostic namespace
Here are a few examples of creating your own custom processes.
// Starting a simple Notepad Process
public static async Task Notepad()
{
await Task.Run(() => ProcessUtilities.StartProcess("notepad.exe"));
}
// Starting an Advanced Process
public static async Task Echo()
{
await Task.Run(() =>
ProcessUtilities.StartAdvProcess("cmd.exe", "/k echo hello, world"));
}
// Creating your own Custom Advanced Process
public static async Task CustomProcess()
{
await Task.Run(() =>
{
using Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.Arguments = @"/k echo hello world";
process.Start();
process.StandardInput.WriteLine("echo hello");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Debug.Log(process.StandardOutput.ReadToEnd());
});
}
These examples below are examples of how I am currently using this Project and may not necessarily work for you.
[MenuItem("Window/Android/ADB/ADB (Start-Server)")]
public static async Task StartAdbServer()
{
await Task.Run(() => ProcessUtilities.StartAdvProcess("adb", @" start-server"));
}
[MenuItem("Window/Android/ADB/ADB (Kill-Server)")]
public static async Task KillAdbServer()
{
await Task.Run(() => ProcessUtilities.StartAdvProcess("adb", @" kill-server"));
}
I use the android emulator to test builds of my android games etc. It does not replace an actual device but it definitely works.
Additionally I enable "Developer Settings" on the emulator to receive android logs about the device and game.
[MenuItem("Window/Android/Emulator/Pixel_3")]
public static async Task OpenAndroidEmulator()
{
await Task.Run(() => ProcessUtilities.StartAdvProcess("emulator", @"-avd Pixel_3_API_30"));
}
The MenuItem attribute allows you to add menu items to the main menu and inspector context menus.
The MenuItem attribute turns any static function into a menu command. Only static functions can use the MenuItem attribute.
To use keyboard shortcuts with your custom processes, you can do the following:
To create a hotkey you can use the following special characters:
% (ctrl on Windows, cmd on macOS),
# (shift),
& (alt).
If no special modifier key combinations are required the key can be given after an underscore.
For example to create a menu with hotkey shift-alt-g use "MyMenu/Do Something #&g".
To create a menu with hotkey g and no key modifiers pressed use "MyMenu/Do Something _g".
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-6.0
https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo?view=net-6.0
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cmd