UIScrollPageMark.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /****************************************************************************
  2. * Copyright (c) 2018.8 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. namespace QFramework
  26. {
  27. using UnityEngine;
  28. using System.Collections.Generic;
  29. using UnityEngine.UI;
  30. public class UIScrollPageMark : MonoBehaviour
  31. {
  32. public UIScrollPage scrollPage;
  33. public ToggleGroup toggleGroup;
  34. public Toggle togglePrefab;
  35. [Tooltip("页签中心位置")]
  36. public UnityEngine.Vector2 centerPos;
  37. [Tooltip("每个页签之间的间距")]
  38. public UnityEngine.Vector2 interval;
  39. public List<Toggle> toggleList = new List<Toggle>();
  40. void Awake()
  41. {
  42. AdjustTogglePos();
  43. scrollPage.AddPageChangeListener(OnScrollPageChanged);
  44. }
  45. public void OnScrollPageChanged(int pageCount, int currentPageIndex)
  46. {
  47. if (pageCount != toggleList.Count)
  48. {
  49. if (pageCount > toggleList.Count)
  50. {
  51. int cc = pageCount - toggleList.Count;
  52. for (int i = 0; i < cc; i++)
  53. {
  54. toggleList.Add(CreateToggle(i));
  55. }
  56. }
  57. else if (pageCount < toggleList.Count)
  58. {
  59. while (toggleList.Count > pageCount)
  60. {
  61. Toggle t = toggleList[toggleList.Count - 1];
  62. toggleList.Remove(t);
  63. DestroyImmediate(t.gameObject);
  64. }
  65. }
  66. AdjustTogglePos();
  67. }
  68. toggleGroup.gameObject.SetActive(pageCount > 1);
  69. if (currentPageIndex >= 0)
  70. {
  71. for (int i = 0; i < toggleList.Count; i++)
  72. {
  73. if (i == currentPageIndex) toggleList[i].isOn = true;
  74. else toggleList[i].isOn = false;
  75. }
  76. }
  77. }
  78. Toggle CreateToggle(int i)
  79. {
  80. Toggle t = GameObject.Instantiate<Toggle>(togglePrefab);
  81. t.gameObject.SetActive(true);
  82. t.transform.SetParent(toggleGroup.transform);
  83. t.transform.localScale = Vector3.one;
  84. t.transform.localPosition = Vector3.zero;
  85. return t;
  86. }
  87. void AdjustTogglePos()
  88. {
  89. UnityEngine.Vector2 startPos = centerPos - 0.5f * (toggleList.Count - 1) * interval;
  90. for (int i = 0; i < toggleList.Count; i++)
  91. {
  92. toggleList[i].GetComponent<RectTransform>().anchoredPosition = startPos + i * interval;
  93. }
  94. }
  95. }
  96. }