UDP command logic

This commit is contained in:
tom.hempel
2025-10-15 10:30:16 +02:00
parent f0ca7263d4
commit bf79495ba2
6 changed files with 172 additions and 9 deletions

BIN
Unity/Assets/Scenes/VR/VR -Task2.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR -Task3.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR -Task4.unity (Stored with Git LFS)

Binary file not shown.

BIN
Unity/Assets/Scenes/VR/VR-Task1.unity (Stored with Git LFS)

Binary file not shown.

View File

@ -1,10 +1,42 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class TaskButtonManager : MonoBehaviour
{
[Header("UDP Settings")]
[SerializeField] private int udpPort = 5555;
[SerializeField] private bool enableUDPControl = true;
private UdpClient udpClient;
private Thread udpListenerThread;
private bool isListening = false;
private string pendingSceneName = null;
private readonly object sceneLock = new object();
private void Start()
{
if (enableUDPControl)
{
StartUDPListener();
}
}
private void Update()
{
// Check for pending UDP scene changes (must be done on main thread)
lock (sceneLock)
{
if (!string.IsNullOrEmpty(pendingSceneName))
{
LoadSceneByName(pendingSceneName);
pendingSceneName = null;
}
}
// VR Tasks (Keys 1-4)
if (Input.GetKeyDown(KeyCode.Alpha1) || Input.GetKeyDown(KeyCode.Keypad1))
LoadSceneByName("VR-Task1");
@ -30,9 +62,101 @@ public class TaskButtonManager : MonoBehaviour
LoadSceneByName("Lobby");
}
private void StartUDPListener()
{
try
{
isListening = true;
udpClient = new UdpClient(udpPort);
udpListenerThread = new Thread(new ThreadStart(ListenForUDPCommands));
udpListenerThread.IsBackground = true;
udpListenerThread.Start();
Debug.Log($"UDP Listener started on port {udpPort}");
}
catch (System.Exception e)
{
Debug.LogError($"Failed to start UDP listener: {e.Message}");
}
}
private void ListenForUDPCommands()
{
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, udpPort);
while (isListening)
{
try
{
byte[] data = udpClient.Receive(ref remoteEndPoint);
string message = Encoding.UTF8.GetString(data);
ProcessUDPCommand(message);
}
catch (System.Exception e)
{
if (isListening)
{
Debug.LogError($"UDP receive error: {e.Message}");
}
}
}
}
private void ProcessUDPCommand(string message)
{
Debug.Log($"Received UDP command: {message}");
if (string.IsNullOrEmpty(message))
return;
// Parse command format: SWITCH:SceneName
string[] parts = message.Split(':');
if (parts.Length == 2 && parts[0].Trim().ToUpper() == "SWITCH")
{
string sceneName = parts[1].Trim();
lock (sceneLock)
{
pendingSceneName = sceneName;
}
Debug.Log($"Scheduled scene switch to: {sceneName}");
}
else
{
Debug.LogWarning($"Invalid UDP command format: {message}. Expected format: SWITCH:SceneName");
}
}
private void LoadSceneByName(string sceneName)
{
Debug.Log($"Loading scene: {sceneName}");
SceneManager.LoadScene(sceneName);
}
private void OnDestroy()
{
StopUDPListener();
}
private void OnApplicationQuit()
{
StopUDPListener();
}
private void StopUDPListener()
{
isListening = false;
if (udpClient != null)
{
udpClient.Close();
udpClient = null;
}
if (udpListenerThread != null && udpListenerThread.IsAlive)
{
udpListenerThread.Abort();
udpListenerThread = null;
}
Debug.Log("UDP Listener stopped");
}
}