using UnityEngine; using System.Collections.Generic; public static class ActionHistory { public enum ActionType { Point2, Shose, ChanraoTuibu, PingFirst, PingSecond, Woman } public class HistoryItem { public ActionType Type; public MonoBehaviour Handler; public int ExtraInt; } public static Stack History = new Stack(); public static void Clear() { History.Clear(); Debug.Log("[ActionHistory] 已清空历史记录"); } public static void Push(ActionType type, MonoBehaviour handler, int extra = 0) { History.Push(new HistoryItem { Type = type, Handler = handler, ExtraInt = extra }); } public static bool TryUndoLast() { if (History.Count == 0) return false; var item = History.Pop(); Debug.Log($"[ActionHistory] Undo {item.Type} handler={(item.Handler != null ? item.Handler.name : "null")}, remaining={History.Count}"); if (item.Type == ActionType.PingFirst || item.Type == ActionType.PingSecond) { // 如果遇到任何 Ping 的动作,就代表用户想重置整个包扎步骤 while (History.Count > 0 && (History.Peek().Type == ActionType.PingFirst || History.Peek().Type == ActionType.PingSecond)) { History.Pop(); } var allPings = Object.FindObjectsOfType(true); foreach (var p in allPings) { if (p.chanraoObject != null) p.chanraoObject.SetActive(false); if (p.pingIndex == 0) { p.gameObject.SetActive(true); p.ResetState(); } else { p.UndoShow(); } } var sm = Object.FindObjectOfType(); if (sm != null) sm.SetPingStepIndex(0); var chanraotuibu = GameObject.Find("chanraotuibu"); if (chanraotuibu == null) { var mi = GameObject.Find("manInjury"); if (mi != null) { var t = mi.transform.Find("chanraotuibu"); if (t != null) chanraotuibu = t.gameObject; } } if (chanraotuibu != null) chanraotuibu.SetActive(false); return true; } if (item.Type == ActionType.Point2) { var h = item.Handler as Point2ClickHandler; if (h != null) h.ResetState(); } else if (item.Type == ActionType.Shose) { var h = item.Handler as ShoseClickHandler; if (h != null) h.ResetState(); } else if (item.Type == ActionType.Woman) { var sm = Object.FindObjectOfType(); if (sm != null) sm.DecrementWomanStep(); var h = item.Handler as TouchPositionHandler; if (h != null) { if (h.nextTouchObject != null) h.nextTouchObject.SetActive(false); h.gameObject.SetActive(true); var mr = h.GetComponent(); if (mr != null) mr.enabled = true; } } return true; } }