Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-06-01 18:23:19 +08:00
parent 948d50cab4
commit 9260e46b3c
7 changed files with 1396 additions and 27 deletions

View File

@ -12,13 +12,14 @@ public class PicoControllerUdpSender : MonoBehaviour
private const string PortPrefsKey = "xr_rm_udp_sender.port";
private const string SendHzPrefsKey = "xr_rm_udp_sender.send_hz";
private const string ConvertPrefsKey = "xr_rm_udp_sender.convert_coordinates";
private const int MaxPacketsPerFrame = 4;
[Header("ROS UDP Target")]
public string host = "192.168.9.99";
public int port = 15000;
[Header("Send")]
public float sendHz = 60.0f;
public float sendHz = 90.0f;
public bool convertUnityToProjectCoordinates = true;
public bool sendOnStart = true;
@ -106,17 +107,24 @@ public class PicoControllerUdpSender : MonoBehaviour
return;
}
if (Time.unscaledTime < nextSendTime)
{
return;
}
nextSendTime = Time.unscaledTime + 1.0f / Mathf.Max(sendHz, 1.0f);
packet.t = Time.realtimeSinceStartupAsDouble;
FillController(XRNode.LeftHand, packet.controllers.left);
FillController(XRNode.RightHand, packet.controllers.right);
SendPacket();
float now = Time.unscaledTime;
float interval = 1.0f / Mathf.Max(sendHz, 1.0f);
if (nextSendTime <= 0.0f || now - nextSendTime > interval * MaxPacketsPerFrame)
{
nextSendTime = now;
}
int sentThisFrame = 0;
while (now >= nextSendTime && sentThisFrame < MaxPacketsPerFrame)
{
packet.t = Time.realtimeSinceStartupAsDouble;
SendPacket();
nextSendTime += interval;
sentThisFrame++;
}
}
public bool ApplyConfig(string newHost, int newPort, float newSendHz, bool newConvertUnityToProjectCoordinates, bool save)

View File

@ -3,6 +3,11 @@ using System.Globalization;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
#if PICO_OPENXR_SDK
using Unity.XR.OpenXR.Features.PICOSupport;
#else
using Unity.XR.PXR;
#endif
public class PicoUdpConfigPanel : MonoBehaviour
{
@ -90,6 +95,29 @@ public class PicoUdpConfigPanel : MonoBehaviour
QualitySettings.vSyncCount = 0;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
XRSettings.eyeTextureResolutionScale = 1.0f;
RequestPicoDisplayRefreshRate();
}
private void RequestPicoDisplayRefreshRate()
{
#if UNITY_ANDROID && !UNITY_EDITOR
try
{
#if PICO_OPENXR_SDK
bool applied = DisplayRefreshRateFeature.SetDisplayRefreshRate((float)targetFrameRate);
if (!applied)
{
Debug.LogWarning($"XR-RM failed to request PICO display refresh rate: {targetFrameRate} Hz");
}
#else
PXR_System.SetSystemDisplayFrequency((float)targetFrameRate);
#endif
}
catch (Exception exception)
{
Debug.LogWarning($"XR-RM failed to request PICO display refresh rate: {exception.Message}");
}
#endif
}
private void EnsurePanel()