BoxUserSettings.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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", "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. }
  79. }
  80. /**
  81. * 调试
  82. */
  83. public void OnClick_EnterDebuggingInfrared() {
  84. if (InfraredDemo._ins != null) {
  85. InfraredDemo._ins.OnClick_See();
  86. }
  87. }
  88. public void OnClick_EnterDeviceConnect() {
  89. AudioMgr.ins.PlayBtn();
  90. ViewMgr.Instance.ShowView<DeviceViewInfrared>();
  91. }
  92. public void OnClick_EnterInfraredCenterCalibration()
  93. {
  94. //红外设备校准偏离点
  95. InfraredDemo._ins?.OnClick_SetAdjustPointsOffset();
  96. }
  97. public void OnClick_ScreenCalibration()
  98. {
  99. //进入屏幕定位
  100. ViewManager2.ShowView(ViewManager2.Path_InfraredView);
  101. }
  102. // Update is called once per frame
  103. //void Update()
  104. //{
  105. //}
  106. }
  107. }