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