DeviceBatteryView.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. void Start()
  13. {
  14. if (ins) {
  15. Destroy(this.gameObject);
  16. } else {
  17. ins = this;
  18. DontDestroyOnLoad(this.gameObject);
  19. }
  20. }
  21. float countingTime1 = 0;
  22. float countingTime2 = 1;
  23. void Update()
  24. {
  25. if (countingTime1 < 5) {
  26. countingTime1 += Time.deltaTime;
  27. } else {
  28. countingTime1 = 0;
  29. RequestBatteryForBow();
  30. RequestBatteryForArrow();
  31. }
  32. if (countingTime2 < 1) {
  33. countingTime2 += Time.deltaTime;
  34. } else {
  35. countingTime2 = 0;
  36. bool activeBow = BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
  37. bool activeArrow = BluetoothShoot.ins && BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess;
  38. if (!activeBow) {
  39. this.transform.Find("Layout/Label1").gameObject.SetActive(false);
  40. this.transform.Find("Layout/Frame1").gameObject.SetActive(false);
  41. }
  42. if (!activeArrow) {
  43. this.transform.Find("Layout/Label2").gameObject.SetActive(false);
  44. this.transform.Find("Layout/Frame2").gameObject.SetActive(false);
  45. }
  46. }
  47. }
  48. void RequestBatteryForBow()
  49. {
  50. try {
  51. if (BluetoothAim.ins.hasData && JC.CS.Utility.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000) {
  52. BluetoothAim.ins.RequestBattery();
  53. }
  54. } catch (Exception) {}
  55. }
  56. void RequestBatteryForArrow()
  57. {
  58. try {
  59. if (BluetoothShoot.ins.hasData) {
  60. BluetoothShoot.ins.WriteData("B");
  61. }
  62. } catch (Exception) {}
  63. }
  64. public void RenderBattery(int deviceID, int value)
  65. {
  66. bool active = true;
  67. if (value <= 1) active = false;
  68. Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
  69. Text txt = this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
  70. this.transform.Find($"Layout/Label{deviceID}").gameObject.SetActive(active);
  71. img.transform.parent.gameObject.SetActive(active);
  72. img.fillAmount = value / 100f;
  73. txt.text = value + "%";
  74. }
  75. }