BoxUserSettings.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }
  83. }
  84. }
  85. /**
  86. * 调试
  87. */
  88. public void OnClick_EnterDebuggingInfrared() {
  89. if (InfraredDemo._ins != null) {
  90. InfraredDemo._ins.OnClick_See();
  91. }
  92. }
  93. public void OnClick_EnterDeviceConnect() {
  94. AudioMgr.ins.PlayBtn();
  95. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  96. }
  97. public void OnClick_EnterInfraredCenterCalibration()
  98. {
  99. //弹出校准提示
  100. AutoResetView.onInfraredGuiderAutoResetViewOrBg();
  101. //红外设备校准偏离点
  102. //InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  103. }
  104. public void OnClick_ScreenCalibration()
  105. {
  106. //进入屏幕定位
  107. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  108. }
  109. /// <summary>
  110. /// 根据名字刷新渲染
  111. /// </summary>
  112. /// <param name="deviceName"></param>
  113. public void OnUpdateClickInfoByName(string deviceName) {
  114. // 如果名称存在于数组中,则获取其索引,否则默认选第一个
  115. selectDevicesIndex = System.Array.IndexOf(SelectDevicesStrs, deviceName);
  116. if (selectDevicesIndex < 0) selectDevicesIndex = 0;
  117. UserSettings.ins.selectDevicesName = SelectDevicesStrs[selectDevicesIndex];
  118. UserSettings.ins.Save();
  119. // 渲染选择的设备
  120. RenderSelectDevicesIndex(selectDevicesIndex);
  121. }
  122. // Update is called once per frame
  123. //void Update()
  124. //{
  125. //}
  126. }
  127. }