ServiceDemo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using Nxr.Internal;
  3. using UnityEngine;
  4. namespace NXR.Samples
  5. {
  6. public class ServiceDemo : MonoBehaviour
  7. {
  8. NibiruService nibiruService;
  9. // Use this for initialization
  10. TextMesh textMesh_DriverVersion;
  11. TextMesh textMesh_VendorVersion;
  12. TextMesh textMesh_Light;
  13. TextMesh textMesh_Proximity;
  14. TextMesh textMesh_Brightness;
  15. TextMesh textMesh_SensorData;
  16. void Start()
  17. {
  18. nibiruService = NxrViewer.Instance.GetNibiruService();
  19. if (nibiruService == null)
  20. {
  21. Debug.Log("nibiruService is null >>>>>>>>>>>>>>>>>>");
  22. }
  23. NibiruService.OnSensorDataChangedHandler += onSensorDataChanged;
  24. Debug.Log("----------------nibiruService is Start----------------");
  25. }
  26. void onSensorDataChanged(NibiruSensorEvent sensorEvent)
  27. {
  28. // sensorEvent.printLog();
  29. if (textMesh_SensorData != null)
  30. {
  31. textMesh_SensorData.text = sensorEvent.sensorType.ToString() + "\nx="
  32. + sensorEvent.x + ",\ny=" + sensorEvent.y + ",\nz=" + sensorEvent.z + "\ntimestamp=" + sensorEvent.timestamp;
  33. }
  34. }
  35. bool updateOnce = false;
  36. int updateCount = 0;
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. updateCount++;
  41. if (textMesh_SensorData == null)
  42. {
  43. GameObject Obj = GameObject.Find("TextSensorData");
  44. if (Obj != null)
  45. {
  46. textMesh_SensorData = Obj.GetComponent<TextMesh>();
  47. Debug.Log("find TextSensorData");
  48. return;
  49. }
  50. }
  51. if (textMesh_DriverVersion == null)
  52. {
  53. GameObject Obj = GameObject.Find("DriverVersion");
  54. if (Obj != null)
  55. {
  56. textMesh_DriverVersion = Obj.GetComponent<TextMesh>();
  57. Debug.Log("find DriverVersion");
  58. return;
  59. }
  60. }
  61. if (textMesh_VendorVersion == null)
  62. {
  63. GameObject Obj = GameObject.Find("VendorSwVersion");
  64. if (Obj != null)
  65. {
  66. textMesh_VendorVersion = Obj.GetComponent<TextMesh>();
  67. Debug.Log("find VendorSwVersion");
  68. return;
  69. }
  70. }
  71. if (textMesh_Light == null)
  72. {
  73. GameObject Obj = GameObject.Find("LightValue");
  74. if (Obj != null)
  75. {
  76. textMesh_Light = Obj.GetComponent<TextMesh>();
  77. Debug.Log("find LightValue");
  78. return;
  79. }
  80. }
  81. if (textMesh_Proximity == null)
  82. {
  83. GameObject Obj = GameObject.Find("ProximityValue");
  84. if (Obj != null)
  85. {
  86. textMesh_Proximity = Obj.GetComponent<TextMesh>();
  87. Debug.Log("find ProximityValue");
  88. return;
  89. }
  90. }
  91. if (textMesh_Brightness == null)
  92. {
  93. GameObject Obj = GameObject.Find("BrightnessValue");
  94. if (Obj != null)
  95. {
  96. textMesh_Brightness = Obj.GetComponent<TextMesh>();
  97. Debug.Log("find BrightnessValue");
  98. return;
  99. }
  100. }
  101. if (nibiruService != null && !updateOnce)
  102. {
  103. // Avoid frequent calls
  104. updateOnce = true;
  105. Debug.Log("----------------------------------------Service-------------");
  106. textMesh_DriverVersion.text = "Driver board software version:" + nibiruService.GetVendorSWVersion();
  107. textMesh_VendorVersion.text = "System version:" + nibiruService.GetModel() + "," + nibiruService.GetOSVersion();
  108. textMesh_Brightness.text = "Screen brightness:" + nibiruService.GetBrightnessValue();
  109. textMesh_Light.text = "Light sensor:" + nibiruService.GetLightValue();
  110. textMesh_Proximity.text = "Distance sensor:" + nibiruService.GetProximityValue();
  111. }
  112. // 1s1次
  113. if (updateCount > 60 && textMesh_Brightness != null && nibiruService != null)
  114. {
  115. updateCount = 0;
  116. textMesh_Brightness.text = "Screen brightness:" + nibiruService.GetBrightnessValue();
  117. }
  118. }
  119. private void OnDestroy()
  120. {
  121. if (nibiruService != null)
  122. {
  123. nibiruService.UnRegisterSensorListener();
  124. }
  125. }
  126. }
  127. }