UIRoot.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. * Copyright (c) 2017 ~ 2020.1 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. using UnityEngine;
  26. using UnityEngine.UI;
  27. namespace QFramework
  28. {
  29. [MonoSingletonPath("UIRoot")]
  30. public class UIRoot : MonoBehaviour,ISingleton
  31. {
  32. public Camera UICamera;
  33. public Canvas Canvas;
  34. public CanvasScaler CanvasScaler;
  35. public GraphicRaycaster GraphicRaycaster;
  36. public RectTransform Bg;
  37. public RectTransform Common;
  38. public RectTransform PopUI;
  39. public RectTransform CanvasPanel;
  40. private new static UIRoot mInstance;
  41. public new static UIRoot Instance
  42. {
  43. get
  44. {
  45. if (!mInstance)
  46. {
  47. mInstance = FindObjectOfType<UIRoot>();
  48. }
  49. if (!mInstance)
  50. {
  51. Instantiate(Resources.Load<GameObject>("UIRoot"));
  52. mInstance = MonoSingletonProperty<UIRoot>.Instance;
  53. mInstance.name = "UIRoot";
  54. DontDestroyOnLoad(mInstance);
  55. }
  56. return mInstance;
  57. }
  58. }
  59. public Camera Camera
  60. {
  61. get { return UICamera; }
  62. }
  63. public void SetResolution(int width, int height, float matchOnWidthOrHeight)
  64. {
  65. CanvasScaler.referenceResolution = new Vector2(width, height);
  66. CanvasScaler.matchWidthOrHeight = matchOnWidthOrHeight;
  67. }
  68. public Vector2 GetResolution()
  69. {
  70. return CanvasScaler.referenceResolution;
  71. }
  72. public float GetMatchOrWidthOrHeight()
  73. {
  74. return CanvasScaler.matchWidthOrHeight;
  75. }
  76. public void ScreenSpaceOverlayRenderMode()
  77. {
  78. Canvas.renderMode = UnityEngine.RenderMode.ScreenSpaceOverlay;
  79. UICamera.gameObject.SetActive(false);
  80. }
  81. public void ScreenSpaceCameraRenderMode()
  82. {
  83. Canvas.renderMode = RenderMode.ScreenSpaceCamera;
  84. UICamera.gameObject.SetActive(true);
  85. Canvas.worldCamera = UICamera;
  86. }
  87. public void SetLevelOfPanel(UILevel level, IPanel panel)
  88. {
  89. var canvas = panel.Transform.GetComponent<Canvas>();
  90. canvas.transform.parent = null;
  91. canvas.transform.position = Vector3.zero;
  92. if (canvas)
  93. {
  94. panel.Transform.SetParent(CanvasPanel);
  95. }
  96. else
  97. {
  98. switch (level)
  99. {
  100. case UILevel.Bg:
  101. panel.Transform.SetParent(Bg);
  102. break;
  103. case UILevel.Common:
  104. panel.Transform.SetParent(Common);
  105. break;
  106. case UILevel.PopUI:
  107. panel.Transform.SetParent(PopUI);
  108. break;
  109. }
  110. }
  111. }
  112. public void OnSingletonInit()
  113. {
  114. }
  115. }
  116. }