DeviceBatteryView.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. 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. BluetoothAim.ins.WriteData("B");
  45. } catch (Exception) {}
  46. }
  47. void RequestBatteryForArrow()
  48. {
  49. try {
  50. BluetoothShoot.ins.WriteData("B");
  51. } catch (Exception) {}
  52. }
  53. public void RenderBattery(int deviceID, float value)
  54. {
  55. Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
  56. Text txt =this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
  57. img.fillAmount = value / 100f;
  58. txt.text = ((int) value).ToString();
  59. }
  60. }