BoxUserSettings.cs 3.7 KB

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