UINavigation.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class UINavigation
  6. {
  7. private static int count;
  8. public static List<UiBase> History;
  9. public static List<UiBase> PopHistory;
  10. public static void Init()
  11. {
  12. if(History != null || PopHistory != null)
  13. {
  14. return;
  15. }
  16. History = new List<UiBase>();
  17. PopHistory = new List<UiBase>();
  18. }
  19. public static void Clear()
  20. {
  21. History.Clear();
  22. PopHistory.Clear();
  23. }
  24. public static void AddItem(UiBase view)
  25. {
  26. if (view == null)
  27. return;
  28. if (view.needNavigation)
  29. {
  30. //弹出窗体后面的窗体冻结
  31. UiBase lastView = GetLastItem();
  32. if (lastView != null)
  33. lastView.Freeze();
  34. History.Add(view);
  35. }
  36. if (view.needPopNavigation)
  37. {
  38. UiBase lastView = GetPopLastItem();
  39. if (lastView != null)
  40. lastView.Freeze();
  41. PopHistory.Add(view);
  42. }
  43. }
  44. public static void RemoveLastItem(UiBase view)
  45. {
  46. if (view == null)
  47. return;
  48. if (view.needNavigation)
  49. {
  50. //从栈中移除
  51. UiBase lastView = GetLastItem();
  52. if(lastView == view)
  53. {
  54. count = History.Count;
  55. if (count == 0)
  56. return;
  57. History.RemoveAt(count - 1);
  58. }
  59. }
  60. if (view.needPopNavigation)
  61. {
  62. UiBase lastView = GetPopLastItem();
  63. if (lastView == view)
  64. {
  65. count = PopHistory.Count;
  66. if (count == 0)
  67. return;
  68. PopHistory.RemoveAt(count - 1);
  69. }
  70. }
  71. }
  72. public static UiBase GetLastItem()
  73. {
  74. count = History.Count;
  75. if (count == 0)
  76. return null;
  77. UiBase data = History[count - 1];
  78. return data;
  79. }
  80. /// <summary>
  81. /// 找到上一个打开过后已关闭的界面
  82. /// </summary>
  83. /// <returns></returns>
  84. public static UiBase GetLastOpenView()
  85. {
  86. count = History.Count;
  87. if (count <=1)
  88. return null;
  89. UiBase data = History[count - 2];
  90. return data;
  91. }
  92. public static UiBase GetPopLastItem()
  93. {
  94. count = PopHistory.Count;
  95. if (count == 0)
  96. return null;
  97. UiBase data = PopHistory[count - 1];
  98. return data;
  99. }
  100. }