BoxUserSettings.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace CustomUIView
  6. {
  7. public class BoxUserSettings : MonoBehaviour
  8. {
  9. [SerializeField] Transform boxSelectDevices;
  10. [SerializeField] GameObject[] boxDevices;
  11. readonly string[] SelectDevicesStrs = { "ARTEMIS Pro", "Pistol M9", "HOUYI Pro" };
  12. int selectDevicesIndex = 0; // 用于保存当前选择的设备索引
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. for (int i = 0; i < boxDevices.Length; i++)
  17. {
  18. Text text = boxDevices[i].GetComponentInChildren<Text>();
  19. text.text = SelectDevicesStrs[i];
  20. Button button = boxDevices[i].GetComponent<Button>();
  21. string deviceName = SelectDevicesStrs[i]; // 捕获当前的设备名称
  22. button.onClick.AddListener(() =>
  23. {
  24. OnClick_SelectDevice(deviceName);
  25. });
  26. }
  27. // 获取用户设置中保存的设备名称
  28. string selectDeviceName = UserSettings.ins.selectDevicesName;
  29. // 如果名称存在于数组中,则获取其索引,否则默认选第一个
  30. selectDevicesIndex = System.Array.IndexOf(SelectDevicesStrs, selectDeviceName);
  31. if (selectDevicesIndex < 0) selectDevicesIndex = 0;
  32. // 渲染选择的设备
  33. RenderSelectDevicesIndex(selectDevicesIndex);
  34. }
  35. public void RenderSelectDevicesIndex(int value)
  36. {
  37. for (int i = 0; i < SelectDevicesStrs.Length; i++)
  38. {
  39. var item = boxSelectDevices.GetChild(i);
  40. var text = item.GetComponentInChildren<Text>();
  41. // 根据是否为选中设备设置颜色
  42. if (i == value)
  43. {
  44. item.GetComponent<Image>().color = new Color(16 / 255f, 194 / 255f, 198 / 255f, 1f);
  45. }
  46. else
  47. {
  48. item.GetComponent<Image>().color = new Color(96 / 255f, 96 / 255f, 96 / 255f, 1f);
  49. }
  50. // 设置设备名称
  51. text.text = SelectDevicesStrs[i];
  52. }
  53. }
  54. // 当用户点击选择设备时调用
  55. void OnClick_SelectDevice(string name)
  56. {
  57. // 获取选中设备的索引
  58. int newValue = System.Array.IndexOf(SelectDevicesStrs, name);
  59. // 如果选择不同设备,更新选择
  60. if (selectDevicesIndex != newValue)
  61. {
  62. // 播放按钮音效
  63. AudioMgr.ins.PlayBtn();
  64. // 更新选择的设备索引和名称
  65. selectDevicesIndex = newValue;
  66. UserSettings.ins.selectDevicesName = SelectDevicesStrs[selectDevicesIndex];
  67. UserSettings.ins.Save();
  68. // 渲染更新后的选择
  69. RenderSelectDevicesIndex(selectDevicesIndex);
  70. //todo 调用串口后续流程?
  71. switch (UserSettings.ins.selectDevicesName)
  72. {
  73. case "ARTEMIS Pro":
  74. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
  75. break;
  76. case "Pistol M9":
  77. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
  78. break;
  79. case "HOUYI Pro":
  80. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
  81. break;
  82. case "Pistol M17":
  83. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.PistolM17);
  84. break;
  85. case "Rifle M416":
  86. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.RifleM416);
  87. break;
  88. }
  89. }
  90. }
  91. /**
  92. * 调试
  93. */
  94. public void OnClick_EnterDebuggingInfrared() {
  95. if (InfraredDemo._ins != null) {
  96. InfraredDemo._ins.OnClick_See();
  97. }
  98. }
  99. public void OnClick_EnterDeviceConnect() {
  100. AudioMgr.ins.PlayBtn();
  101. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  102. }
  103. public void OnClick_EnterInfraredCenterCalibration()
  104. {
  105. //弹出校准提示
  106. AutoResetView.onInfraredGuiderAutoResetViewOrBg();
  107. //红外设备校准偏离点
  108. //InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  109. }
  110. public void OnClick_ScreenCalibration()
  111. {
  112. //进入屏幕定位
  113. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  114. }
  115. /// <summary>
  116. /// 根据名字刷新渲染
  117. /// </summary>
  118. public void OnUpdateClickInfoByName() {
  119. // 获取用户设置中保存的设备名称
  120. string selectDeviceName = UserSettings.ins.selectDevicesName;
  121. // 如果名称存在于数组中,则获取其索引,否则默认选第一个
  122. selectDevicesIndex = System.Array.IndexOf(SelectDevicesStrs, selectDeviceName);
  123. if (selectDevicesIndex < 0) selectDevicesIndex = 0;
  124. // 渲染选择的设备
  125. RenderSelectDevicesIndex(selectDevicesIndex);
  126. }
  127. // Update is called once per frame
  128. //void Update()
  129. //{
  130. //}
  131. }
  132. }