DeviceView_ItemShow.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class DeviceView_ItemShow : MonoBehaviour
  7. {
  8. [SerializeField] Button btnConnectBow;
  9. [SerializeField] GameObject process;
  10. [SerializeField] GameObject connected;
  11. [SerializeField] GameObject betteryBar;
  12. [SerializeField] GameObject betteryValue;
  13. [SerializeField] Image pointImage;
  14. [SerializeField] Sprite[] pointImages;
  15. float countingTime1 = 0;
  16. BluetoothStatusEnum bowStatus;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. btnConnectBow.onClick.AddListener(delegate () {
  21. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  22. BluetoothAim.ins.DoConnect();
  23. });
  24. }
  25. // Update is called once per frame
  26. void Update()
  27. {
  28. if (BluetoothAim.ins && bowStatus != BluetoothAim.ins.status)
  29. {
  30. bowStatus = BluetoothAim.ins.status;
  31. if (!process.activeSelf) process.SetActive(true);
  32. if (connected.activeSelf) connected.SetActive(false);
  33. if (process.activeSelf)
  34. {
  35. (int textID, Color color) = BluetoothStatus.GetStatusInfo(BluetoothAim.ins.status);
  36. process.GetComponentInChildren<TextAutoLanguage>().SetText(textID);
  37. process.GetComponentInChildren<Text>().color = color;
  38. }
  39. //btnConnectBow.transform.Find("Check").gameObject.SetActive(bowStatus == BluetoothStatusEnum.ConnectSuccess);
  40. pointImage.sprite = pointImages[1];
  41. }
  42. else if (BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess)
  43. {
  44. if (process.activeSelf) process.SetActive(false);
  45. if (!connected.activeSelf) connected.SetActive(true);
  46. //Á¬½Óºó£¬¸üÐÂµç³Ø×´Ì¬
  47. if (countingTime1 < 5)
  48. {
  49. countingTime1 += Time.deltaTime;
  50. }
  51. else
  52. {
  53. countingTime1 = 0;
  54. //RequestBatteryForBow();
  55. RenderBattery(DeviceBatteryView.ins.batteryDeviceID, DeviceBatteryView.ins.batteryValue);
  56. pointImage.sprite = pointImages[0];
  57. }
  58. }
  59. }
  60. void RequestBatteryForBow()
  61. {
  62. try
  63. {
  64. if (BluetoothAim.ins.hasData && JCUnityLib.TimeUtils.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000)
  65. {
  66. BluetoothAim.ins.RequestBattery();
  67. }
  68. }
  69. catch (Exception) { }
  70. }
  71. public void RenderBattery(int deviceID, int value)
  72. {
  73. Image img = betteryBar.GetComponent<Image>();
  74. Text txt = betteryValue.GetComponent<Text>();
  75. img.fillAmount = value / 100f;
  76. txt.text = value + "%";
  77. }
  78. }