| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773 |
- using System;
- using UnityEngine;
- namespace SmartBowSDK
- {
- public class SmartBowHelper : MonoBehaviour
- {
- private static SmartBowHelper _Instance;
- internal SmartBowNetwork smartBowNetwork;
- internal BluetoothAim_SDK bluetoothAim;
- internal AimHandler_SDK aimHandler;
- internal ShootChecker_SDK shootChecker;
- public CalibrateDataStorageMode calibrateDataStorageMode = CalibrateDataStorageMode.Remote;
- void Awake()
- {
- DontDestroyOnLoad(gameObject);
- gameObject.AddComponent<AOTAdapter_SDK>();
- smartBowNetwork = gameObject.AddComponent<SmartBowNetwork>();
- bluetoothAim = gameObject.AddComponent<BluetoothAim_SDK>();
- aimHandler = gameObject.AddComponent<AimHandler_SDK>();
- shootChecker = gameObject.AddComponent<ShootChecker_SDK>();
- smartBowNetwork.smartBowHelper = this;
- bluetoothAim.smartBowHelper = this;
- aimHandler.smartBowHelper = this;
- shootChecker.smartBowHelper = this;
- }
- /// <summary>
- /// 获取唯一实例
- /// </summary>
- /// <returns></returns>
- public static SmartBowHelper GetInstance()
- {
- if (!_Instance) _Instance = new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
- return _Instance;
- }
- /// <summary>
- /// 创建新的实例
- /// </summary>
- /// <returns></returns>
- public static SmartBowHelper NewInstance()
- {
- return new GameObject(typeof(SmartBowHelper).Name).AddComponent<SmartBowHelper>();
- }
- /// <summary>
- /// 获取一个window 蓝牙用例
- /// </summary>
- /// <returns></returns>
- public BluetoothWindows CreateBluetoothWindows()
- {
- return bluetoothAim.CreateWindowBLE();
- }
- #region 手机验证
- /// <summary>
- /// 发送手机验证码
- /// </summary>
- public void SendPhoneValidateCode(string phone, int language, Action<RequestResult> callback)
- {
- smartBowNetwork.SendPhoneValidateCode(phone, language, callback);
- }
- /// <summary>
- /// 验证手机验证码
- /// </summary>
- public void ValidatePhone(string phone, int code, Action<RequestResult> callback)
- {
- smartBowNetwork.ValidatePhoneCode(phone, code, callback);
- }
- #endregion
- #region 邮箱验证
- /// <summary>
- /// 发送邮箱验证码
- /// </summary>
- public void SendEmailValidateCode(string email, int language, Action<RequestResult> callback)
- {
- smartBowNetwork.SendEmailValidateCode(email, language, callback);
- }
- /// <summary>
- /// 验证邮箱验证码
- /// </summary>
- public void ValidateEmail(string email, int code, Action<RequestResult> callback)
- {
- smartBowNetwork.SendEmailValidateCode(email, code, callback);
- }
- #endregion
- #region 注册
- /// <summary>
- /// 用户注册
- /// </summary>
- /// <param name="serverIndex">服务器索引</param>
- /// <param name="username">用户名</param>
- /// <param name="password">密码</param>
- /// <param name="email">邮箱</param>
- /// <param name="phone">手机号</param>
- /// <param name="code">当前接收的code,海外用邮箱 serverIndex = 1,国内 serverIndex = 0用手机号</param>
- /// <param name="callback">回调</param>
- public void Register(int serverIndex, string username, string password, string email, string phone,int code, Action<RequestResult> callback)
- {
- smartBowNetwork.RegisterByCode(serverIndex, username, password, email, phone, code, callback);
- }
- /// <summary>
- /// 重置密码(支持邮箱或手机号)
- /// </summary>
- /// <param name="serverIndex">服务器索引</param>
- /// <param name="account">账号(邮箱或手机号,现在国内用手机,海外用邮箱)</param>
- /// <param name="code">验证码</param>
- /// <param name="newPassword">新密码</param>
- /// <param name="callback">回调结果</param>
- public void ResetPassword(int serverIndex,string account, string codeAccount, int code, string newPassword, Action<RequestResult> callback)
- {
- smartBowNetwork.ResetPasswordByAccount(serverIndex, account, codeAccount, code, newPassword, callback);
- }
- #endregion
- #region 登录
- /// <summary>
- /// 用户登录
- /// </summary>
- /// <param name="gameID">游戏ID</param>
- /// <param name="channelID">渠道ID</param>
- /// <param name="username">账号</param>
- /// <param name="password">密码</param>
- /// <param name="callback">回调函数</param>
- public void Login(string gameID, string channelID, string username, string password, Action<LoginResult> callback)
- {
- smartBowNetwork.Login(gameID, channelID, username, password, callback);
- }
- /// <summary>
- /// 用户登出
- /// </summary>
- /// <param name="callback">回调函数</param>
- public void Logout(Action<bool> callback)
- {
- smartBowNetwork.Logout(callback);
- }
- /// <summary>
- /// 获取用户基本信息
- /// </summary>
- /// <param name="callback">回调函数</param>
- public void GetUserInfo(Action<GetUserInfoResult> callback)
- {
- smartBowNetwork.GetUserInfo(callback);
- }
- /// <summary>
- /// 设置用户名
- /// </summary>
- public void SaveUserName(string nickname, Action<RequestResult> callback)
- {
- smartBowNetwork.SaveUserName(nickname, callback);
- }
- /// <summary>
- /// 设置用户扩展信息
- /// </summary>
- public void SaveUserInfo(UserInfoExtended userInfo, Action<RequestResult> callback)
- {
- smartBowNetwork.SaveUserInfo(userInfo, callback);
- }
- #endregion
- #region 分数上传
- /// <summary>
- /// 上传单人赛季成绩
- /// </summary>
- public void UploadSeasonSinglePlayerGameRes(string secret, int score, Action<RequestResult> callback)
- {
- smartBowNetwork.UploadSeasonSinglePlayerGameRes(secret, score, callback);
- }
- /// <summary>
- /// 上传PK赛季成绩
- /// </summary>
- public void UploadSeasonPKGameRes(string roomKey, string secret, int p2ID, int gameRes, Action<RequestResult> callback)
- {
- smartBowNetwork.UploadSeasonPKGameRes(roomKey, secret, p2ID, gameRes, callback);
- }
- #endregion
- #region 排行榜
- /// <summary>
- /// 获取排行榜(按游戏类型)
- /// </summary>
- public void GetSeasonRankListByGameType(string secret, int rankArea, string country, string state, string city, Action<RequestResult> callback)
- {
- smartBowNetwork.GetSeasonRankListByGameType(secret, rankArea, country, state, city, callback);
- }
- /// <summary>
- /// 获取自定义排行榜
- /// </summary>
- public void GetSeasonRankListByCustom(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action<RequestResult> callback)
- {
- smartBowNetwork.GetSeasonRankListByCustom(secret, rankArea, country, state, city, rankType, count, callback);
- }
- /// <summary>
- /// 获取当前用户的排名信息
- /// </summary>
- public void GetUserRankByGameType(string secret, int rankArea, string country, string state, string city, int rankType, int count, Action<RequestResult> callback)
- {
- smartBowNetwork.GetUserRankByGameType(secret, rankArea, country, state, city, rankType, count, callback);
- }
- #endregion
- #region 定制游戏上传接口
- /// <summary>
- /// 上传自定义排行榜分数
- /// </summary>
- public void UploadCustomLeaderboardScore(string secret, int taskId, int difficultType, int modeType, int deviceType, int score, Action<RequestResult> callback)
- {
- smartBowNetwork.UploadCustomLeaderboardScore(secret, taskId, difficultType, modeType, deviceType, score, callback);
- }
- /// <summary>
- /// 获取自定义排行榜数据
- /// </summary>
- public void GetCustomLeaderboard(string secret, int taskId, int difficultType, int modeType, int deviceType, int timeType, Action<RequestResult> callback)
- {
- smartBowNetwork.GetCustomLeaderboard(secret, taskId, difficultType, modeType, deviceType, timeType, callback);
- }
- #endregion
- #region 蓝牙:蓝牙部分新增修改,添加当前判断设备mac
- ///// <summary>
- ///// 连接蓝牙模块
- ///// </summary>
- //public void Connect()
- //{
- // //之前的默认不用判断mac
- // //当前连接不需要需要检测设备,设置为null
- // bluetoothAim.ResetAimDeviceInfoToNull();
- // bluetoothAim.Connect();
- //}
- /// <summary>
- /// 设置蓝牙过滤名称
- /// </summary>
- /// <param name="name">不传参数时候,设置值 ""</param>
- public void SetFilters(string name = "") {
- if (!string.IsNullOrWhiteSpace(name))
- {
- bluetoothAim.filters = name;
- }
- else {
- bluetoothAim.filters = "";
- }
- }
- /// <summary>
- /// 设置是否使用名称连接
- /// </summary>
- /// <param name="useName">是否使用名称连接</param>
- public void SetIsConnectName(bool useName)
- {
- bluetoothAim.isConnectName = useName;
- }
- /// <summary>
- /// 设置连接的MAC地址
- /// </summary>
- /// <param name="macStr">MAC地址字符串</param>
- public void SetConnectMacStr(string macStr)
- {
- if (!string.IsNullOrWhiteSpace(macStr))
- {
- bluetoothAim.connectMacStr = macStr;
- }
- }
- /// <summary>
- /// 设置传感器类型,用来区分解析
- /// </summary>
- /// <param name="sensorAxisType"></param>
- public void SetSensorAxisType(SensorAxisType sensorAxisType)
- {
- bluetoothAim.sensorAxisType = sensorAxisType;
- SmartBowLogger.Log(this, "SetSensorAxisType:" + bluetoothAim.sensorAxisType);
- }
- /// <summary>
- /// 连接蓝牙模块
- /// 新增连接操作,需要用户标签和设备id
- /// userTags: 存储的信息标签,按实际用户区分
- /// deviceId:对应设备id,同一个用户下的不同设备
- /// bRecreateDeviceInfo: 重新创建设备信息。false 的情况下默认限制mac
- /// </summary>
- public void Connect(string userTags,int deviceId, bool bRecreateDeviceInfo = false)
- {
- if (userTags == null || userTags.Length == 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
- return;
- }
- if (deviceId <= 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
- return;
- }
- //判断mac需要创建一个连接信息
- bluetoothAim.initAimDeviceInfo(userTags,deviceId,bRecreateDeviceInfo);
- //进行蓝牙连接
- bluetoothAim.Connect();
- }
- /// <summary>
- /// 清空建立的蓝牙判断数据
- /// userTags: 存储的信息标签,按实际用户区分
- /// deviceId:对应设备id,同一个用户下的不同设备
- /// </summary>
- public void ClearDeviceInfo(string userTags,int deviceId)
- {
- if (userTags == null || userTags.Length == 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
- return;
- }
- if (deviceId <= 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
- return ;
- }
- bluetoothAim.onClearAimDeviceInfoByDeviceId(userTags, deviceId);
- }
- /// <summary>
- /// 获取存储的数据
- /// userTags: 存储的信息标签,按实际用户区分
- /// deviceId:对应设备id,同一个用户下的不同设备
- /// </summary>
- public AimDeviceInfo GetDeviceInfo(string userTags, int deviceId) {
- if (userTags == null || userTags.Length == 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,userTags不能为空!");
- return null;
- }
- if (deviceId <= 0)
- {
- SmartBowLogger.LogError(this, $"清除信息错误,deviceId 不能小于等于 0!");
- return null;
- }
- return bluetoothAim.onGetAimDeviceInfos(userTags, deviceId);
- }
- /// <summary>
- /// 断开蓝牙模块
- /// </summary>
- public void Disconnect()
- {
- bluetoothAim.Disconnect();
- }
- /// <summary>
- /// 获取蓝牙状态
- /// </summary>
- /// <returns>蓝牙状态</returns>
- public BluetoothStatusEnum GetBluetoothStatus()
- {
- return bluetoothAim.bluetoothStatus;
- }
- /// <summary>
- /// 监听蓝牙状态变化
- /// </summary>
- public event BluetoothStatusChangedEvent OnBluetoothStatusChanged;
- /// <summary>
- /// 判断蓝牙模块是否初始化完成
- /// </summary>
- /// <returns>蓝牙模块是否初始化完成</returns>
- public bool IsBluetoothModuleInited()
- {
- return bluetoothAim.moduleInited;
- }
- /// <summary>
- /// 监听蓝牙模块初始化完成
- /// Tip:连接蓝牙模块成功后会有几秒的初始化过程,初始化完成后才能进行传感器相关的函数调用。
- /// </summary>
- public Action OnBluetoothModuleInited;
- /// <summary>
- /// 监听蓝牙错误
- /// </summary>
- public event BluetoothErrorEvent OnBluetoothError;
- /// <summary>
- /// 获取Mac地址
- /// Tip:需要模块初始化完成才能获取
- /// </summary>
- /// <returns>如果返回null则是获取失败</returns>
- public string GetMacAddress()
- {
- return bluetoothAim.macAddress;
- }
- /// <summary>
- /// 获取硬件固件版本
- /// Tip:需要模块初始化完成才能获取
- /// </summary>
- /// <returns>如果返回null则是获取失败</returns>
- public string GetDeviceVersion()
- {
- return bluetoothAim.deviceVersion;
- }
- /// <summary>
- /// 获取电量
- /// Tip:需要模块初始化完成才能获取
- /// </summary>
- /// <returns>电量值(范围0~100),值为0时也代表获取失败</returns>
- public int GetBattery()
- {
- return bluetoothAim.battery;
- }
- /// <summary>
- /// 设置目标设备名称(用于搜索和连接)
- /// </summary>
- /// <param name="deviceName">设备名称</param>
- public void SetTargetDeviceName(string deviceName)
- {
- bluetoothAim.deviceConfig.deviceName = deviceName;
- }
- #endregion
- #region 传感器
- /// <summary>
- /// 开启九轴传感
- /// </summary>
- /// <returns>请求是否发送成功</returns>
- public bool StartRotationSensor()
- {
- return bluetoothAim.RequestOpen9Axis();
- }
- /// <summary>
- /// 停止九轴传感
- /// </summary>
- /// <returns>请求是否发送成功</returns>
- public bool StopRotationSensor()
- {
- return bluetoothAim.RequestClose9Axis();
- }
- /// <summary>
- /// 监听姿态角更新
- /// </summary>
- public event RotationUpdateEvent OnRotationUpdate;
- /// <summary>
- /// 开启射箭传感
- /// </summary>
- /// <returns>请求是否发送成功</returns>
- public bool StartShootingSensor()
- {
- return bluetoothAim.RequestOpenInfrared();
- }
- /// <summary>
- /// 停止射箭传感
- /// </summary>
- /// <returns>请求是否发送成功</returns>
- public bool StopShootingSensor()
- {
- return bluetoothAim.RequestCloseInfrared();
- }
- /// <summary>
- /// 监听射箭
- /// </summary>
- public ShootingEvent OnShooting;
- /// <summary>
- /// 开始陀螺仪校准
- /// </summary>
- public void StartGyrCalibration()
- {
- aimHandler.StartGyrCalibration();
- }
- /// <summary>
- /// 停止陀螺仪校准
- /// </summary>
- public void StopGyrCalibration()
- {
- aimHandler.StopGyrCalibration();
- }
- /// <summary>
- /// 判断陀螺仪是否正在校准
- /// </summary>
- /// <returns>陀螺仪是否正在校准</returns>
- public bool IsGyrCalibrating()
- {
- return aimHandler.IsGyrCalibrating();
- }
- /// <summary>
- /// 获取陀螺仪校准进度
- /// </summary>
- /// <returns>陀螺仪校准进度</returns>
- public float GetGyrProgress()
- {
- return aimHandler.GetGyrProgress();
- }
- /// <summary>
- /// 判断陀螺仪是否校准完成
- /// </summary>
- /// <returns>陀螺仪是否校准完成</returns>
- public bool IsGyrCompleted()
- {
- return aimHandler.IsGyrCompleted();
- }
- /// <summary>
- /// 开始地磁计校准
- /// </summary>
- public void StartMagCalibration()
- {
- aimHandler.StartMagCalibration();
- }
- /// <summary>
- /// 判断地磁计是否校准完成
- /// </summary>
- /// <returns>地磁计是否校准完成</returns>
- public bool IsMagCompleted()
- {
- return aimHandler.IsMagCompleted();
- }
- /// <summary>
- /// 视角归位
- /// </summary>
- public void ResetAim()
- {
- aimHandler.DoIdentity();
- }
- /// <summary>
- /// 监听模块的功能键短按
- /// </summary>
- public Action OnFunctionKeyPress;
- /// <summary>
- /// 监听模块的功能键长按
- /// </summary>
- public Action OnFunctionKeyLongPress;
- #endregion
- /// <summary>
- /// 蓝牙状态变化事件
- /// </summary>
- /// <param name="oldStatus">旧状态</param>
- /// <param name="newStatus">新状态</param>
- public delegate void BluetoothStatusChangedEvent(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus);
- internal void InvokeOnBluetoothStatusChanged(BluetoothStatusEnum oldStatus, BluetoothStatusEnum newStatus)
- {
- try
- {
- OnBluetoothStatusChanged?.Invoke(oldStatus, newStatus);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- /// <summary>
- /// 蓝牙错误事件
- /// </summary>
- /// <param name="error">错误类型</param>
- /// <param name="message">消息描述</param>
- public delegate void BluetoothErrorEvent(BluetoothError error, string message);
- internal void InvokeOnBluetoothError(BluetoothError error, string message)
- {
- try
- {
- OnBluetoothError?.Invoke(error, message);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- internal void InvokeOnFunctionKeyPress()
- {
- try
- {
- OnFunctionKeyPress?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- internal void InvokeOnFunctionKeyLongPress()
- {
- try
- {
- OnFunctionKeyLongPress?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- /// <summary>
- /// 姿态角更新事件
- /// </summary>
- /// <param name="rotation">姿态角</param>
- public delegate void RotationUpdateEvent(Quaternion rotation);
- internal void InvokeOnRotationUpdate(Quaternion rotation)
- {
- if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
- try
- {
- OnRotationUpdate?.Invoke(rotation);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- /// <summary>
- /// 射箭事件
- /// </summary>
- /// <param name="speed">速度(m/s)</param>
- public delegate void ShootingEvent(float speed);
- internal void InvokeOnShooting(float speed)
- {
- try
- {
- OnShooting?.Invoke(speed);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- internal void InvokeOnBluetoothModuleInited()
- {
- try
- {
- OnBluetoothModuleInited?.Invoke();
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- /// <summary>
- /// 监听枪状态
- /// </summary>
- public BleDeviceEvent OnBleDeviceState;
- /// <summary>
- /// 硬件对应的事件
- /// </summary>
- public delegate void BleDeviceEvent(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum);
- internal void InvokeOnBleDevice(BluetoothDeviceType bleDeviceType, BluetoothDeviceStatus gunStatusEnum)
- {
- try
- {
- OnBleDeviceState?.Invoke(bleDeviceType,gunStatusEnum);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- /// <summary>
- /// 监听设备状态和连接的设备平台信息
- /// </summary>
- public DeviceAndSystemInfoEvent OnDeviceAndSystemInfoEvent;
- /// <summary>
- /// 硬件对应的事件初始化回调
- /// </summary>
- public delegate void DeviceAndSystemInfoEvent(ConnectPlatform connectPlatform,BluetoothDeviceType bleDeviceType);
- internal void InvokeOnDeviceAndSystemInfoEvent(ConnectPlatform connectPlatform, BluetoothDeviceType bleDeviceType)
- {
- try
- {
- OnDeviceAndSystemInfoEvent?.Invoke(connectPlatform, bleDeviceType);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- #region 6轴部分接口
- /// <summary>
- /// 6轴数据重置初始位置数据
- /// </summary>
- public void ResetSixAxisIdentity()
- {
- bluetoothAim.ResetSixAxisFusion();
- }
- /// <summary>
- /// 监听姿态角更新
- /// </summary>
- public event SixAxisRotationUpdateEvent OnSixAxisRotationUpdate;
- /// <summary>
- /// 姿态角更新事件
- /// </summary>
- /// <param name="rotation">姿态角</param>
- public delegate void SixAxisRotationUpdateEvent(Vector3 eulerAngles);
- internal void InvokeOnSixAxisRotationUpdate(Vector3 eulerAngles)
- {
- if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
- try
- {
- OnSixAxisRotationUpdate?.Invoke(eulerAngles);
- }
- catch (Exception e)
- {
- Debug.LogError(e);
- }
- }
- ///// <summary>
- ///// 芯片数据更新事件
- ///// </summary>
- ///// <param name="rotation">姿态角</param>
- //public delegate void AxisDataUpdateEvent(byte[] value);
- //internal void InvokeOnAxisDataUpdateEvent(byte[] value)
- //{
- // if (bluetoothAim.bluetoothStatus != BluetoothStatusEnum.Connected) return;
- // try
- // {
- // OnAxisDataUpdate?.Invoke(value);
- // }
- // catch (Exception e)
- // {
- // Debug.LogError(e);
- // }
- //}
- ///// <summary>
- ///// 芯片数据更新
- ///// </summary>
- //public event AxisDataUpdateEvent OnAxisDataUpdate;
- #endregion
- }
- }
|