DrawListManager.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class DrawListManager : MonoBehaviour
  5. {
  6. public static DrawListManager _instance;
  7. private Stack<GameObject> Dlist = new Stack<GameObject>();
  8. private void Awake()
  9. {
  10. _instance = this;
  11. MessageCenterController.Instance.Register(GameEnum.MESSAGE_DRAW_UNDO_PREVIOUS, RemoveLast);
  12. MessageCenterController.Instance.Register(GameEnum.MESSAGE_DRAW_UNDO_ALL, RemoveAll);
  13. }
  14. private void OnDestroy()
  15. {
  16. RemoveAll();
  17. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_DRAW_UNDO_PREVIOUS, RemoveLast);
  18. MessageCenterController.Instance.UnRegister(GameEnum.MESSAGE_DRAW_UNDO_ALL, RemoveAll);
  19. }
  20. public void AddBrush(GameObject g)
  21. {
  22. g.transform.parent = transform;
  23. Dlist.Push(g);
  24. }
  25. public void RemoveLast(object o = null)
  26. {
  27. if (Dlist.Count > 0)
  28. {
  29. GameObject g = Dlist.Pop();
  30. Destroy(g);
  31. }
  32. }
  33. public void RemoveAll(object o = null)
  34. {
  35. while (Dlist.Count > 0)
  36. {
  37. Destroy(Dlist.Pop());
  38. }
  39. Dlist.Clear();
  40. }
  41. private void Update()
  42. {
  43. if (Input.GetKeyUp(KeyCode.H))
  44. {
  45. RemoveLast();
  46. }
  47. else if (Input.GetKeyUp(KeyCode.J))
  48. {
  49. RemoveAll();
  50. }
  51. }
  52. }