UIKitConfig.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 System.Collections.Generic;
  26. using UnityEngine;
  27. namespace QFramework
  28. {
  29. public class UIKitConfig
  30. {
  31. public virtual UIRoot Root
  32. {
  33. get { return UIRoot.Instance; }
  34. }
  35. public virtual IPanel LoadPanel(PanelSearchKeys panelSearchKeys)
  36. {
  37. var panelLoader = PanelLoaderPool.AllocateLoader();
  38. panelLoader.LoadPanelPrefab(panelSearchKeys);
  39. var panelPrefab = panelLoader.LoadPanelPrefab(panelSearchKeys);
  40. var obj = Object.Instantiate(panelPrefab);
  41. var retScript = obj.GetComponent<UIPanel>();
  42. var panelInterface = retScript as IPanel;
  43. panelInterface.Loader = panelLoader;
  44. Debug.Log(panelInterface);
  45. return retScript;
  46. }
  47. public IPanelLoaderPool PanelLoaderPool = new DefaultPanelLoaderPool();
  48. public virtual void SetDefaultSizeOfPanel(IPanel panel)
  49. {
  50. var panelRectTrans = panel.Transform as RectTransform;
  51. panelRectTrans.offsetMin = Vector2.zero;
  52. panelRectTrans.offsetMax = Vector2.zero;
  53. panelRectTrans.anchoredPosition3D = Vector3.zero;
  54. panelRectTrans.anchorMin = Vector2.zero;
  55. panelRectTrans.anchorMax = Vector2.one;
  56. panelRectTrans.localScale = Vector3.one;
  57. }
  58. }
  59. /// <summary>
  60. /// 如果想要定制自己的加载器,自定义 IPanelLoader 以及
  61. /// </summary>
  62. public interface IPanelLoader
  63. {
  64. GameObject LoadPanelPrefab(PanelSearchKeys panelSearchKeys);
  65. void Unload();
  66. }
  67. public interface IPanelLoaderPool
  68. {
  69. IPanelLoader AllocateLoader();
  70. void RecycleLoader(IPanelLoader panelLoader);
  71. }
  72. public abstract class AbstractPanelLoaderPool : IPanelLoaderPool
  73. {
  74. private Stack<IPanelLoader> mPool = new Stack<IPanelLoader>(16);
  75. public IPanelLoader AllocateLoader()
  76. {
  77. return mPool.Count > 0 ? mPool.Pop() :CreatePanelLoader();
  78. }
  79. protected abstract IPanelLoader CreatePanelLoader();
  80. public void RecycleLoader(IPanelLoader panelLoader)
  81. {
  82. mPool.Push(panelLoader);
  83. }
  84. }
  85. public class DefaultPanelLoaderPool : AbstractPanelLoaderPool
  86. {
  87. /// <summary>
  88. /// Default
  89. /// </summary>
  90. public class DefaultPanelLoader : IPanelLoader
  91. {
  92. private GameObject mPanelPrefab;
  93. public GameObject LoadPanelPrefab(PanelSearchKeys panelSearchKeys)
  94. {
  95. mPanelPrefab = Resources.Load<GameObject>(panelSearchKeys.GameObjName);
  96. return mPanelPrefab;
  97. }
  98. public void Unload()
  99. {
  100. }
  101. }
  102. protected override IPanelLoader CreatePanelLoader()
  103. {
  104. return new DefaultPanelLoader();
  105. }
  106. }
  107. }