SvrEventMonitor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using System.Runtime.InteropServices;
  5. namespace Ximmerse.XR.Utils
  6. {
  7. public class SvrEventMonitor : MonoBehaviour
  8. {
  9. public enum svrThermalLevel
  10. {
  11. kSafe,
  12. kLevel1,
  13. kLevel2,
  14. kLevel3,
  15. kCritical,
  16. kNumThermalLevels
  17. };
  18. public enum svrThermalZone
  19. {
  20. kCpu,
  21. kGpu,
  22. kSkin,
  23. kNumThermalZones
  24. };
  25. public struct svrEventData_Thermal
  26. {
  27. public svrThermalZone zone; //!< Thermal zone
  28. public svrThermalLevel level; //!< Indication of the current zone thermal level
  29. };
  30. public struct svrEventData_Proximity
  31. {
  32. public float distance; //!< Proximity value in cm
  33. };
  34. [StructLayout(LayoutKind.Explicit)]
  35. public struct svrEventData
  36. {
  37. [FieldOffset(0)]
  38. public svrEventData_Thermal thermal;
  39. [FieldOffset(0)]
  40. public svrEventData_Proximity proximity;
  41. [FieldOffset(0)]
  42. public Int64 data;
  43. }
  44. public enum svrEventType
  45. {
  46. kEventNone = 0,
  47. kEventSdkServiceStarting = 1,
  48. kEventSdkServiceStarted = 2,
  49. kEventSdkServiceStopped = 3,
  50. kEventControllerConnecting = 4,
  51. kEventControllerConnected = 5,
  52. kEventControllerDisconnected = 6,
  53. kEventThermal = 7,
  54. kEventVrModeStarted = 8,
  55. kEventVrModeStopping = 9,
  56. kEventVrModeStopped = 10,
  57. kEventSensorError = 11,
  58. kEventMagnometerUncalibrated = 12,
  59. kEventBoundarySystemCollision = 13,
  60. kEvent6dofRelocation = 14,
  61. kEvent6dofWarningFeatureCount = 15,
  62. kEvent6dofWarningLowLight = 16,
  63. kEvent6dofWarningBrightLight = 17,
  64. kEvent6dofWarningCameraCalibration = 18,
  65. kEventProximity = 19,
  66. kEvent6dofLowQuality = 40
  67. };
  68. public struct SvrEvent
  69. {
  70. public svrEventType eventType; //!< Type of event
  71. public uint deviceId; //!< An identifier for the device that generated the event (0 == HMD)
  72. public float eventTimeStamp; //!< Time stamp for the event in seconds since the last svrBeginVr call
  73. public svrEventData eventData; //!< Event specific data payload
  74. };
  75. private void Start()
  76. {
  77. }
  78. svrEventData eventData = new svrEventData();
  79. float timeWarning = 0;
  80. bool warningEnable = false;
  81. public void Inspect(Text warningText, float keepTime)
  82. {
  83. uint deviceId = 0;
  84. float eventTimeStamp = 0;
  85. int dataCount = Marshal.SizeOf(eventData) / sizeof(uint);
  86. uint[] dataBuffer = new uint[dataCount];
  87. int eventType = 0;
  88. #if !UNITY_EDITOR
  89. bool isEvent = SvrPluginAndroid.SvrPollEvent(ref eventType, ref deviceId, ref eventTimeStamp, dataCount, dataBuffer);
  90. if (isEvent)
  91. {
  92. switch ((svrEventType)(eventType))
  93. {
  94. case svrEventType.kEvent6dofLowQuality:
  95. {
  96. if (warningText != null)
  97. {
  98. //warningText.enabled = true;
  99. warningText.color = Color.red;
  100. warningText.text = string.Format("定位异常,请检查环境纹理和亮度..");
  101. }
  102. timeWarning = keepTime;
  103. warningEnable = true;
  104. };
  105. break;
  106. }
  107. }
  108. if (warningEnable)
  109. {
  110. timeWarning -= Time.deltaTime;
  111. if (timeWarning <= 0)
  112. {
  113. if (warningText != null)
  114. {
  115. //warningText.enabled = false;
  116. warningText.color = Color.green;
  117. warningText.text = "";
  118. }
  119. warningEnable = false;
  120. }
  121. }
  122. #endif
  123. }
  124. }
  125. }