Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LayoutGroupArrangement.cs 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using UnityEngine.TestTools;
  6. using UnityEngine.UI;
  7. [TestFixture]
  8. public class LayoutGroupArrangement
  9. {
  10. const float k_LayoutDefaultSize = 100f;
  11. GameObject m_Canvas;
  12. GameObject m_LayoutGameObject;
  13. RectTransform m_Child1;
  14. RectTransform m_Child2;
  15. [SetUp]
  16. public void TestSetup()
  17. {
  18. m_Canvas = new GameObject("Canvas", typeof(Canvas));
  19. m_LayoutGameObject = new GameObject("LayoutGroup", typeof(RectTransform));
  20. m_LayoutGameObject.transform.SetParent(m_Canvas.transform, false);
  21. m_LayoutGameObject.GetComponent<RectTransform>().sizeDelta =
  22. new Vector2(k_LayoutDefaultSize, k_LayoutDefaultSize);
  23. m_Child1 = new GameObject("Child1").AddComponent<RectTransform>();
  24. m_Child1.SetParent(m_LayoutGameObject.transform, false);
  25. m_Child2 = new GameObject("Child2").AddComponent<RectTransform>();
  26. m_Child2.SetParent(m_LayoutGameObject.transform, false);
  27. }
  28. [UnityTest]
  29. [Category("RegressionTest")]
  30. [Description("Nested GameObjects without a Layout Element will effect a Layout Group's arrangement (case 1026779)")]
  31. public IEnumerator LayoutGroup_ShouldResizeChildren_AfterDisablingAndEnablingAnyChild(
  32. [Values(typeof(HorizontalLayoutGroup), typeof(VerticalLayoutGroup))]
  33. Type layoutGroup)
  34. {
  35. var layoutComponent = (HorizontalOrVerticalLayoutGroup)m_LayoutGameObject.AddComponent(layoutGroup);
  36. layoutComponent.childForceExpandHeight = true;
  37. layoutComponent.childForceExpandWidth = true;
  38. layoutComponent.childControlHeight = true;
  39. layoutComponent.childControlWidth = true;
  40. Assert.That(m_LayoutGameObject.GetComponent<RectTransform>().sizeDelta,
  41. Is.EqualTo(Vector2.one * k_LayoutDefaultSize),
  42. "Layout's size should not change after adding the " + layoutGroup.Name);
  43. yield return null;
  44. // HorizontalLayoutGroup will rearrange the children to have width equal to half the layout's width.
  45. var expectedWidth = layoutGroup == typeof(HorizontalLayoutGroup)
  46. ? k_LayoutDefaultSize / 2
  47. : k_LayoutDefaultSize;
  48. // VerticalLayoutGroup will rearrange the children to have height equal to half the layout's height.
  49. var expectedHeight = layoutGroup == typeof(VerticalLayoutGroup) ? k_LayoutDefaultSize / 2 : k_LayoutDefaultSize;
  50. Assert.That(m_Child1.sizeDelta.x, Is.EqualTo(expectedWidth),
  51. "Adding " + layoutGroup.Name + " did not resize the first child.");
  52. Assert.That(m_Child1.sizeDelta.y, Is.EqualTo(expectedHeight),
  53. "Adding " + layoutGroup.Name + " did not resize the first child.");
  54. Assert.That(m_Child2.sizeDelta, Is.EqualTo(m_Child1.sizeDelta), "Both children should be of the same size.");
  55. // Disable the second child and verify that the first child is resized to have size equal to the layout's size.
  56. m_Child2.gameObject.SetActive(false);
  57. yield return null;
  58. Assert.That(m_Child1.sizeDelta.x, Is.EqualTo(k_LayoutDefaultSize),
  59. layoutGroup.Name + " did not resize the first child after disabling the second child.");
  60. Assert.That(m_Child1.sizeDelta.y, Is.EqualTo(k_LayoutDefaultSize),
  61. layoutGroup.Name + " did not resize the first child after disabling the second child.");
  62. // Enable the second child and verify that the first child is resized to the expected width/height.
  63. m_Child2.gameObject.SetActive(true);
  64. yield return null;
  65. Assert.That(m_Child1.sizeDelta.x, Is.EqualTo(expectedWidth),
  66. layoutGroup.Name + " did not resize the first child after enabling the second child.");
  67. Assert.That(m_Child1.sizeDelta.y, Is.EqualTo(expectedHeight),
  68. layoutGroup.Name + " did not resize the first child after enabling the second child.");
  69. Assert.That(m_Child2.sizeDelta, Is.EqualTo(m_Child1.sizeDelta), "Both children should be of the same size.");
  70. }
  71. [TearDown]
  72. public void TearDown()
  73. {
  74. GameObject.DestroyImmediate(m_Canvas);
  75. }
  76. }