UIManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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("UI/" + uiName);
  39. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  40. gameObject.SetActive(false);
  41. baseUI = (gameObject.AddComponent(type) as BaseUI);
  42. baseUI.UIName = uiName;
  43. baseUI.UIInit();
  44. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  45. m_DicUI[baseUI.UIName] = baseUI;
  46. }
  47. baseUI.Show(param);
  48. }
  49. public void ShowUI(string uiName, Type type, WarningUI.OnYesClick onYesClick, object param = null)
  50. {
  51. if (null == UIRoot)
  52. {
  53. return;
  54. }
  55. BaseUI baseUI = null;
  56. m_DicUI.TryGetValue(uiName, out baseUI);
  57. if (null == baseUI)
  58. {
  59. UnityEngine.Object @object = ResMgr.Instance.Load("UI/" + uiName);
  60. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  61. gameObject.SetActive(false);
  62. baseUI = (gameObject.AddComponent(type) as BaseUI);
  63. baseUI.UIName = uiName;
  64. baseUI.UIInit();
  65. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  66. m_DicUI[baseUI.UIName] = baseUI;
  67. }
  68. baseUI.Show(onYesClick, param);
  69. }
  70. public void ShowUI(string uiName, Type type, WarningUI.OnYesClick onYesClick, WarningUI.OnNoClick onNoClick, object param = null)
  71. {
  72. if (null == UIRoot)
  73. {
  74. return;
  75. }
  76. BaseUI baseUI = null;
  77. m_DicUI.TryGetValue(uiName, out baseUI);
  78. if (null == baseUI)
  79. {
  80. UnityEngine.Object @object = ResMgr.Instance.Load("UI/" + uiName);
  81. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(@object as GameObject);
  82. gameObject.SetActive(false);
  83. baseUI = (gameObject.AddComponent(type) as BaseUI);
  84. baseUI.UIName = uiName;
  85. baseUI.UIInit();
  86. baseUI.CacheTransform.SetParent(UIRoot.transform, false);
  87. m_DicUI[baseUI.UIName] = baseUI;
  88. }
  89. baseUI.Show(onYesClick, onNoClick, param);
  90. }
  91. public void HideUI(string uiName)
  92. {
  93. BaseUI baseUI = null;
  94. m_DicUI.TryGetValue(uiName, out baseUI);
  95. if (null != baseUI)
  96. {
  97. baseUI.Hide();
  98. }
  99. }
  100. public void DestroyUI(string uiName)
  101. {
  102. BaseUI baseUI = null;
  103. m_DicUI.TryGetValue(uiName, out baseUI);
  104. if (null != baseUI)
  105. {
  106. m_DicUI.Remove(baseUI.UIName);
  107. UnityEngine.Object.Destroy(baseUI.CacheGameObject);
  108. }
  109. }
  110. public bool IsUIOpen(string uiName)
  111. {
  112. BaseUI baseUI = null;
  113. m_DicUI.TryGetValue(uiName, out baseUI);
  114. return null != baseUI && baseUI.IsShow();
  115. }
  116. public BaseUI GetUI(string uiName)
  117. {
  118. BaseUI baseUI = null;
  119. m_DicUI.TryGetValue(uiName, out baseUI);
  120. if (null != baseUI)
  121. {
  122. return baseUI;
  123. }
  124. else
  125. {
  126. return null;
  127. }
  128. }
  129. public void ChangeCanvasPos()
  130. {
  131. if (OpenXRCamera.Instance.head != null)
  132. {
  133. m_UIRoot.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.eulerAngles.y, 0);
  134. m_UIRoot.transform.position = OpenXRCamera.Instance.head.position + GameManager.Instance.Player.transform.forward + new Vector3(0, 0.3f, 0);
  135. }
  136. }
  137. public void ChangePos()
  138. {
  139. if (OpenXRCamera.Instance.head != null&&GameManager.Instance.Player.transform.position == OpenXRCamera.Instance.head.position)
  140. {
  141. m_UIRoot.transform.eulerAngles = new Vector3(0, OpenXRCamera.Instance.head.eulerAngles.y, 0);
  142. // Debug.Log(GameManager.Instance.Player.transform.position+" ########## "+ API_GSXR_Slam.GSXR_Get_Head().transform.forward);
  143. m_UIRoot.transform.position = GameManager.Instance.Player.transform.forward + new Vector3(0, -0.2f, 0.2f);
  144. }
  145. }
  146. }