BoxUserSettings.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. public void OnEnable()
  29. {
  30. FlushDeviceSelect();
  31. SerialPortHelper.ins.GetPort()?.RequestDeviceIno();//每次打开拉取一次设备信息
  32. }
  33. public void FlushDeviceSelect()
  34. {
  35. // 获取用户设置中保存的设备名称
  36. string selectDeviceName = UserSettings.ins.selectDevicesName;
  37. // 如果名称存在于数组中,则获取其索引,否则默认选第一个
  38. selectDevicesIndex = System.Array.IndexOf(SelectDevicesStrs, selectDeviceName);
  39. // 渲染选择的设备
  40. RenderSelectDevicesIndex(selectDevicesIndex);
  41. }
  42. public void RenderSelectDevicesIndex(int value)
  43. {
  44. for (int i = 0; i < SelectDevicesStrs.Length; i++)
  45. {
  46. var item = boxSelectDevices.GetChild(i);
  47. var text = item.GetComponentInChildren<Text>();
  48. // 根据是否为选中设备设置颜色
  49. if (i == value)
  50. {
  51. item.GetComponent<Image>().color = new Color(16 / 255f, 194 / 255f, 198 / 255f, 1f);
  52. }
  53. else
  54. {
  55. item.GetComponent<Image>().color = new Color(96 / 255f, 96 / 255f, 96 / 255f, 1f);
  56. }
  57. // 设置设备名称
  58. text.text = SelectDevicesStrs[i];
  59. }
  60. }
  61. // 当用户点击选择设备时调用
  62. void OnClick_SelectDevice(string name)
  63. {
  64. // 获取选中设备的索引
  65. int newValue = System.Array.IndexOf(SelectDevicesStrs, name);
  66. // 如果选择不同设备,更新选择
  67. if (selectDevicesIndex != newValue)
  68. {
  69. // 播放按钮音效
  70. AudioMgr.ins.PlayBtn();
  71. // 更新选择的设备索引和名称
  72. selectDevicesIndex = newValue;
  73. UserSettings.ins.selectDevicesName = SelectDevicesStrs[selectDevicesIndex];
  74. UserSettings.ins.Save();
  75. // 渲染更新后的选择
  76. RenderSelectDevicesIndex(selectDevicesIndex);
  77. //todo 调用串口后续流程?
  78. switch (UserSettings.ins.selectDevicesName)
  79. {
  80. case "ARTEMIS Pro":
  81. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.ARTEMISPRO);
  82. break;
  83. case "Pistol M9":
  84. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.Gun);
  85. break;
  86. case "HOUYI Pro":
  87. DevicesHolder.ins.SwitchDeviceByType(AimDeviceType.HOUYIPRO);
  88. break;
  89. }
  90. }
  91. }
  92. /**
  93. * 调试
  94. */
  95. public void OnClick_EnterDebuggingInfrared() {
  96. if (InfraredDemo._ins != null) {
  97. InfraredDemo._ins.OnClick_See();
  98. }
  99. }
  100. public void OnClick_EnterDeviceConnect() {
  101. AudioMgr.ins.PlayBtn();
  102. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  103. }
  104. public void OnClick_EnterInfraredCenterCalibration()
  105. {
  106. //弹出校准提示
  107. AutoResetView.onInfraredGuiderAutoResetViewOrBg();
  108. //红外设备校准偏离点
  109. //InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  110. }
  111. public void OnClick_ScreenCalibration()
  112. {
  113. //进入屏幕定位
  114. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  115. }
  116. /// <summary>
  117. /// 根据名字刷新渲染
  118. /// </summary>
  119. public void OnUpdateClickInfoByName() {
  120. // 获取用户设置中保存的设备名称
  121. string selectDeviceName = UserSettings.ins.selectDevicesName;
  122. // 如果名称存在于数组中,则获取其索引,否则默认选第一个
  123. selectDevicesIndex = System.Array.IndexOf(SelectDevicesStrs, selectDeviceName);
  124. if (selectDevicesIndex < 0) selectDevicesIndex = 0;
  125. // 渲染选择的设备
  126. RenderSelectDevicesIndex(selectDevicesIndex);
  127. }
  128. // Update is called once per frame
  129. //void Update()
  130. //{
  131. //}
  132. }
  133. }