| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using SmartBowSDK;
- public class BLEView : MonoBehaviour
- {
- public Button btnConnect;
- public Button btnGyr;
- public Button btnMag;
- public Text textBattery;
- public int playerIndex;
- SmartBowHelper smartBowHelper;
- void Start()
- {
- smartBowHelper = SmartBowHelper.NewInstance();
- smartBowHelper.OnBluetoothModuleInited += () =>
- {
- UpdateConnectText();
- smartBowHelper.StartRotationSensor();
- smartBowHelper.StartShootingSensor();
- };
- smartBowHelper.OnBluetoothError += (error, message) =>
- {
- if (error == BluetoothError.ScanNotFoundTargetDevice)
- {
- PopupMgr.ins.ShowTip("连接失败,未发现目标设备!");
- return;
- }
- PopupMgr.ins.ShowTip(message);
- };
- smartBowHelper.OnBluetoothStatusChanged += (oldStatus, newStatus) =>
- {
- if (playerIndex == 0) //1号控制
- {
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected) SimulateMouseController.ins?.SetBleConnected(true);
- else SimulateMouseController.ins?.SetBleConnected(false);
- }
- UpdateConnectText();
- };
- smartBowHelper.OnRotationUpdate += (r) =>
- {
- GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r);
- if (playerIndex == 0) //1号控制
- {
- if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r);
- }
- };
- smartBowHelper.OnShooting += OnShot;
- smartBowHelper.OnFunctionKeyPress += () =>
- {
- smartBowHelper.ResetAim();
- };
- if (playerIndex == 0 && BluetoothWindows.IsWindows())
- BleWinHelper.RegisterTo(smartBowHelper.gameObject);
- btnConnect.onClick.AddListener(OnClick_Connect);
- btnGyr.onClick.AddListener(OnClick_CalibrateGyr);
- btnMag.onClick.AddListener(OnClick_CalibrateMag);
- }
- void Update()
- {
- UpdateCalibrateGyrText();
- UpdateCalibrateMagText();
- UpdateBatteryText();
- }
- float _lastShotTime = 0;
- void OnShot(float speed)
- {
- if (Time.time - _lastShotTime < 1) return;
- _lastShotTime = Time.time;
- GameController.ins.aimCrossHairs[playerIndex].Shoot(speed);
- if (playerIndex == 0) //1号控制
- {
- if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked)
- {
- SB_EventSystem.ins.ClickMouse();
- }
- }
- }
- void UpdateConnectText()
- {
- Text t = btnConnect.GetComponentInChildren<Text>();
- var newStatus = smartBowHelper.GetBluetoothStatus();
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) t.text = "<color=blue>未连接</color>(点击连接)";
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) t.text = "<color=#FF670D>连接中</color>";
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
- {
- if (smartBowHelper.IsBluetoothModuleInited()) t.text = "<color=green>已连接</color>(点击断开)";
- else t.text = "<color=green>已连接</color><color=blue>(正在初始化)</color>";
- }
- }
- void UpdateCalibrateGyrText()
- {
- Text text = btnGyr.GetComponentInChildren<Text>();
- int progress = (int)(smartBowHelper.GetGyrProgress() * 100);
- string act = smartBowHelper.IsGyrCalibrating() ? "停止" : "开始";
- text.text = $"点击{act}陀螺仪校准({progress}%)";
- }
- void UpdateCalibrateMagText()
- {
- Text text = btnMag.GetComponentInChildren<Text>();
- if (smartBowHelper.IsMagCompleted()) text.text = $"<color=green>地磁计校准完成</color>(点击重置)";
- else
- {
- string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "<color=#FF670D>已重置</color>" : "点击重置";
- text.text = $"<color=blue>地磁计校准中</color>({tip})";
- }
- }
- void UpdateBatteryText()
- {
- //更新显示电量
- int battery = smartBowHelper.GetBattery();
- textBattery.text = battery > 0 ? $"电量值:{battery}" : "未获得电量值";
- }
- void OnClick_Connect()
- {
- var s = smartBowHelper.GetBluetoothStatus();
- if (s == SmartBowSDK.BluetoothStatusEnum.Connecting) return;
- else if (s == SmartBowSDK.BluetoothStatusEnum.None) smartBowHelper.Connect();
- else if (s == SmartBowSDK.BluetoothStatusEnum.Connected) smartBowHelper.Disconnect();
- }
- void OnClick_CalibrateGyr()
- {
- if (smartBowHelper.IsGyrCalibrating()) smartBowHelper.StopGyrCalibration();
- else smartBowHelper.StartGyrCalibration();
- }
- float _clickCalibrateMagTime = -100;
- void OnClick_CalibrateMag()
- {
- _clickCalibrateMagTime = Time.realtimeSinceStartup;
- smartBowHelper.StartMagCalibration();
- }
- }
|