using System; using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; public enum AimDeviceType { NONE = -1, HOUYI = 0, HOUYI2 = 1, ARTEMIS = 2, HOUYIPRO = 3, //枪械类 Gun = 4, ARTEMISPRO = 5, } [Serializable]//需要在转换为json格式的类的上方添加序列化 public class AimDeviceInfo { public int id; public int type = -1;//类型 AimDeviceType public int pos; //位置 public bool bInitMac = false; //是否初始化Mac public string mac; //记录当前 public AimDeviceInfo(int _id) { this.id = _id; } public void setInitMac(string macTemp) { bInitMac = true; mac = macTemp; } public void resetInitMac() { bInitMac = false; mac = ""; } } [Serializable] public class AimDeviceInfos { public List arry; } /* 瞄准处理器 */ public class AimHandler : MonoBehaviour { CameraToLook m_controlObj { get { return CameraToLook.ins; } } //记录一个设备 public AimDeviceInfo aimDeviceInfo = null; public AimDeviceInfo tempAimDeviceInfo = null;//临时处理值。最后赋值给aimDeviceInfo public AimDeviceInfos aimDeviceInfos = new AimDeviceInfos(); int deviceSelectIndex = 0;//默认选中第一个设备(1p) public Action aimDeviceInfoChangeEvent; public int DeviceType { get { if (m_axisHandler.GetType() == typeof(Axis9Handler)) return 9; if (m_axisHandler.GetType() == typeof(Axis663Handler)) return 663; return 0; } } private AxisBaseHandler m_axisHandler; //陀螺仪校准进度记录 [NonSerialized] public int gyrCalibrateCompleteCount = 0; [NonSerialized] public int gyrCalibrateTotalCount = 2000; public static AimHandler ins; public bool bInitOne = false; public OnCrossBtnEventEvent OnCrossBtnEvent; public delegate void OnCrossBtnEventEvent(); /// /// 准心事件 /// public void InvokeOnCrossBtnEvent() { try { OnCrossBtnEvent?.Invoke(); } catch (Exception e) { Debug.LogError(e); } } void Start() { ins = this; BluetoothDispatcher.aim = OnDataReceived; // m_axisHandler = new Axis9NopackHandler(this); //九轴旧 m_axisHandler = new Axis9Handler(this); //九轴新 // m_axisHandler = new Axis663Handler(this); //双陀螺仪 m_axisHandler.Init(); // StartCoroutine(TestRecord()); } public void onClearAimDeviceInfosNew() { PlayerPrefs.DeleteKey("aim-device-info-" + LoginMgr.myUserInfo.id); aimDeviceInfos.arry.Clear(); } /// /// 移除2p。即除了1p之后的设备 /// public void onClear2PAimDeviceInfosNew() { OnGetAimDeviceInfos(); if (aimDeviceInfos.arry.Count > 1) { aimDeviceInfos.arry.RemoveRange(1, aimDeviceInfos.arry.Count - 1); } OnSaveAimDeviceInfos(); } public void OnGetAimDeviceInfos() { string deviceInfo = PlayerPrefs.GetString("aim-device-info-" + LoginMgr.myUserInfo.id, ""); Debug.Log("get deviceSelectIndex:" + deviceInfo); if (deviceInfo != "") { aimDeviceInfos = JsonUtility.FromJson(deviceInfo);//这里的类是依据最外层{}决定的 } aimDeviceInfoChangeEvent?.Invoke(); } public void OnSaveAimDeviceInfos() { aimDeviceInfoChangeEvent?.Invoke(); PlayerPrefs.SetString("aim-device-info-" + LoginMgr.myUserInfo.id, JsonUtility.ToJson(aimDeviceInfos)); } //创建一个选择值 public void onCreateTempAimDeviceInfo() { tempAimDeviceInfo = new AimDeviceInfo(deviceSelectIndex); Debug.Log("onCreateTempAimDeviceInfo deviceSelectIndex:" + deviceSelectIndex); } //是否存在AimDeviceInfo,存在更新,不存在创建; public void onCreateAimDeviceInfoById() { OnGetAimDeviceInfos(); //deviceIndex 区分 1p 还是 2p bool bCanAdd = true; foreach (AimDeviceInfo p in aimDeviceInfos.arry) { if (deviceSelectIndex == p.id) { aimDeviceInfo = p; bCanAdd = false; } //Debug.Log("33deviceSelectIndex:" + deviceSelectIndex + ",bCanAdd:" + bCanAdd+ " , id:" + p.id +" , type:"+ p.type); } if (bCanAdd) { Debug.Log("add deviceSelectIndex:" + deviceSelectIndex); aimDeviceInfo = new AimDeviceInfo(deviceSelectIndex); aimDeviceInfos.arry.Add(aimDeviceInfo); } OnSaveAimDeviceInfos(); } public void SetAimDeviceSelectIndex(int selectIndex) { if (selectIndex < 0) { Debug.LogWarning("selectIndex不能小于0:"+ selectIndex); return; } deviceSelectIndex = selectIndex; Debug.Log("SetAimDeviceSelectIndex :" + selectIndex); } public void SetTempAimDeviceType(AimDeviceType _aimDeviceType) { if (tempAimDeviceInfo == null) return; tempAimDeviceInfo.type = (int)_aimDeviceType; } public void SetAimDeviceType(AimDeviceType _aimDeviceType) { if (aimDeviceInfo == null) return; aimDeviceInfo.type = (int)_aimDeviceType; _AimDeviceInfosUpdate(); OnSaveAimDeviceInfos(); } public void SetAimDeviceType(int _aimDeviceType) { if (aimDeviceInfo == null) return; aimDeviceInfo.type = _aimDeviceType; _AimDeviceInfosUpdate(); OnSaveAimDeviceInfos(); } //APP连接需进行MAC地址的比对。 //只有再引导初始化页面才保存连接使用的mac地址。 public void SetAimDeviceMac(string _mac) { if (aimDeviceInfo == null) return; aimDeviceInfo.setInitMac(_mac); _AimDeviceInfosUpdate(); OnSaveAimDeviceInfos(); } //重置mac存储信息 public void ResetAimDeviceMac() { if (aimDeviceInfo == null) return; aimDeviceInfo.resetInitMac(); _AimDeviceInfosUpdate(); OnSaveAimDeviceInfos(); } void _AimDeviceInfosUpdate() { for (int i = 0; i < aimDeviceInfos.arry.Count; i++) { if (aimDeviceInfo.id == aimDeviceInfos.arry[i].id) { aimDeviceInfos.arry[i] = aimDeviceInfo; } } } #region 根据设备1p或者2p 获取HOUYIPRO的状态 public bool isHOUYIPRO(BluetoothPlayer bluetoothPlayer) { bool _isHOUYIPRO = false; foreach (AimDeviceInfo p in AimHandler.ins.aimDeviceInfos.arry) { if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.HOUYIPRO) { _isHOUYIPRO = true; } } return _isHOUYIPRO; } #endregion #region 根据设备1p或者2p 获取ARTEMISPro的状态 public bool isARTEMISPro(BluetoothPlayer bluetoothPlayer) { bool _isARTEMISPro = false; foreach (AimDeviceInfo p in ins.aimDeviceInfos.arry) { if ((int)bluetoothPlayer == p.id && p.type == (int)AimDeviceType.ARTEMISPRO) { _isARTEMISPro = true; } } return _isARTEMISPro; } #endregion public void ReinitAxisHandler() { Debug.Log("ReinitAxisHandler"); m_axisHandler.Init(); } // System.Collections.IEnumerator TestRecord() { // while (LoginMgr.myUserInfo.id == 0) yield return null; // UserComp.Instance.saveMac(); // } [NonSerialized] private Quaternion newRotation = Quaternion.identity; public void SetNewRotation(Quaternion quat) { this.newRotation = quat; } public void SetNewRotation(o0.Geometry.Quaternion o0Quat) { Quaternion quat = o0.Bow.Extension.ToUnityQuaternion(o0Quat); if (float.IsNaN(quat.x) || float.IsNaN(quat.y) || float.IsNaN(quat.z) || float.IsNaN(quat.w)) { Debug.LogError($"九轴Rotation存在Nan值,double:{o0Quat.ToString()},float:{quat.ToString()}"); return; } if (float.IsInfinity(quat.x) || float.IsInfinity(quat.y) || float.IsInfinity(quat.z) || float.IsInfinity(quat.w)) { Debug.LogError($"九轴Rotation存在Infinity值,double:{o0Quat.ToString()},float:{quat.ToString()}"); return; } this.newRotation = quat; } [NonSerialized] public bool lerpForRotation = true; [NonSerialized] public float lerpTimeRate = 7; //加一个枪射击的触发时间 float _lastShootTime = 0; public void Update() { //&& !InfraredDemo.running if (m_controlObj && !m_ban9AxisCalculate && bRuning9Axis()) { if (lerpForRotation) m_controlObj.localRotation = Quaternion.Lerp(m_controlObj.localRotation, newRotation, Time.deltaTime * lerpTimeRate); else m_controlObj.localRotation = newRotation; } if (IsMagCompleted()) { if (!m_magCompleted) { StartCoroutine(SaveMag()); PopupMgr.ins.ShowTipTop(TextAutoLanguage2.GetTextByKey("tip_mag-calibrate_success")); } m_magCompleted = true; } else { m_magCompleted = false; } //if (Input.GetKeyDown(KeyCode.Space)) //{ // //AutoResetView.DoIdentity(); // CrossBtnEvent(); //} } public void OnDataReceived(byte[] bytes) { if (bytes.Length != 27 && bytes.Length != 39) { if (bytes.Length == 2) { if (bytes[0] == 0x66 && bytes[1] == 0x31) { if (bInitOne) { bInitOne = false; if (aimDeviceInfo.type == (int)AimDeviceType.NONE || aimDeviceInfo.type == (int)AimDeviceType.HOUYI || aimDeviceInfo.type == (int)AimDeviceType.HOUYI2 || aimDeviceInfo.type == (int)AimDeviceType.ARTEMIS) { AutoResetView.DoIdentity(); } else { //准心开关 CrossBtnEvent(); } } else { if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) { //模拟鼠标弹出时候 InfraredGuider infraredGuider = FindAnyObjectByType(); if (infraredGuider == null) { //视角回正 DoIdentity(); //鼠标居中自然会居中 } //流程触发红外描点 InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType(); if (infraredScreenPositioningView) { InvokeOnCrossBtnEvent(); } } else { if (aimDeviceInfo.type == (int)AimDeviceType.NONE || aimDeviceInfo.type == (int)AimDeviceType.HOUYI || aimDeviceInfo.type == (int)AimDeviceType.HOUYI2 || aimDeviceInfo.type == (int)AimDeviceType.ARTEMIS) { AutoResetView.DoIdentity(); } else { //准心开关 CrossBtnEvent(); } } } } else if (bytes[0] == 0x66 && bytes[1] == 0x32) { //红外部分 if (GlobalData.MyDeviceMode == DeviceMode.Gun || BluetoothAim.ins && BluetoothAim.ins.isMainConnectToInfraredDevice()) { InfraredGuider infraredGuider = FindAnyObjectByType(); //‘校准'功能的调用,使用原来调出光标的功能键;即在HOUYI Pro使用长按视角归位键;在ARTEMIS Pro使用快速双击按键 if (infraredGuider == null) { AutoResetView.DoIdentity(); } else { //先判断是否处于校准步骤 infraredGuider.onLongClickDevice(); } } else if (SB_EventSystem.ins) { // if (SB_EventSystem.ins && !CommonConfig.SpecialVersion1) { //唤起/隐藏虚拟鼠标 SB_EventSystem.ins.AwakenSimulateMouse(); } } else if (bytes[1] == 10) { //显示电量 DeviceBatteryView.ins.RenderBattery(1, bytes[0]); //DeviceView.ins.RenderBattery(1, bytes[0]); } } else if (bytes[0] == 0x5b) { if (GlobalData.MyDeviceMode == DeviceMode.Gun) { if (Time.realtimeSinceStartup - _lastShootTime < 1) return; _lastShootTime = Time.realtimeSinceStartup; //流程触发红外描点 InfraredScreenPositioningView infraredScreenPositioningView = FindAnyObjectByType(); if (infraredScreenPositioningView) { InvokeOnCrossBtnEvent(); } } //红外射击检测 ShootCheck.ins.ShootByInfrared(bytes); } else if (bytes[0] == 0x5C) { //00 弹夹分离,01 上弹夹 ShootCheck.ins.UpdateTheMagazine(bytes); } else if (bytes[0] == 0x5E) { Debug.Log("接收到系统数据:" + BitConverter.ToString(bytes)); //设备类型 switch (bytes[1]) { case 0x01: Debug.Log("设备类型:HOUYI Pro"); break; case 0x02: Debug.Log("设备类型:ARTEMIS Pro"); break; case 0x03: Debug.Log("设备类型:Pistol 1"); break; } // 系统类型 switch (bytes[2]) { case 0x01: Debug.Log("系统类型:移动手机"); break; case 0x02: Debug.Log("系统类型:PC电脑"); break; case 0x03: Debug.Log("系统类型:VR设备"); break; } } return; } //if (InfraredDemo.running) //{ // BluetoothAim.ins.WriteData("4"); //关闭九轴 // return; //} if (!bRuning9Axis()) { return; } m_axisHandler.Update(bytes); if (BowQuatDebug.ins) BowQuatDebug.ins.ShowModuleQuat(newRotation.eulerAngles); if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) SB_EventSystem.ins.MoveSimulateMouse(newRotation); } public byte[] InsertByteAtBeginning(byte[] originalArray, byte newByte) { byte[] newArray = new byte[originalArray.Length + 1]; newArray[0] = newByte; Array.Copy(originalArray, 0, newArray, 1, originalArray.Length); return newArray; } private bool m_magCompleted; public void CorrectMagCompleted(bool value) { m_magCompleted = value; } private bool m_ban9AxisCalculate = false; public void Ban9AxisCalculate(bool ban) { // m_ban9AxisCalculate = ban; // if (!ban) // { // SetMsOldDefault(); // if (m_controlObj) m_controlObj.localRotation = newRotation; // } } public void SetMsOldDefault() { if (m_axisHandler.GetType() == typeof(Axis9NopackHandler)) (m_axisHandler as Axis9NopackHandler).msOld = default; } public void DoIdentity() { //if (InfraredDemo.running) return; m_axisHandler.DoIdentity(); if (!bRuning9Axis()) return; if (m_controlObj) m_controlObj.localRotation = newRotation; } public void NotifyAxisOnShot() { m_axisHandler.NotifyAxisOnShot(); } public void CalibrateGyr(bool calibration) { m_axisHandler.CalibrateGyr(calibration); } public void InitGyr(string record) { m_axisHandler.InitGyr(record); } public void InitMag(string record) { m_axisHandler.InitMag(record); } public void ResetGyr() { m_axisHandler.ResetGyr(); } public void ResetMag() { m_axisHandler.ResetMag(); } public void ApplyImpreciseMag() { m_axisHandler.ApplyImpreciseMag(); } public bool IsGyrCompleted() { return m_axisHandler.IsGyrCompleted(); } public bool IsMagCompleted() { return m_axisHandler.IsMagCompleted(); } public IEnumerator SaveGyr() { return m_axisHandler.SaveGyr(); } public IEnumerator SaveMag() { return m_axisHandler.SaveMag(); } public void ResumeCalibrateRecord(string record) { m_axisHandler.ResumeCalibrateRecord(record); } #region 获取一个连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断 public bool bRuning9Axis() { //连接的设备下,对应的 Archery 是运行9轴的代码处理位置信息判断 //弓箭类型,但是不是HOUYI Pro 情况下。运行9轴代码 bool bRuning = GlobalData.MyDeviceMode == DeviceMode.Archery && !BluetoothAim.ins.isMainConnectToInfraredDevice(); return bRuning; } #endregion /// /// 操作准心开关事件 /// private void CrossBtnEvent() { //b端不处理准心 if (CommonConfig.StandaloneModeOrPlatformB) return; Debug.Log("CrossBtnEvent"); //准心开关 InvokeOnCrossBtnEvent(); if (GameAssistUI.ins) { InfraredGuider infraredGuider = FindAnyObjectByType(); if (infraredGuider == null) { //显示控制准心按钮 Transform _button5 = GameAssistUI.ins.transform.Find("Button5"); if (_button5.gameObject.activeSelf) { Button crossHairBtn = _button5.GetComponent