| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class DeviceBatteryView : MonoBehaviour
- {
- static DeviceBatteryView ins;
- void Start()
- {
- if (ins) {
- Destroy(this.gameObject);
- } else {
- ins = this;
- DontDestroyOnLoad(this.gameObject);
- }
- }
- float countingTime1 = 0;
- float countingTime2 = 1;
- void Update()
- {
- if (countingTime1 < 5) {
- countingTime1 += Time.deltaTime;
- } else {
- countingTime1 = 0;
- RequestBatteryForBow();
- RequestBatteryForArrow();
- }
- if (countingTime2 < 1) {
- countingTime2 += Time.deltaTime;
- } else {
- countingTime2 = 0;
- bool activeBow = BluetoothAim.ins && BluetoothAim.ins.status == BluetoothStatusEnum.ConnectSuccess;
- bool activeArrow = BluetoothShoot.ins && BluetoothShoot.ins.status == BluetoothStatusEnum.ConnectSuccess;
- this.transform.Find("Layout/Label1").gameObject.SetActive(activeBow);
- this.transform.Find("Layout/Frame1").gameObject.SetActive(activeBow);
- this.transform.Find("Layout/Label2").gameObject.SetActive(activeArrow);
- this.transform.Find("Layout/Frame2").gameObject.SetActive(activeArrow);
- }
- }
- void RequestBatteryForBow()
- {
- try {
- BluetoothAim.ins.WriteData("B");
- } catch (Exception) {}
- }
- void RequestBatteryForArrow()
- {
- try {
- BluetoothShoot.ins.WriteData("B");
- } catch (Exception) {}
- }
- public void RenderBattery(int deviceID, float value)
- {
- Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
- Text txt =this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
- img.fillAmount = value / 100f;
- txt.text = ((int) value).ToString();
- }
- }
|