DeviceBatteryView.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. this.transform.Find("Layout/Label1").gameObject.SetActive(activeBow);
  36. this.transform.Find("Layout/Frame1").gameObject.SetActive(activeBow);
  37. this.transform.Find("Layout/Label2").gameObject.SetActive(activeArrow);
  38. this.transform.Find("Layout/Frame2").gameObject.SetActive(activeArrow);
  39. }
  40. }
  41. void RequestBatteryForBow()
  42. {
  43. try {
  44. if (BluetoothAim.ins.hasData) {
  45. BluetoothAim.ins.WriteData("B");
  46. }
  47. } catch (Exception) {}
  48. }
  49. void RequestBatteryForArrow()
  50. {
  51. try {
  52. if (BluetoothShoot.ins.hasData) {
  53. BluetoothShoot.ins.WriteData("B");
  54. }
  55. } catch (Exception) {}
  56. }
  57. public void RenderBattery(int deviceID, float value)
  58. {
  59. Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
  60. Text txt =this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
  61. img.fillAmount = value / 100f;
  62. txt.text = ((int) value) + "%";
  63. }
  64. }