PopUpManager.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using UnityEngine;
  2. using System.Collections;
  3. public class PopUpManager : MonoBehaviour {
  4. public static bool modal = false;
  5. public static void UpdateModal()
  6. {
  7. PopUpPanel[] popUpPanels = GameObject.FindObjectsOfType<PopUpPanel>();
  8. for(int i=0; i<popUpPanels.Length; i++)
  9. {
  10. PopUpPanel panel = popUpPanels[i];
  11. if(panel.modal == true && panel.gameObject.activeSelf == true)
  12. {
  13. modal = true;
  14. return;
  15. }
  16. }
  17. modal = false;
  18. }
  19. public static PopUpPanel AddPopUp(GameObject panelPrefab, Transform parent=null, bool modal=false, System.Type panelType=null)
  20. {
  21. if(panelPrefab == null)
  22. return null;
  23. GameObject gameObj = Instantiate(panelPrefab) as GameObject;
  24. PopUpPanel panel = null;
  25. if (panelType != null)
  26. {
  27. panel = (PopUpPanel)gameObj.AddComponent(panelType);
  28. }
  29. else
  30. {
  31. panel = gameObj.GetComponent<PopUpPanel>();
  32. }
  33. panel.gameObject.AddComponent<PopUpUtil>();
  34. PopUpManager.AddToMainCanvas(gameObj, parent);
  35. panel.modal = modal;
  36. UpdateModal();
  37. return panel;
  38. }
  39. public static T AddPopUp<T>(GameObject panelPrefab, Transform parent=null, bool modal=false) where T : PopUpPanel
  40. {
  41. if(panelPrefab == null)
  42. return null;
  43. GameObject gameObj = Instantiate(panelPrefab) as GameObject;
  44. T panel = gameObj.GetComponent<T>();
  45. if (panel == null)
  46. {
  47. panel = gameObj.AddComponent<T>();
  48. }
  49. if(panel.GetComponent<PopUpUtil>() == null)
  50. panel.gameObject.AddComponent<PopUpUtil>();
  51. PopUpManager.AddToMainCanvas(gameObj, parent);
  52. panel.modal = modal;
  53. UpdateModal();
  54. return panel;
  55. }
  56. public static void AddToMainCanvas(GameObject gameObj, Transform parent = null)
  57. {
  58. RectTransform rectTrans = gameObj.GetComponent<RectTransform>();
  59. if(rectTrans != null)
  60. {
  61. Vector3 originPos = rectTrans.localPosition;
  62. Vector3 originSizeDelta = rectTrans.sizeDelta;
  63. if(parent == null)
  64. {
  65. Canvas canvas = GameObject.FindObjectOfType<Canvas>();
  66. parent = canvas.transform;
  67. }
  68. gameObj.transform.SetParent(parent);
  69. gameObj.transform.localScale = new Vector3(1f, 1f, 1f);
  70. rectTrans.localPosition = originPos;
  71. rectTrans.sizeDelta = originSizeDelta;
  72. }
  73. else
  74. {
  75. Vector3 pos =gameObj.transform.localPosition;
  76. Vector3 scale = gameObj.transform.localScale;
  77. if(parent == null)
  78. {
  79. MainCanvas canvas = GameObject.FindObjectOfType<MainCanvas>();
  80. parent = canvas.transform;
  81. }
  82. gameObj.transform.SetParent(parent);
  83. gameObj.transform.localScale = scale;
  84. gameObj.transform.localPosition = pos;
  85. }
  86. }
  87. public static void RemovePopUp(PopUpPanel panel)
  88. {
  89. Destroy(panel.gameObject);
  90. UpdateModal();
  91. }
  92. }