| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- 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<GameObject>("AutoResetViewNew"));
- resetView.name = "AutoResetViewNewLeft";
- AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
- autoResetViewNewScript.setPosLeft();
- autoResetViewNewScript.action_OnDestroy += () => {
- smartBowHelper.ResetAim();
- };
- }
- else if (playerIndex == 1)
- {
- if (GameObject.Find("AutoResetViewNewRight")) return;
- GameObject resetView = Instantiate(Resources.Load<GameObject>("AutoResetViewNew"));
- resetView.name = "AutoResetViewNewRight";
- AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
- 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<GameObject>("Prefabs/Views/AutoResetViewNew"));
- // resetView.name = "AutoResetViewNewLeft";
- // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
- // 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<GameObject>("Prefabs/Views/AutoResetViewNew"));
- // resetView.name = "AutoResetViewNewRight";
- // AutoResetViewNew autoResetViewNewScript = resetView.GetComponent<AutoResetViewNew>();
- // 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<Text>();
- var newStatus = smartBowHelper.GetBluetoothStatus();
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.None) {
- t.text = "<color=blue>未连接</color>(点击连接)";
- if (playerIndex == 1)
- {
- GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = true;
- }
- }
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connecting) t.text = "<color=#FF670D>连接中</color>";
- if (newStatus == SmartBowSDK.BluetoothStatusEnum.Connected)
- {
- if (playerIndex == 1)
- {
- GameController.ins.GetBowCameraDoublePlayer(PlayerType.SecondPlayer).isTouchMode = false;
- }
- 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}" : "未获得电量值";
- }
- 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();
- }
- }
|