| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- using UnityEngine;
- using UnityEngine.UI;
- using SmartBowSDK;
- namespace DuckHunter
- {
- public class SmartBowController : MonoBehaviour
- {
- [SerializeField] Button _btnConnect;
- [SerializeField] Button _btnCalibrateGyr;
- [SerializeField] Button _btnCalibrateMag;
- [SerializeField] Button _btnResetAim;
- public static SmartBowController Instance;
- void Awake()
- {
- if (Instance)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- void Start()
- {
- _btnCalibrateGyr.onClick.AddListener(OnClick_CalibrateGyr);
- _btnCalibrateMag.onClick.AddListener(OnClick_CalibrateMag);
- //监听蓝牙状态变化
- SmartBowHelper.GetInstance().OnBluetoothStatusChanged += OnBluetoothStatusChanged;
- //监听蓝牙抛出的错误
- SmartBowHelper.GetInstance().OnBluetoothError += OnBluetoothError;
- //监听蓝牙模块初始化完成
- SmartBowHelper.GetInstance().OnBluetoothModuleInited += OnBluetoothModuleInited;
- //监听姿态角变化
- SmartBowHelper.GetInstance().OnRotationUpdate += OnRotationUpdate;
- //监听射击
- SmartBowHelper.GetInstance().OnShooting += OnShooting;
- //监听模块功能键短按
- SmartBowHelper.GetInstance().OnFunctionKeyPress += OnFunctionKeyPress;
- //监听模块功能键长按
- SmartBowHelper.GetInstance().OnFunctionKeyLongPress += OnFunctionKeyLongPress;
- UpdateBtnConnectText();
- // ActiveCanvasUI();
- // ActiveBoxBLEModule(false);
- }
- float lastAutoConnectTime = 0;
- void Update()
- {
- // if (_boxBLEActived)
- // {
- // UpdateCalibrateGyrText();
- // UpdateCalibrateMagText();
- // }
- UpdateCalibrateGyrText();
- UpdateCalibrateMagText();
- if (
- SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None &&
- Time.realtimeSinceStartup - lastAutoConnectTime > 1
- ) {
- lastAutoConnectTime = Time.realtimeSinceStartup;
- SmartBowHelper.GetInstance().Connect();
- }
- }
- /// <summary>
- /// 按钮逻辑-连接/断开蓝牙模块
- /// </summary>
- public void OnClick_Connect()
- {
- AudioMgr.ins.PlayBtn();
- if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connecting) return;
- if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.None)
- {
- SmartBowHelper.GetInstance().Connect();
- return;
- }
- if (SmartBowHelper.GetInstance().GetBluetoothStatus() == BluetoothStatusEnum.Connected)
- {
- SmartBowHelper.GetInstance().Disconnect();
- return;
- }
- }
- /// <summary>
- /// 按钮逻辑-视角归位
- /// </summary>
- public void OnClick_ResetAim()
- {
- AudioMgr.ins.PlayBtn();
- SmartBowHelper.GetInstance().ResetAim();
- }
- /// <summary>
- /// 按钮逻辑-开始/停止陀螺仪校准
- /// </summary>
- public void OnClick_CalibrateGyr()
- {
- AudioMgr.ins.PlayBtn();
- if (SmartBowHelper.GetInstance().IsGyrCalibrating())
- SmartBowHelper.GetInstance().StopGyrCalibration();
- else
- SmartBowHelper.GetInstance().StartGyrCalibration();
- }
- void UpdateCalibrateGyrText()
- {
- Text text = _btnCalibrateGyr.GetComponentInChildren<Text>();
- //当陀螺仪校准完成时
- if (SmartBowHelper.GetInstance().IsGyrCompleted())
- {
- //UI显示校准完成
- text.text = "<color=#00FF00>陀螺仪初始化完成</color>";
- }
- else
- {
- //获取陀螺仪校准进度
- int progress = (int)(SmartBowHelper.GetInstance().GetGyrProgress() * 100);
- //UI显示校准进度
- if (SmartBowHelper.GetInstance().IsGyrCalibrating()) text.text = string.Format("<color=#FFC500>停止</color>初始化陀螺仪{0}%", progress);
- else text.text = string.Format("开始初始化陀螺仪{0}%", progress);
- }
- }
- float _clickCalibrateMagTime = -100;
- /// <summary>
- /// 按钮逻辑-重置地磁计校准
- /// </summary>
- public void OnClick_CalibrateMag()
- {
- AudioMgr.ins.PlayBtn();
- _clickCalibrateMagTime = Time.realtimeSinceStartup;
- SmartBowHelper.GetInstance().StartMagCalibration();
- }
- void UpdateCalibrateMagText()
- {
- Text text = _btnCalibrateMag.GetComponentInChildren<Text>();
- //当地磁计校准完成时
- if (SmartBowHelper.GetInstance().IsMagCompleted())
- {
- text.text = "<color=#00FF00>地磁计初始化完成</color>";
- }
- else
- {
- text.text = Time.realtimeSinceStartup - _clickCalibrateMagTime < 1 ? "<color=#FFC500>地磁计已重置</color>" : "地磁计自动初始化中";
- }
- }
- void UpdateBtnConnectText()
- {
- // //获取蓝牙状态
- // BluetoothStatusEnum bluetoothStatus = SmartBowHelper.GetInstance().GetBluetoothStatus();
- // Text text = _btnConnect.GetComponentInChildren<Text>();
- // //显示蓝牙状态-未连接
- // if (bluetoothStatus == BluetoothStatusEnum.None) text.text = "连接";
- // //显示蓝牙状态-连接中
- // if (bluetoothStatus == BluetoothStatusEnum.Connecting) text.text = "<color=#FFC500>连接中</color>";
- // //显示蓝牙状态-已连接
- // if (bluetoothStatus == BluetoothStatusEnum.Connected)
- // {
- // //判断蓝牙模块是否初始化完成
- // if (SmartBowHelper.GetInstance().IsBluetoothModuleInited()) text.text = "<color=#00FF00>已连接</color>";
- // else text.text = "<color=#00FF00>已连接</color> <color=#00DFFF>(初始化中)</color>";
- // }
- }
- void OnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
- {
- UpdateBtnConnectText();
- if (newStatus == BluetoothStatusEnum.Connected) {
- BowCamera.isTouchMode = false;
- } else {
- BowCamera.isTouchMode = true;
- }
- }
- void OnBluetoothError(BluetoothError error, string message)
- {
- Debug.Log("OnBluetoothError:" + error + " " + message);
- string errorText = null;
- if (error == BluetoothError.BluetoothNotEnabled)
- errorText = "蓝牙开关未打开";
- else if (error == BluetoothError.LocationPermissionNotGranted)
- errorText = "尚未授予定位权限";
- else if (error == BluetoothError.ScanPermissionNotGranted)
- errorText = "尚未授予扫描附近设备权限";
- else if (error == BluetoothError.ScanNotFoundTargetDevice)
- errorText = "连接失败,未发现目标设备!";
- if (errorText != null)
- TextSmartBowTip.Show(errorText);
- }
- /// <summary>
- /// 当蓝牙模块初始化完成时
- /// </summary>
- void OnBluetoothModuleInited()
- {
- UpdateBtnConnectText();
- //获取Mac地址
- string macAddress = SmartBowHelper.GetInstance().GetMacAddress();
- Debug.Log("Mac地址为" + macAddress);
- //获取电量
- int battery = SmartBowHelper.GetInstance().GetBattery();
- Debug.Log("当前电量为" + battery);
- //开启九轴传感
- SmartBowHelper.GetInstance().StartRotationSensor();
- //开启射箭传感
- SmartBowHelper.GetInstance().StartShootingSensor();
- }
- void OnRotationUpdate(Quaternion rotation)
- {
- CameraToLook.ins.transform.localRotation = rotation;
- }
- float _lastShootTime = 0;
- void OnShooting(float speed)
- {
- if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
- _lastShootTime = Time.realtimeSinceStartup;
- if (ArmBow.ins) {
- ArmBow.ins.shootSpeed = speed;
- ArmBow.ins.ADS_fire();
- }
- }
- void OnFunctionKeyPress()
- {
- Debug.Log("功能键短按");
- AutoResetView.DoIdentity();
- }
- [ContextMenu("TestIdentity")]
- void TestIdentity() {
- AutoResetView.DoIdentity();
- }
- void OnFunctionKeyLongPress()
- {
- Debug.Log("功能键长按");
- // SB_EventSystem.ins.AwakenSimulateMouse();
- }
- private void ActiveCanvasUI()
- {
- transform.Find("CanvasUI").gameObject.SetActive(true);
- }
- // bool _boxBLEActived = false;
- // public void ActiveBoxBLEModule(bool active)
- // {
- // var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
- // o.SetActive(active);
- // _boxBLEActived = active;
- // }
- /// <summary>
- /// 反转激活BoxBLEModule
- /// </summary>
- public void ActiveBoxBLEModule()
- {
- // var o = transform.Find("CanvasUI/BoxBLEModule").gameObject;
- // o.SetActive(!o.activeSelf);
- // _boxBLEActived = o.activeSelf;
- }
- /// <summary>
- /// 按钮逻辑-停止传感器
- /// </summary>
- public void OnClick_StopSensor()
- {
- //停止九轴传感
- SmartBowHelper.GetInstance().StopRotationSensor();
- //停止射箭传感
- SmartBowHelper.GetInstance().StopShootingSensor();
- }
- }
- }
|