HistoryController.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// отмена создания стены, маяка, зоны
  8. /// </summary>
  9. public class HistoryController : MonoBehaviour
  10. {
  11. public static HistoryController instance;
  12. public Button redo;
  13. public Button undo; // отмена
  14. public List<History> histories = new List<History>();
  15. private void Awake()
  16. {
  17. instance = this;
  18. }
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. undo.onClick.AddListener(Undo);
  23. redo.onClick.AddListener(Redo);
  24. }
  25. // Update is called once per frame
  26. void Update()
  27. {
  28. }
  29. public void Undo() {
  30. var last = histories.Where(l => l.status).LastOrDefault();
  31. if (last != null)
  32. {
  33. last.obj.SetActive(false);
  34. if (last.ButtonPanel != null) last.ButtonPanel.SetActive(false);
  35. last.status = false;
  36. }
  37. }
  38. public void Redo()
  39. {
  40. var last = histories.Where(l => !l.status).FirstOrDefault();
  41. if (last != null)
  42. {
  43. last.obj.SetActive(true);
  44. if(last.ButtonPanel != null) last.ButtonPanel.SetActive(true);
  45. last.status = true;
  46. }
  47. histories.Remove(last);
  48. }
  49. public void EventHistory(uint id, GameObject gameObject, GameObject panel = null)
  50. {
  51. histories.Add(new History { id = id, obj = gameObject, ButtonPanel = panel, status = true });
  52. }
  53. }