| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class EditWindows : MonoBehaviour
- {
- public RectTransform scrollRect;
- public GameObject labelItem;
- public InputField ioText; // 输入:第 X 步
- public InputField qiehuanText; // 输入:切换至第 Y 步
- public Button qiehuanBut;
- public Button fanhuiBut;
- public Text statusText;
- private List<string> excelData = new List<string>();
- void Start()
- {
- qiehuanBut.onClick.AddListener(OnConfirm);
- fanhuiBut.onClick.AddListener(OnBackClick);
- //LoadExcelToMemory();
- RefreshScrollList();
- }
- /*/// <summary>
- /// 读取Excel
- /// </summary>
- void LoadExcelToMemory()
- {
- excelData = ExcelHandler.ReadFirstColumn();
- if (excelData == null || excelData.Count == 0)
- {
- UpdateStatus("Excel 数据为空或读取失败!");
- return;
- }
- UpdateStatus($"读取Excel成功,共加载 {excelData.Count} 条步骤");
- }*/
- /// <summary>
- /// 刷新UI列表
- /// </summary>
- void RefreshScrollList()
- {
- foreach (Transform child in scrollRect)
- Destroy(child.gameObject);
- for (int i = 0; i < excelData.Count; i++)
- {
- GameObject obj = Instantiate(labelItem, scrollRect);
- LabelItem item = obj.GetComponent<LabelItem>();
- item.SetNameOrNumber(excelData[i], (i + 1).ToString());
- }
- }
- /// <summary>
- /// 按钮点击时处理交换
- /// </summary>
- private void OnConfirm()
- {
- if (!ValidateText(ioText.text, out int fromStep))
- {
- UpdateStatus("请输入正确的源步骤编号!");
- ClearInputs();
- return;
- }
- if (!ValidateText(qiehuanText.text, out int toStep))
- {
- UpdateStatus("请输入正确的目标步骤编号!");
- ClearInputs();
- return;
- }
- //防止交换同一个数字
- if (fromStep == toStep)
- {
- UpdateStatus("两个步骤一致,无需交换");
- ClearInputs();
- return;
- }
- OnSwapClick(fromStep, toStep);
- }
- /// <summary>
- /// 数字验证
- /// </summary>
- /// <param name="input"></param>
- /// <param name="result"></param>
- /// <returns></returns>
- private bool ValidateText(string input, out int result)
- {
- result = 0;
- if (string.IsNullOrWhiteSpace(input))
- return false;
- //允许负号,所以要严格 TryParse
- if (!int.TryParse(input, out result))
- return false;
- if (result <= 0)
- {
- result = 0;
- return false;
- }
- if (result > excelData.Count)
- {
- result = 0;
- return false;
- }
- return true;
- }
- /// <summary>
- /// 执行交换逻辑
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- private void OnSwapClick(int x, int y)
- {
- int fromIndex = x - 1;
- int toIndex = y - 1;
- //保险检查
- if (fromIndex < 0 || toIndex < 0 ||
- fromIndex >= excelData.Count || toIndex >= excelData.Count)
- {
- UpdateStatus("输入范围错误!");
- ClearInputs();
- return;
- }
- //交换顺序
- string tmp = excelData[fromIndex];
- excelData[fromIndex] = excelData[toIndex];
- excelData[toIndex] = tmp;
- UpdateStatus($"已交换步骤:{x} ↔ {y}");
- //写回 Excel(即时保存)
- //ExcelHandler.WriteFirstColumn(excelData);
- //L对 moveItemInfos 重排(即时更新模型播放顺序)
- MoveItemManager.instance.ApplyOrderFromExcel();
- //UI 更新
- RefreshScrollList();
- ClearInputs();
- }
- private void ClearInputs()
- {
- ioText.text = "";
- qiehuanText.text = "";
- }
- void OnBackClick()
- {
- UpdateStatus("返回上级页面");
- //你自己的跳转逻辑
- }
- public void UpdateStatus(string msg)
- {
- if (statusText != null)
- statusText.text = msg;
- }
- }
|