EditWindows.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. public class EditWindows : MonoBehaviour
  5. {
  6. public RectTransform scrollRect;
  7. public GameObject labelItem;
  8. public InputField ioText; // 输入:第 X 步
  9. public InputField qiehuanText; // 输入:切换至第 Y 步
  10. public Button qiehuanBut;
  11. public Button fanhuiBut;
  12. public Text statusText;
  13. private List<string> excelData = new List<string>();
  14. void Start()
  15. {
  16. qiehuanBut.onClick.AddListener(OnConfirm);
  17. fanhuiBut.onClick.AddListener(OnBackClick);
  18. //LoadExcelToMemory();
  19. RefreshScrollList();
  20. }
  21. /*/// <summary>
  22. /// 读取Excel
  23. /// </summary>
  24. void LoadExcelToMemory()
  25. {
  26. excelData = ExcelHandler.ReadFirstColumn();
  27. if (excelData == null || excelData.Count == 0)
  28. {
  29. UpdateStatus("Excel 数据为空或读取失败!");
  30. return;
  31. }
  32. UpdateStatus($"读取Excel成功,共加载 {excelData.Count} 条步骤");
  33. }*/
  34. /// <summary>
  35. /// 刷新UI列表
  36. /// </summary>
  37. void RefreshScrollList()
  38. {
  39. foreach (Transform child in scrollRect)
  40. Destroy(child.gameObject);
  41. for (int i = 0; i < excelData.Count; i++)
  42. {
  43. GameObject obj = Instantiate(labelItem, scrollRect);
  44. LabelItem item = obj.GetComponent<LabelItem>();
  45. item.SetNameOrNumber(excelData[i], (i + 1).ToString());
  46. }
  47. }
  48. /// <summary>
  49. /// 按钮点击时处理交换
  50. /// </summary>
  51. private void OnConfirm()
  52. {
  53. if (!ValidateText(ioText.text, out int fromStep))
  54. {
  55. UpdateStatus("请输入正确的源步骤编号!");
  56. ClearInputs();
  57. return;
  58. }
  59. if (!ValidateText(qiehuanText.text, out int toStep))
  60. {
  61. UpdateStatus("请输入正确的目标步骤编号!");
  62. ClearInputs();
  63. return;
  64. }
  65. //防止交换同一个数字
  66. if (fromStep == toStep)
  67. {
  68. UpdateStatus("两个步骤一致,无需交换");
  69. ClearInputs();
  70. return;
  71. }
  72. OnSwapClick(fromStep, toStep);
  73. }
  74. /// <summary>
  75. /// 数字验证
  76. /// </summary>
  77. /// <param name="input"></param>
  78. /// <param name="result"></param>
  79. /// <returns></returns>
  80. private bool ValidateText(string input, out int result)
  81. {
  82. result = 0;
  83. if (string.IsNullOrWhiteSpace(input))
  84. return false;
  85. //允许负号,所以要严格 TryParse
  86. if (!int.TryParse(input, out result))
  87. return false;
  88. if (result <= 0)
  89. {
  90. result = 0;
  91. return false;
  92. }
  93. if (result > excelData.Count)
  94. {
  95. result = 0;
  96. return false;
  97. }
  98. return true;
  99. }
  100. /// <summary>
  101. /// 执行交换逻辑
  102. /// </summary>
  103. /// <param name="x"></param>
  104. /// <param name="y"></param>
  105. private void OnSwapClick(int x, int y)
  106. {
  107. int fromIndex = x - 1;
  108. int toIndex = y - 1;
  109. //保险检查
  110. if (fromIndex < 0 || toIndex < 0 ||
  111. fromIndex >= excelData.Count || toIndex >= excelData.Count)
  112. {
  113. UpdateStatus("输入范围错误!");
  114. ClearInputs();
  115. return;
  116. }
  117. //交换顺序
  118. string tmp = excelData[fromIndex];
  119. excelData[fromIndex] = excelData[toIndex];
  120. excelData[toIndex] = tmp;
  121. UpdateStatus($"已交换步骤:{x} ↔ {y}");
  122. //写回 Excel(即时保存)
  123. //ExcelHandler.WriteFirstColumn(excelData);
  124. //L对 moveItemInfos 重排(即时更新模型播放顺序)
  125. MoveItemManager.instance.ApplyOrderFromExcel();
  126. //UI 更新
  127. RefreshScrollList();
  128. ClearInputs();
  129. }
  130. private void ClearInputs()
  131. {
  132. ioText.text = "";
  133. qiehuanText.text = "";
  134. }
  135. void OnBackClick()
  136. {
  137. UpdateStatus("返回上级页面");
  138. //你自己的跳转逻辑
  139. }
  140. public void UpdateStatus(string msg)
  141. {
  142. if (statusText != null)
  143. statusText.text = msg;
  144. }
  145. }