Files
FutureMen/Assets/Scripts/Manager/TrueGearEffectManager.cs
2025-08-13 15:39:59 +08:00

243 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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
TrueGearEffectManager.Ins.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<DeviceData> 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<DeviceData> 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;
return;
}
}
Debug.Log("No device found");
}
/// <summary>
/// 断开连接
/// </summary>
public void CloseConnect()
{
TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance;
androidConnector.DisconnectFromDevice();
}
private void OnApplicationQuit()
{
//断开连接
Debug.Log("断开连接");
#if !UNITY_EDITOR
CloseConnect();
#endif
}
/// <summary>
/// 触发TrueGear震动
/// </summary>
/// <param name="effectName">效果名,可自定义</param>
/// <param name="motorIndex">作用的电机ID列表</param>
/// <param name="startTime">起始时间单位ms</param>
/// <param name="endTime">结束时间单位ms</param>
/// <param name="startIntensity">起始强度0-100</param>
/// <param name="endIntensity">结束强度0-100</param>
/// <param name="intensityMode">强度模式Const/Fade/FadeInAndOut</param>
/// <param name="actionType">震动位置</param>
public void PlayVibrationEffect(
string effectName,
List<int> motorIndex,
int startTime = 0,
int endTime = 300,
int startIntensity = 30,
int endIntensity = 30,
string intensityMode = "Const",
string actionType="Shake"
)
{
string indexStr = string.Join(",", motorIndex);
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"":""False"",
""interval"":0,
""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<string> hitParts=new List<string>();
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)
{
List<int> motorIDs = new List<int>();
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;
default:
motorIDs.Add(0); break;
}
if(isArm)
motorIDs.Add(0);
PlayVibrationEffect(
effectName: $"Hit_{hitPart}",
motorIndex: motorIDs,
startIntensity: 50,
endIntensity: 50,
endTime: 400,
actionType:isArm? "elect" :"shack"
);
}
}