Skip to content

Instantly share code, notes, and snippets.

@hareendranmg
Created October 3, 2024 07:41
Show Gist options
  • Save hareendranmg/5d327d11fe05c5940e6ee93b1e434359 to your computer and use it in GitHub Desktop.
Save hareendranmg/5d327d11fe05c5940e6ee93b1e434359 to your computer and use it in GitHub Desktop.
C# program to control unity game with arrow keys
using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;
using System.Diagnostics;
class UnityController
{
private static Process unityProcess;
private static string unityExePath = @"C:\Users\TACHLOG-SW-4\Desktop\build\My project.exe";
static void Main(string[] args)
{
Console.WriteLine("Unity Cube Controller");
Console.WriteLine("Launching Unity application...");
LaunchUnityApplication();
Console.WriteLine("Use arrow keys to move the cube. Press 'Q' to quit.");
using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "UnityControlPipe", PipeDirection.Out))
{
try
{
Console.WriteLine("Connecting to Unity...");
pipeClient.Connect(10000); // 10 second timeout, increased to allow for Unity startup
Console.WriteLine("Connected to Unity. You can now control the cube.");
using (StreamWriter writer = new StreamWriter(pipeClient))
{
while (true)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true).Key;
string command = "";
switch (key)
{
case ConsoleKey.UpArrow:
command = "UP";
break;
case ConsoleKey.DownArrow:
command = "DOWN";
break;
case ConsoleKey.LeftArrow:
command = "LEFT";
break;
case ConsoleKey.RightArrow:
command = "RIGHT";
break;
case ConsoleKey.Q:
Console.WriteLine("Exiting...");
CloseUnityApplication();
return;
}
if (!string.IsNullOrEmpty(command))
{
SendCommand(writer, command);
}
}
Thread.Sleep(10); // Small delay to reduce CPU usage
}
}
}
catch (Exception e)
{
Debug.WriteLine($"Error: {e.GetType().Name} - {e.Message}");
}
finally
{
CloseUnityApplication();
}
}
}
static void SendCommand(StreamWriter writer, string command)
{
try
{
writer.WriteLine(command);
writer.Flush();
Console.WriteLine($"Sent command: {command}");
}
catch (Exception e)
{
Debug.WriteLine($"Error sending command: {e.Message}");
}
}
static void LaunchUnityApplication()
{
try
{
unityProcess = new Process();
unityProcess.StartInfo.FileName = unityExePath;
unityProcess.StartInfo.UseShellExecute = true;
unityProcess.Start();
Console.WriteLine("Unity application launched successfully.");
// Give Unity some time to start up
Thread.Sleep(5000); // Wait for 5 seconds
}
catch (Exception e)
{
Debug.WriteLine($"Error launching Unity application: {e.Message}");
Environment.Exit(1);
}
}
static void CloseUnityApplication()
{
if (unityProcess != null && !unityProcess.HasExited)
{
try
{
unityProcess.CloseMainWindow();
unityProcess.WaitForExit(5000); // Wait up to 5 seconds for the process to exit
if (!unityProcess.HasExited)
{
unityProcess.Kill(); // Force close if it doesn't exit normally
}
Console.WriteLine("Unity application closed.");
}
catch (Exception e)
{
Debug.WriteLine($"Error closing Unity application: {e.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment