UIManager.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class UIManager : Singleton<UIManager>
  5. {
  6. private GameObject m_UIRoot;
  7. private Dictionary<string, BaseUI> m_DicUI = new Dictionary<string, BaseUI>();
  8. private bool m_IsInit;
  9. public GameObject UIRoot
  10. {
  11. get
  12. {
  13. if (m_UIRoot == null)
  14. {
  15. m_UIRoot = GameObject.FindGameObjectWithTag("UIRoot_UGUI");
  16. }
  17. return m_UIRoot;
  18. }
  19. }
  20. public void Init()
  21. {
  22. if (m_IsInit)
  23. {
  24. return;
  25. }
  26. m_IsInit = true;
  27. }
  28. public void ShowUI(string uiName, Type type, object param = null)
  29. {
  30. if (null == UIRoot)
  31. {
  32. return;
  33. }
  34. BaseUI baseUI = null;
  35. m_DicUI.TryGetValue(uiName, out baseUI);
  36. if (null == baseUI)
  37. {
  38. UnityEngine.Object @object = ResMgr.Instance.Load(uiName);
  39. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  40. gameObject.SetActive(false);
  41. if((gameObject.GetComponent(type) is BaseUI))
  42. {
  43. baseUI= (gameObject.GetComponent(type) as BaseUI);
  44. }
  45. else
  46. {
  47. baseUI = (gameObject.AddComponent(type) as BaseUI);
  48. }
  49. baseUI.UIName = uiName;
  50. baseUI.UIInit();
  51. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  52. m_DicUI[baseUI.UIName] = baseUI;
  53. }
  54. baseUI.Show(param);
  55. }
  56. public void ShowUI(string uiName, Type type, WarningUI.OnYesClick onYesClick, object param = null)
  57. {
  58. if (null == UIRoot)
  59. {
  60. return;
  61. }
  62. BaseUI baseUI = null;
  63. m_DicUI.TryGetValue(uiName, out baseUI);
  64. if (null == baseUI)
  65. {
  66. UnityEngine.Object @object = ResMgr.Instance.Load(uiName);
  67. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  68. gameObject.SetActive(false);
  69. baseUI = (gameObject.AddComponent(type) as BaseUI);
  70. baseUI.UIName = uiName;
  71. baseUI.UIInit();
  72. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  73. m_DicUI[baseUI.UIName] = baseUI;
  74. }
  75. baseUI.Show(onYesClick, param);
  76. }
  77. public void ShowUI(string uiName, Type type, WarningUI.OnYesClick onYesClick, WarningUI.OnNoClick onNoClick, object param = null)
  78. {
  79. if (null == UIRoot)
  80. {
  81. return;
  82. }
  83. BaseUI baseUI = null;
  84. m_DicUI.TryGetValue(uiName, out baseUI);
  85. if (null == baseUI)
  86. {
  87. UnityEngine.Object @object = ResMgr.Instance.Load(uiName);
  88. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  89. gameObject.SetActive(false);
  90. baseUI = (gameObject.AddComponent(type) as BaseUI);
  91. baseUI.UIName = uiName;
  92. baseUI.UIInit();
  93. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  94. m_DicUI[baseUI.UIName] = baseUI;
  95. }
  96. baseUI.Show(onYesClick, onNoClick, param);
  97. }
  98. public void HideUI(string uiName)
  99. {
  100. BaseUI baseUI = null;
  101. m_DicUI.TryGetValue(uiName, out baseUI);
  102. if (null != baseUI)
  103. {
  104. baseUI.Hide();
  105. }
  106. }
  107. public void DestroyUI(string uiName)
  108. {
  109. BaseUI baseUI = null;
  110. m_DicUI.TryGetValue(uiName, out baseUI);
  111. if (null != baseUI)
  112. {
  113. m_DicUI.Remove(baseUI.UIName);
  114. UnityEngine.Object.Destroy(baseUI.CacheGameObject);
  115. }
  116. }
  117. public bool IsUIOpen(string uiName)
  118. {
  119. BaseUI baseUI = null;
  120. m_DicUI.TryGetValue(uiName, out baseUI);
  121. return null != baseUI && baseUI.IsShow();
  122. }
  123. public BaseUI GetUI(string uiName)
  124. {
  125. BaseUI baseUI = null;
  126. m_DicUI.TryGetValue(uiName, out baseUI);
  127. if (null != baseUI)
  128. {
  129. return baseUI;
  130. }
  131. else
  132. {
  133. return null;
  134. }
  135. }
  136. public void ChangeCanvasPos()
  137. {
  138. if (OpenXRCamera.Instance.head != null)
  139. {
  140. m_UIRoot.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.eulerAngles.y, 0);
  141. m_UIRoot.transform.position = OpenXRCamera.Instance.head.position + GameManager.Instance.Player.transform.forward + new Vector3(0, 0.3f, 0);
  142. }
  143. }
  144. public void ChangePos()
  145. {
  146. if (OpenXRCamera.Instance.head != null&&GameManager.Instance.Player.transform.position == OpenXRCamera.Instance.head.position)
  147. {
  148. m_UIRoot.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.eulerAngles.y, 0);
  149. // Debug.Log(GameManager.Instance.Player.transform.position+" ########## "+ API_GSXR_Slam.GSXR_Get_Head().transform.forward);
  150. m_UIRoot.transform.position = GameManager.Instance.Player.transform.forward + new Vector3(0, -0.2f, 0.2f);
  151. }
  152. }
  153. }