using System; using System.Collections; using System.Collections.Generic; using BehaviorDesigner.Runtime.Tasks; using TruegearSdk; using UnityEngine; public class TrueGearEffectManager : MonoBehaviour { public static TrueGearEffectManager Ins; private void Awake() { Ins = this; isAvailableIndex = 20; GetConnectIndex = 20; AddHitPart(); #if !UNITY_EDITOR StartRequestTrueGear(); #endif } private void Start() { } public void StartRequestTrueGear() { TruegearAndroidConnector androidConnector = new TruegearAndroidConnector(); androidConnector.InitShellJavaObject(); androidConnector.RequestPermission(); Debug.Log("开始连接"); StartCoroutine(TrueGearAndroidConnector()); } public bool isGetConnect; private int isAvailableIndex = 0; private int GetConnectIndex; IEnumerator TrueGearAndroidConnector() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool res = androidConnector.IsAvailable(); Debug.Log("蓝牙是否授权成功:"+res); while (!res&& isAvailableIndex>0) { yield return new WaitForSeconds(1f); Debug.Log("蓝牙尝试连接中...."); androidConnector.InitShellJavaObject(); androidConnector.RequestPermission(); res = androidConnector.IsAvailable(); isAvailableIndex--; } if (isAvailableIndex <= 0) { Debug.Log("蓝牙授权失败"); } else { Debug.Log("蓝牙授权成功"); StartScanTrueGear(); while (!isGetConnect&& GetConnectIndex>0) { GetScanedDevices(); StartConnect(); yield return new WaitForSeconds(1); Debug.Log("正在重新连接..."); GetConnectIndex--; } } } //搜索设备 public void StartScanTrueGear() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool res = androidConnector.IsAvailable(); if (res) androidConnector.StartScan(); Debug.Log("OnStartScanClick" + res); } //获取设备列表 public void GetScanedDevices() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool res = androidConnector.IsAvailable(); Debug.Log("OnGetScanedDevicesClick" + res); if (!res) return; List res1 = androidConnector.GetScanedDevices(); string str = ""; foreach (var item in res1) { str += (string.Format("{0}, {1} ", item.name, item.address)); } Debug.Log("OnGetScanedDevicesClick " + str); } //开始连接设备 public void StartConnect() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool res = androidConnector.IsAvailable(); Debug.Log("OnStartConnectClick" + res); if (!res) return; List res1 = androidConnector.GetScanedDevices(); Debug.Log(res1.ToString()); foreach (var item in res1) { { androidConnector.ConnectToDevice(item.address); Debug.Log(string.Format("Device Connect: {0}, {1}", item.name, item.address)); isGetConnect = true; ChangeElectricalLevel(); return; } } Debug.Log("No device found"); } public void ChangeElectricalLevel() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool isZD= androidConnector.ModifyElectricalPercent(80); Debug.LogError("是否震动:"+isZD); } /// /// 断开连接 /// public void CloseConnect() { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; androidConnector.DisconnectFromDevice(); } private void OnApplicationQuit() { //断开连接 Debug.Log("断开连接"); #if !UNITY_EDITOR CloseConnect(); #endif } /// /// 触发TrueGear震动 /// /// 效果名,可自定义 /// 作用的电机ID列表 /// 起始时间,单位ms /// 结束时间,单位ms /// 起始强度(0-100) /// 结束强度(0-100) /// 强度模式:Const/Fade/FadeInAndOut /// 震动位置 public void PlayVibrationEffect( string effectName, List motorIndex, int startTime = 0, int endTime = 300, int startIntensity = 30, int endIntensity = 30, string intensityMode = "Const", string actionType="Shake" ) { string indexStr = string.Join(",", motorIndex); Debug.Log(indexStr); string json = $@" {{ ""name"":""{effectName}"", ""uuid"":""{effectName}"", ""keep"":""False"", ""priority"": 0, ""tracks"":[ {{ ""start_time"":{startTime}, ""end_time"":{endTime}, ""stop_name"":"""", ""start_intensity"":{startIntensity}, ""end_intensity"":{endIntensity}, ""intensity_mode"":""{intensityMode}"", ""action_type"":""{actionType}"", ""once"":""True"", ""interval"":1, ""index"":[{indexStr}] }} ] }}"; SendPlayEffectByContent(json); } public void SendPlayEffectByContent(string jsonStr) { TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance; bool res = androidConnector.IsAvailable(); Debug.Log("OnTestClick" + res); if (!res) return; androidConnector.SendPlayEffectByContent(jsonStr); } public List hitParts=new List(); public void AddHitPart() { hitParts.Add("leftUp"); hitParts.Add("leftDown"); hitParts.Add("rightUp"); hitParts.Add("rightDown"); hitParts.Add("rightAim"); } public void OnHit(bool isUp, int index,bool isArm) { //ChangeElectricalLevel(); List motorIDs = new List(); string hitPart= hitParts[index]; switch (hitPart) { case "leftUp": motorIDs.AddRange( isUp?new int[] { 0, 1, 4,5 }: new int[] { 100, 101, 104,105 }); break; case "leftDown": motorIDs.AddRange(isUp?new int[] { 8, 9, 12,13 }: new int[] { 108, 109, 112,113 }); break; case "rightUp": motorIDs.AddRange(isUp?new int[] { 2,3,6,7}: new int[] { 102, 103, 106,107 }); break; case "rightDown": motorIDs.AddRange(isUp?new int[] { 10,11,14,15}: new int[] { 114, 115, 118,119 }); break; case "rightAim": Debug.Log("xxzz"); motorIDs.AddRange(new int[] { 0,100}); break; default: motorIDs.Add(0); break; } PlayVibrationEffect( effectName: $"Hit_{hitPart}", motorIndex: motorIDs, startIntensity: isArm?20:50, endIntensity: isArm?20:50, endTime: 400, actionType: isArm? "Electrical" :"Shake" ); } }