| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- /* 设备的电池显示界面(右上角) */
- public class DeviceBatteryView : MonoBehaviour
- {
- public static DeviceBatteryView ins;
- public Text labelTemperature;
- 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;
- if (!activeBow) {
- this.transform.Find("Layout/Label1").gameObject.SetActive(false);
- this.transform.Find("Layout/Frame1").gameObject.SetActive(false);
- }
- if (!activeArrow) {
- this.transform.Find("Layout/Label2").gameObject.SetActive(false);
- this.transform.Find("Layout/Frame2").gameObject.SetActive(false);
- }
- }
- }
- void RequestBatteryForBow()
- {
- try {
- if (BluetoothAim.ins.hasData && JC.CS.Utility.GetTimestamp() - BluetoothAim.ins.hasDataTime > 5000) {
- BluetoothAim.ins.RequestBattery();
- }
- } catch (Exception) {}
- }
- void RequestBatteryForArrow()
- {
- try {
- if (BluetoothShoot.ins.hasData) {
- BluetoothShoot.ins.WriteData("B");
- }
- } catch (Exception) {}
- }
- public void RenderBattery(int deviceID, int value)
- {
- bool active = true;
- if (value <= 1) active = false;
- Image img = this.transform.Find($"Layout/Frame{deviceID}/Bar").GetComponent<Image>();
- Text txt = this.transform.Find($"Layout/Frame{deviceID}/Value").GetComponent<Text>();
- this.transform.Find($"Layout/Label{deviceID}").gameObject.SetActive(active);
- img.transform.parent.gameObject.SetActive(active);
- img.fillAmount = value / 100f;
- txt.text = value + "%";
- }
- }
|