添加选择马甲初版
This commit is contained in:
8
Assets/_Zombie/Editor.meta
Normal file
8
Assets/_Zombie/Editor.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d8672051715c16429b86421b92b9686
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
File diff suppressed because it is too large
Load Diff
@ -3913,6 +3913,7 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: e7c907821bb6e484689d92d3dc05f5a0, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
enableTrueGear: 1
|
||||
isGetConnect: 0
|
||||
hitParts: []
|
||||
--- !u!1 &495846439
|
||||
@ -6786,7 +6787,7 @@ MonoBehaviour:
|
||||
IncludeChildColliders: 0
|
||||
RuntimeFollower: {fileID: 0}
|
||||
colliderMaxDistance: 100
|
||||
lastTimeMaxDistanceCalced: 639100281025242179
|
||||
lastTimeMaxDistanceCalced: 639111773777905388
|
||||
--- !u!1 &679496949
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@ -6578,7 +6578,7 @@ MonoBehaviour:
|
||||
IncludeChildColliders: 0
|
||||
RuntimeFollower: {fileID: 0}
|
||||
colliderMaxDistance: 100
|
||||
lastTimeMaxDistanceCalced: 639111545265390333
|
||||
lastTimeMaxDistanceCalced: 639111585956252531
|
||||
--- !u!1 &679496949
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@ -10,6 +10,14 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
|
||||
public static TrueGearEffectManager Ins;
|
||||
|
||||
// 马甲功能开关(在Unity编辑器中配置)
|
||||
[SerializeField] private bool enableTrueGear = false;
|
||||
|
||||
/// <summary>
|
||||
/// 是否启用TrueGear马甲功能
|
||||
/// </summary>
|
||||
public bool EnableTrueGear => enableTrueGear;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Ins = this;
|
||||
@ -17,9 +25,7 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
isAvailableIndex = 20;
|
||||
GetConnectIndex = 20;
|
||||
AddHitPart();
|
||||
#if !UNITY_EDITOR
|
||||
TrueGearEffectManager.Ins.StartRequestTrueGear();
|
||||
#endif
|
||||
// 不再自动连接,等待用户选择设备后再连接
|
||||
}
|
||||
|
||||
|
||||
@ -28,6 +34,17 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化TrueGear蓝牙(仅初始化,不连接)
|
||||
/// </summary>
|
||||
public void InitTrueGear()
|
||||
{
|
||||
TruegearAndroidConnector androidConnector = new TruegearAndroidConnector();
|
||||
androidConnector.InitShellJavaObject();
|
||||
androidConnector.RequestPermission();
|
||||
Debug.Log("TrueGear蓝牙初始化完成");
|
||||
}
|
||||
|
||||
public void StartRequestTrueGear()
|
||||
{
|
||||
TruegearAndroidConnector androidConnector = new TruegearAndroidConnector();
|
||||
@ -41,6 +58,97 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
|
||||
private int isAvailableIndex = 0;
|
||||
private int GetConnectIndex;
|
||||
|
||||
// 用于停止扫描和连接的标志
|
||||
private bool shouldStopScan = false;
|
||||
private Coroutine scanCoroutine;
|
||||
|
||||
/// <summary>
|
||||
/// 停止扫描和连接
|
||||
/// </summary>
|
||||
public void StopScan()
|
||||
{
|
||||
shouldStopScan = true;
|
||||
if (scanCoroutine != null)
|
||||
{
|
||||
StopCoroutine(scanCoroutine);
|
||||
scanCoroutine = null;
|
||||
}
|
||||
Debug.Log("已停止TrueGear扫描和连接");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置扫描状态(当用户选择设备时调用)
|
||||
/// </summary>
|
||||
public void ResetScanState()
|
||||
{
|
||||
shouldStopScan = false;
|
||||
isAvailableIndex = 20;
|
||||
GetConnectIndex = 20;
|
||||
}
|
||||
|
||||
// 设备选择相关
|
||||
public int SelectedDeviceIndex { get; set; }
|
||||
private List<DeviceData> scannedDevices = new List<DeviceData>();
|
||||
|
||||
/// <summary>
|
||||
/// 获取扫描到的设备列表
|
||||
/// </summary>
|
||||
public List<DeviceData> GetDeviceList()
|
||||
{
|
||||
TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance;
|
||||
if (androidConnector == null || !androidConnector.IsAvailable())
|
||||
{
|
||||
return new List<DeviceData>();
|
||||
}
|
||||
scannedDevices = androidConnector.GetScanedDevices();
|
||||
return scannedDevices;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取选中设备的地址
|
||||
/// </summary>
|
||||
public string GetSelectedDeviceAddress()
|
||||
{
|
||||
if (scannedDevices != null && SelectedDeviceIndex >= 0 && SelectedDeviceIndex < scannedDevices.Count)
|
||||
{
|
||||
return scannedDevices[SelectedDeviceIndex].address;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据索引连接指定设备
|
||||
/// </summary>
|
||||
public void ConnectToDeviceByIndex(int index)
|
||||
{
|
||||
if (scannedDevices == null || index < 0 || index >= scannedDevices.Count)
|
||||
{
|
||||
Debug.LogWarning("无效的设备索引");
|
||||
return;
|
||||
}
|
||||
|
||||
TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance;
|
||||
if (androidConnector == null || !androidConnector.IsAvailable())
|
||||
{
|
||||
Debug.LogWarning("TrueGear 蓝牙不可用");
|
||||
return;
|
||||
}
|
||||
|
||||
DeviceData device = scannedDevices[index];
|
||||
bool connected = androidConnector.ConnectToDevice(device.address);
|
||||
if (connected)
|
||||
{
|
||||
Debug.Log($"成功连接设备: {device.name} - {device.address}");
|
||||
isGetConnect = true;
|
||||
SelectedDeviceIndex = index;
|
||||
ChangeElectricalLevel();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"设备连接失败: {device.name} - {device.address}");
|
||||
}
|
||||
}
|
||||
IEnumerator TrueGearAndroidConnector()
|
||||
{
|
||||
TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance;
|
||||
@ -258,6 +366,12 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
}
|
||||
public void OnHit(bool isUp, int index,bool isArm)
|
||||
{
|
||||
// 如果未启用马甲功能,则跳过
|
||||
if (!enableTrueGear)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TruegearAndroidConnector androidConnector = TruegearAndroidConnector.Instance;
|
||||
if (androidConnector == null)
|
||||
{
|
||||
@ -275,9 +389,8 @@ public class TrueGearEffectManager : MonoBehaviour
|
||||
|
||||
if (!isGetConnect)
|
||||
{
|
||||
androidConnector.InitShellJavaObject();
|
||||
androidConnector.RequestPermission();
|
||||
StartCoroutine(TrueGearAndroidConnector());
|
||||
// 不再自动连接,只记录状态
|
||||
Debug.LogWarning("TrueGear 未连接,跳过震动");
|
||||
return;
|
||||
}
|
||||
List<int> motorIDs = new List<int>();
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XUI;
|
||||
using TMPro;
|
||||
using TruegearSdk;
|
||||
|
||||
public class ConPanel : UIBehaviour
|
||||
{
|
||||
@ -12,7 +14,6 @@ public class ConPanel : UIBehaviour
|
||||
public List<IPBtn> IPBtns;
|
||||
public bool IsInit = false;
|
||||
|
||||
|
||||
public static void Show()
|
||||
{
|
||||
WorldUIManager.Ins.Cover("UI/ConPanel", false);
|
||||
@ -26,6 +27,7 @@ public class ConPanel : UIBehaviour
|
||||
btn.gameObject.SetActive(false);
|
||||
IPBtns.Add(btn);
|
||||
}
|
||||
|
||||
IsInit = true;
|
||||
}
|
||||
|
||||
@ -40,6 +42,11 @@ public class ConPanel : UIBehaviour
|
||||
|
||||
public void OnClickHost()
|
||||
{
|
||||
// 用户未选择设备直接开始游戏,停止扫描
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.StopScan();
|
||||
}
|
||||
WorldUIManager.Ins.Back();
|
||||
MRNetworkManager.Ins.CreateAndJoinRoom();
|
||||
MRNetworkManager.Ins.networkDiscovery.AdvertiseServer();
|
||||
@ -79,6 +86,11 @@ public class ConPanel : UIBehaviour
|
||||
|
||||
public void OnOnePlayerClick()
|
||||
{
|
||||
// 用户未选择设备直接开始游戏,停止扫描
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.StopScan();
|
||||
}
|
||||
GameLocal.Ins.GameMode = 0;
|
||||
WorldUIManager.Ins.Back();
|
||||
MRNetworkManager.Ins.CreateAndJoinRoom();
|
||||
@ -87,6 +99,11 @@ public class ConPanel : UIBehaviour
|
||||
|
||||
public void OnServerPlayerClick()
|
||||
{
|
||||
// 用户未选择设备直接开始游戏,停止扫描
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.StopScan();
|
||||
}
|
||||
GameLocal.Ins.GameMode = 1;
|
||||
WorldUIManager.Ins.Back();
|
||||
MRNetworkManager.Ins.CreateAndJoinRoom();
|
||||
|
||||
232
Assets/_Zombie/Scripts/UI/TrueGearPanel.cs
Normal file
232
Assets/_Zombie/Scripts/UI/TrueGearPanel.cs
Normal file
@ -0,0 +1,232 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using XUI;
|
||||
using TMPro;
|
||||
using TruegearSdk;
|
||||
|
||||
public class TrueGearPanel : UIBehaviour
|
||||
{
|
||||
// TrueGear设备选择
|
||||
public TMP_Dropdown deviceDropdown;
|
||||
public UnityEngine.UI.Button connectButton;
|
||||
public UnityEngine.UI.Button refreshButton;
|
||||
private List<DeviceData> scannedDevices = new List<DeviceData>();
|
||||
private int selectedDeviceIndex = -1;
|
||||
|
||||
// 连接成功反馈的Text组件
|
||||
public UnityEngine.UI.Text connectStatusText;
|
||||
|
||||
// 面板的GameObject(用于隐藏/显示)
|
||||
public GameObject panelGameObject;
|
||||
|
||||
public override void OnUIShow(params object[] args)
|
||||
{
|
||||
base.OnUIShow(args);
|
||||
|
||||
// 从TrueGearEffectManager获取开关状态
|
||||
bool isEnabled = TrueGearEffectManager.Ins != null && TrueGearEffectManager.Ins.EnableTrueGear;
|
||||
|
||||
// 如果未启用马甲功能,则隐藏面板并返回
|
||||
if (!isEnabled)
|
||||
{
|
||||
Debug.Log("TrueGear功能未启用,隐藏面板");
|
||||
if (panelGameObject != null)
|
||||
{
|
||||
panelGameObject.SetActive(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// 启用功能时显示面板
|
||||
if (panelGameObject != null)
|
||||
{
|
||||
panelGameObject.SetActive(true);
|
||||
}
|
||||
|
||||
// 打开面板时自动扫描设备
|
||||
ScanAndPopulateDevices();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描设备并填充Dropdown
|
||||
/// </summary>
|
||||
private void ScanAndPopulateDevices()
|
||||
{
|
||||
if (deviceDropdown == null)
|
||||
{
|
||||
Debug.LogWarning("DeviceDropdown 未设置");
|
||||
return;
|
||||
}
|
||||
|
||||
// 先初始化蓝牙
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.InitTrueGear();
|
||||
}
|
||||
|
||||
// 延迟1秒后开始扫描,等待蓝牙初始化完成
|
||||
StartCoroutine(InitAndScanCoroutine());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化蓝牙后开始扫描
|
||||
/// </summary>
|
||||
private System.Collections.IEnumerator InitAndScanCoroutine()
|
||||
{
|
||||
// 增加等待时间,确保蓝牙初始化完成
|
||||
yield return new WaitForSeconds(2f);
|
||||
|
||||
// 开始扫描
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.StartScanTrueGear();
|
||||
}
|
||||
|
||||
// 延迟获取设备列表,等待扫描完成
|
||||
StartCoroutine(ScanDevicesCoroutine());
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator ScanDevicesCoroutine()
|
||||
{
|
||||
// 等待3秒让扫描完成
|
||||
yield return new WaitForSeconds(3f);
|
||||
|
||||
// 获取设备列表
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
Debug.Log("开始获取设备列表...");
|
||||
scannedDevices = TrueGearEffectManager.Ins.GetDeviceList();
|
||||
Debug.Log($"扫描到设备数量: {scannedDevices?.Count ?? 0}");
|
||||
PopulateDropdown();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 填充Dropdown选项
|
||||
/// </summary>
|
||||
private void PopulateDropdown()
|
||||
{
|
||||
if (deviceDropdown == null) return;
|
||||
|
||||
deviceDropdown.ClearOptions();
|
||||
|
||||
if (scannedDevices == null || scannedDevices.Count == 0)
|
||||
{
|
||||
// 没有设备时显示提示
|
||||
List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
|
||||
options.Add(new TMP_Dropdown.OptionData("未扫描到设备"));
|
||||
deviceDropdown.AddOptions(options);
|
||||
return;
|
||||
}
|
||||
|
||||
List<TMP_Dropdown.OptionData> deviceOptions = new List<TMP_Dropdown.OptionData>();
|
||||
foreach (var device in scannedDevices)
|
||||
{
|
||||
// 显示设备ID(address)
|
||||
string displayText = device.address;
|
||||
deviceOptions.Add(new TMP_Dropdown.OptionData(displayText));
|
||||
}
|
||||
deviceDropdown.AddOptions(deviceOptions);
|
||||
|
||||
// 如果只有一台设备,自动选中
|
||||
if (scannedDevices.Count == 1)
|
||||
{
|
||||
selectedDeviceIndex = 0;
|
||||
deviceDropdown.value = 0;
|
||||
Debug.Log($"只有一台设备,自动选中: {scannedDevices[0].address}");
|
||||
}
|
||||
|
||||
// 添加Dropdown选择事件
|
||||
deviceDropdown.onValueChanged.RemoveAllListeners();
|
||||
deviceDropdown.onValueChanged.AddListener(OnDeviceDropdownChanged);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备Dropdown选择改变时调用
|
||||
/// </summary>
|
||||
public void OnDeviceDropdownChanged(int index)
|
||||
{
|
||||
if (index < 0 || index >= scannedDevices.Count)
|
||||
{
|
||||
Debug.LogWarning("无效的设备索引");
|
||||
return;
|
||||
}
|
||||
|
||||
// 只保存选中的设备索引,不自动连接
|
||||
selectedDeviceIndex = index;
|
||||
Debug.Log($"选中设备索引: {index}, 地址: {scannedDevices[index].address}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接按钮点击事件
|
||||
/// </summary>
|
||||
public void OnClickConnectButton()
|
||||
{
|
||||
if (selectedDeviceIndex < 0 || selectedDeviceIndex >= scannedDevices.Count)
|
||||
{
|
||||
Debug.LogWarning("请先选择要连接的设备");
|
||||
ShowConnectStatus("请先选择要连接的设备", false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 连接选中的设备
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.ConnectToDeviceByIndex(selectedDeviceIndex);
|
||||
|
||||
// 检查是否连接成功
|
||||
if (TrueGearEffectManager.Ins.isGetConnect)
|
||||
{
|
||||
ShowConnectStatus("连接成功!", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowConnectStatus("连接失败,请重试", false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示连接状态
|
||||
/// </summary>
|
||||
private void ShowConnectStatus(string message, bool isSuccess)
|
||||
{
|
||||
if (connectStatusText != null)
|
||||
{
|
||||
connectStatusText.text = message;
|
||||
connectStatusText.color = isSuccess ? Color.green : Color.red;
|
||||
}
|
||||
Debug.Log($"连接状态: {message}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 刷新按钮点击事件
|
||||
/// </summary>
|
||||
public void OnClickRefreshButton()
|
||||
{
|
||||
Debug.Log("点击刷新按钮,重新扫描设备...");
|
||||
|
||||
// 重新初始化并扫描
|
||||
if (TrueGearEffectManager.Ins != null)
|
||||
{
|
||||
TrueGearEffectManager.Ins.InitTrueGear();
|
||||
}
|
||||
|
||||
StartCoroutine(InitAndScanCoroutine());
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
// 注册连接按钮点击事件
|
||||
if (connectButton != null)
|
||||
{
|
||||
connectButton.onClick.AddListener(OnClickConnectButton);
|
||||
}
|
||||
|
||||
// 注册刷新按钮点击事件
|
||||
if (refreshButton != null)
|
||||
{
|
||||
refreshButton.onClick.AddListener(OnClickRefreshButton);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/_Zombie/Scripts/UI/TrueGearPanel.cs.meta
Normal file
11
Assets/_Zombie/Scripts/UI/TrueGearPanel.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 752e5b94c1848a74da7497e498739cf6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -17,7 +17,7 @@ EditorBuildSettings:
|
||||
- enabled: 0
|
||||
path: Assets/_Zombie/Scenes/Chongqing_Yuzhong_Hongyadong_Xiakexing.unity
|
||||
guid: ea7144ccc37aa02459fe416f33210900
|
||||
- enabled: 0
|
||||
- enabled: 1
|
||||
path: Assets/_Zombie/Scenes/Company1Floor.unity
|
||||
guid: 386a8fdea01af8a4e8d4a9835407ddec
|
||||
- enabled: 0
|
||||
@ -29,7 +29,7 @@ EditorBuildSettings:
|
||||
- enabled: 0
|
||||
path: Assets/_Zombie/Scenes/Jilin_Changchun_Beihu_WyueGuangchang.unity
|
||||
guid: 6ff31f8be37608e43b0ae8e8ae348095
|
||||
- enabled: 1
|
||||
- enabled: 0
|
||||
path: Assets/_Zombie/Scenes/Jiangxi_Ganzhou_Zhanggong_GanzhouShucheng.unity
|
||||
guid: 72cacd349decdb941938da562d3ce54f
|
||||
m_configObjects:
|
||||
|
||||
@ -13,7 +13,7 @@ PlayerSettings:
|
||||
useOnDemandResources: 0
|
||||
accelerometerFrequency: 60
|
||||
companyName: pineappletech
|
||||
productName: "\u50F5\u5C38\u6765\u4E86"
|
||||
productName: "\u50F5\u5C38\u6765\u4E86_\u6D4B\u8BD5"
|
||||
defaultCursor: {fileID: 0}
|
||||
cursorHotspot: {x: 0, y: 0}
|
||||
m_SplashScreenBackgroundColor: {r: 0.13725491, g: 0.12156863, b: 0.1254902, a: 1}
|
||||
@ -163,7 +163,7 @@ PlayerSettings:
|
||||
androidSupportedAspectRatio: 1
|
||||
androidMaxAspectRatio: 2.1
|
||||
applicationIdentifier:
|
||||
Android: com.pineappletech.zombie.jiangxiganzhouzhanggongganzhoushucheng
|
||||
Android: com.pineappletech.zombie.gongsiyilou
|
||||
Standalone: com.DefaultCompany.com.unity.template.ar
|
||||
buildNumber:
|
||||
Standalone: 0
|
||||
|
||||
Reference in New Issue
Block a user