Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ScrollRectClamp.cs 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.TestTools;
  4. using NUnit.Framework;
  5. using System.Collections;
  6. public class ScrollRectClamp
  7. {
  8. // Prefab has the following hierarchy:
  9. // - PrefabRoot
  10. // - Canvas
  11. // - Root
  12. // - Scroll View
  13. // - Content
  14. // - Scrollbar
  15. GameObject m_PrefabRoot;
  16. RectTransform Root { get; set; }
  17. ScrollRect Scroll { get; set; }
  18. RectTransform ScrollTransform { get; set; }
  19. RectTransform Content { get; set; }
  20. float ScrollSizeY { get { return ScrollTransform.rect.size.y; } }
  21. float ContentSizeY { get { return Content.rect.size.y; } }
  22. [SetUp]
  23. public void Setup()
  24. {
  25. // We setup the ScrollRect so that it will vertically resize with the Root object, to simulate
  26. // a change in screen size
  27. m_PrefabRoot = new GameObject("ScrollRectClamp");
  28. GameObject CanvasGO = new GameObject("Canvas");
  29. CanvasGO.transform.SetParent(m_PrefabRoot.transform);
  30. GameObject RootGO = new GameObject("Root", typeof(RectTransform));
  31. RootGO.transform.SetParent(CanvasGO.transform);
  32. Root = RootGO.GetComponent<RectTransform>();
  33. Root.pivot = Root.anchorMin = Root.anchorMax = new Vector2(0.5f, 0.5f);
  34. Root.sizeDelta = new Vector2(150.0f, 200.0f);
  35. GameObject ScrollViewGO = new GameObject("Scroll View", typeof(RectTransform), typeof(ScrollRect), typeof(Image));
  36. ScrollViewGO.transform.SetParent(Root);
  37. Scroll = ScrollViewGO.GetComponent<ScrollRect>();
  38. ScrollTransform = ScrollViewGO.GetComponent<RectTransform>();
  39. Scroll.viewport = ScrollTransform;
  40. Scroll.movementType = ScrollRect.MovementType.Clamped;
  41. Scroll.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHide;
  42. ScrollTransform.pivot = ScrollTransform.anchorMax = new Vector2(0.0f, 1.0f);
  43. ScrollTransform.anchorMin = Vector2.zero;
  44. ScrollTransform.sizeDelta = new Vector2(150.0f, 0.0f);
  45. ScrollTransform.localPosition = Vector3.zero;
  46. GameObject ContentGO = new GameObject("Content", typeof(RectTransform));
  47. Content = ContentGO.GetComponent<RectTransform>();
  48. Content.SetParent(ScrollTransform);
  49. Scroll.content = Content;
  50. Content.pivot = Content.anchorMin = new Vector2(0.0f, 1.0f);
  51. Content.anchorMax = new Vector2(1.0f, 1.0f);
  52. Content.sizeDelta = new Vector2(0.0f, 300.0f);
  53. Content.anchoredPosition = Vector2.zero;
  54. GameObject ScrollbarGO = new GameObject("Scrollbar", typeof(RectTransform), typeof(Image), typeof(Scrollbar));
  55. ScrollbarGO.transform.SetParent(ScrollTransform);
  56. Scroll.verticalScrollbar = ScrollbarGO.GetComponent<Scrollbar>();
  57. }
  58. [TearDown]
  59. public void TearDown()
  60. {
  61. Object.DestroyImmediate(m_PrefabRoot);
  62. }
  63. [UnityTest]
  64. public IEnumerator ScrollRect_CorrectClampOnResize()
  65. {
  66. Assert.IsNotNull(Scroll.verticalScrollbar);
  67. Scroll.verticalNormalizedPosition = 1.0f;
  68. yield return null;
  69. Assert.IsTrue(Mathf.Approximately(0.0f, Content.anchoredPosition.y));
  70. Scroll.verticalNormalizedPosition = 0.0f;
  71. yield return null;
  72. // The content is vertically bigger than the viewport.
  73. Assert.IsTrue(Mathf.Approximately(Content.anchoredPosition.y, ContentSizeY - ScrollSizeY));
  74. // Resizing the root will resize the viewport accordingly.
  75. Root.sizeDelta = new Vector2(150.0f, 300.0f);
  76. yield return null;
  77. // The content is vertically the same size as the viewport
  78. Assert.IsTrue(Mathf.Approximately(ContentSizeY, ScrollSizeY));
  79. Assert.False(Scroll.verticalScrollbar.gameObject.activeSelf);
  80. Assert.IsTrue(Mathf.Approximately(0.0f, Scroll.verticalNormalizedPosition));
  81. Assert.IsTrue(Mathf.Approximately(0.0f, Content.anchoredPosition.y));
  82. Root.sizeDelta = new Vector2(150.0f, 200.0f);
  83. yield return null;
  84. Assert.IsTrue(Mathf.Approximately(1.0f, Scroll.verticalNormalizedPosition));
  85. }
  86. }