DeviceBatteryView.cs 2.3 KB

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