DeviceBatteryView.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UnityEngine.SceneManagement;
  7. /* 设备的电池显示界面(右上角) */
  8. public class DeviceBatteryView : MonoBehaviour
  9. {
  10. public static DeviceBatteryView ins;
  11. public Text labelTemperature;
  12. public int batteryValue;
  13. public int batteryDeviceID;
  14. void Start()
  15. {
  16. if (ins) {
  17. Destroy(this.gameObject);
  18. } else {
  19. ins = this;
  20. DontDestroyOnLoad(this.gameObject);
  21. }
  22. }
  23. float countingTime1 = 0;
  24. float countingTime2 = 1;
  25. void Update()
  26. {
  27. if (countingTime1 < 5) {
  28. countingTime1 += Time.deltaTime;
  29. } else {
  30. countingTime1 = 0;
  31. RequestBatteryForBow();
  32. RequestBatteryForArrow();
  33. }
  34. if (countingTime2 < 1) {
  35. countingTime2 += Time.deltaTime;
  36. } else {
  37. countingTime2 = 0;
  38. bool activeBow = BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  39. bool activeArrow = BluetoothShoot.ins && BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess;
  40. if (!activeBow) {
  41. this.transform.Find("Layout/Label1").gameObject.SetActive(false);
  42. this.transform.Find("Layout/Frame1").gameObject.SetActive(false);
  43. }
  44. if (!activeArrow) {
  45. this.transform.Find("Layout/Label2").gameObject.SetActive(false);
  46. this.transform.Find("Layout/Frame2").gameObject.SetActive(false);
  47. }
  48. }
  49. }
  50. void RequestBatteryForBow()
  51. {
  52. try {
  53. if (BluetoothAim.ins.hasData && JCUnityLib.TimeUtils.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000) {
  54. BluetoothAim.ins.RequestBattery();
  55. }
  56. } catch (Exception) {}
  57. }
  58. void RequestBatteryForArrow()
  59. {
  60. try {
  61. if (BluetoothShoot.ins.hasData) {
  62. BluetoothShoot.ins.WriteData("B");
  63. }
  64. } catch (Exception) {}
  65. }
  66. public void RenderBattery(int deviceID, int value)
  67. {
  68. batteryDeviceID = deviceID;
  69. batteryValue = value;
  70. //bool active = true;
  71. //if (value <= 1) active = false;
  72. //Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
  73. //Text txt = this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
  74. //this.transform.Find($"Layout/Label{deviceID}").gameObject.SetActive(active);
  75. //img.transform.parent.gameObject.SetActive(active);
  76. //img.fillAmount = value / 100f;
  77. //txt.text = value + "%";
  78. }
  79. }