DeviceBatteryView.cs 2.5 KB

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