DeviceStateManager.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. 项目名称:
  3. 功能:1.实现隐藏功能
  4. 2.撤销隐藏功能
  5. 3.一键还原功能
  6. */
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. public class DeviceStateManager : MonoBehaviour
  11. {
  12. #region 单例
  13. private static DeviceStateManager _instance;
  14. public DeviceStateManager() { }
  15. public static DeviceStateManager Instance
  16. {
  17. get
  18. {
  19. if (_instance != null)
  20. {
  21. return _instance;
  22. }
  23. else
  24. {
  25. _instance = new GameObject().AddComponent<DeviceStateManager>();
  26. return _instance;
  27. }
  28. }
  29. }
  30. #endregion
  31. #region 变量声明
  32. //全部隐藏管理栈
  33. private Stack<GameObject> _Pool;
  34. //模式栈
  35. private Stack<Mode> _ModePool;
  36. //用于存放所有Material
  37. private Dictionary<string, Material> Material_dic;
  38. #endregion
  39. #region 公开方法
  40. /// <summary>
  41. /// 隐藏Obj并添加到栈中
  42. /// </summary>
  43. /// <param name="obj"></param>
  44. public void AddObjToPool(GameObject obj)
  45. {
  46. obj.SetActive(false);
  47. _Pool.Push(obj);
  48. }
  49. /// <summary>
  50. /// 撤销上一步隐藏的obj
  51. /// </summary>
  52. public void RemoveObjToPool()
  53. {
  54. if (_Pool.Count != 0)
  55. {
  56. _Pool.Pop().SetActive(true);
  57. }
  58. }
  59. /// <summary>
  60. ///撤销所有步骤
  61. /// </summary>
  62. public void RemoveAllFromPool()
  63. {
  64. Debug.Log("撤销所有步骤");
  65. while (_Pool.Count != 0)
  66. {
  67. Debug.Log(_Pool.Count);
  68. _Pool.Pop().SetActive(true);
  69. }
  70. }
  71. #endregion
  72. #region 私有
  73. void Awake()
  74. {
  75. _instance = this;
  76. _Pool = new Stack<GameObject>();
  77. }
  78. #endregion
  79. }