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; public GameObject autoResetView; //2p蓝牙信息部分参数 bool lerpForRotation = true; float lerpTimeRate = 7; private Quaternion newRotation = Quaternion.identity; 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) => { if (playerIndex == 0) //1号控制 { //new Vector4(0,Screen.width*0.5f,Screen.height,0) GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r, new Vector4(0, Screen.width * 0.5f, Screen.height, 0)); if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(r); } else if (playerIndex == 1) { newRotation = r; //GameController.ins.aimCrossHairs[playerIndex].UpdatePositionByModuleRotation(r, new Vector4(Screen.width * 0.5f, Screen.width, Screen.height, 0)); } }; smartBowHelper.OnShooting += OnShot; smartBowHelper.OnFunctionKeyPress += () => { //smartBowHelper.ResetAim(); if (playerIndex == 0) //1号控制 { if (GameObject.Find("AutoResetViewNewLeft")) return; GameObject resetView = Instantiate(Resources.Load("AutoResetViewNew")); resetView.name = "AutoResetViewNewLeft"; AutoResetViewNew autoResetViewNewScript = resetView.GetComponent(); autoResetViewNewScript.setPosLeft(); autoResetViewNewScript.action_OnDestroy += () => { smartBowHelper.ResetAim(); }; } else if (playerIndex == 1) { if (GameObject.Find("AutoResetViewNewRight")) return; GameObject resetView = Instantiate(Resources.Load("AutoResetViewNew")); resetView.name = "AutoResetViewNewRight"; AutoResetViewNew autoResetViewNewScript = resetView.GetComponent(); autoResetViewNewScript.setPosRight(); autoResetViewNewScript.action_OnDestroy += () => { smartBowHelper.ResetAim(); }; } }; if (playerIndex == 0 && BluetoothWindows.IsWindows()) BleWinHelper.RegisterTo(smartBowHelper.gameObject, smartBowHelper.CreateBluetoothWindows()); btnConnect.onClick.AddListener(OnClick_Connect); btnGyr.onClick.AddListener(OnClick_CalibrateGyr); btnMag.onClick.AddListener(OnClick_CalibrateMag); } void Update() { if (CameraToLookNew.ins != null) { Transform m_controlObj = CameraToLookNew.ins.transform; if (lerpForRotation) m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate); else m_controlObj.localRotation = newRotation; } //if (playerIndex == 0 && Input.GetKeyUp(KeyCode.Alpha1)) //{ // Debug.Log("Alpha1"); // if (GameObject.Find("AutoResetViewNewLeft")) return; // GameObject resetView = Instantiate(Resources.Load("Prefabs/Views/AutoResetViewNew")); // resetView.name = "AutoResetViewNewLeft"; // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent(); // autoResetViewNewScript.setPosLeft(); // autoResetViewNewScript.action_OnDestroy += () => // { // smartBowHelper.ResetAim(); // }; //} //if (playerIndex == 1 && Input.GetKeyUp(KeyCode.Alpha2)) //{ // Debug.Log("Alpha2"); // if (GameObject.Find("AutoResetViewNewRight")) return; // GameObject resetView = Instantiate(Resources.Load("Prefabs/Views/AutoResetViewNew")); // resetView.name = "AutoResetViewNewRight"; // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent(); // autoResetViewNewScript.setPosRight(); // autoResetViewNewScript.action_OnDestroy += () => // { // smartBowHelper.ResetAim(); // }; //} 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(); var newStatus = smartBowHelper.GetBluetoothStatus(); if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) { t.text = "未连接(点击连接)"; if (playerIndex == 1) { GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = true; } } if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) t.text = "连接中"; if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected) { if (playerIndex == 1) { GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false; } if (smartBowHelper.IsBluetoothModuleInited()) t.text = "已连接(点击断开)"; else t.text = "已连接(正在初始化)"; } } void UpdateCalibrateGyrText() { Text text = btnGyr.GetComponentInChildren(); int progress = (int)(smartBowHelper.GetGyrProgress() * 100); string act = smartBowHelper.IsGyrCalibrating() ? "停止" : "开始"; text.text = $"点击{act}陀螺仪校准({progress}%)"; } void UpdateCalibrateMagText() { Text text = btnMag.GetComponentInChildren(); if (smartBowHelper.IsMagCompleted()) text.text = $"地磁计校准完成(点击重置)"; else { string tip = Time.realtimeSinceStartup - _clickCalibrateMagTime < 2 ? "已重置" : "点击重置"; text.text = $"地磁计校准中({tip})"; } } void UpdateBatteryText() { //更新显示电量 int battery = smartBowHelper.GetBattery(); textBattery.text = battery > 0 ? $"电量值:{battery}" : "未获得电量值"; } string userTags = "smartbow_test"; int deviceId2 = 2; void OnClick_Connect() { var s = smartBowHelper.GetBluetoothStatus(); if (s == SmartBowSDK.BluetoothStatusEnum.Connecting) return; else if (s == SmartBowSDK.BluetoothStatusEnum.None) smartBowHelper.Connect(userTags, playerIndex); 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(); } }