No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SiblingOrderChangesLayout.cs 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using UnityEngine;
  2. using UnityEngine.TestTools;
  3. using NUnit.Framework;
  4. using System.Collections;
  5. using UnityEngine.UI;
  6. [TestFixture]
  7. [Category("RegressionTest")]
  8. [Description("Case 723062")]
  9. public class SiblingOrderChangesLayout
  10. {
  11. GameObject m_CanvasGO;
  12. GameObject m_ParentGO;
  13. GameObject m_Child1GO;
  14. GameObject m_Child2GO;
  15. [SetUp]
  16. public void TestSetup()
  17. {
  18. m_CanvasGO = new GameObject("Canvas", typeof(Canvas));
  19. m_ParentGO = new GameObject("ParentRenderer");
  20. m_Child1GO = CreateTextObject("ChildRenderer1");
  21. m_Child2GO = CreateTextObject("ChildRenderer2");
  22. #if UNITY_EDITOR
  23. UnityEditor.EditorApplication.ExecuteMenuItem("Window/General/Game");
  24. #endif
  25. }
  26. [UnityTest]
  27. public IEnumerator ReorderingSiblingChangesLayout()
  28. {
  29. m_ParentGO.transform.SetParent(m_CanvasGO.transform);
  30. m_Child1GO.transform.SetParent(m_ParentGO.transform);
  31. m_Child2GO.transform.SetParent(m_ParentGO.transform);
  32. m_ParentGO.AddComponent<CanvasRenderer>();
  33. m_ParentGO.AddComponent<RectTransform>();
  34. m_ParentGO.AddComponent<VerticalLayoutGroup>();
  35. m_ParentGO.AddComponent<ContentSizeFitter>();
  36. yield return new WaitForEndOfFrame();
  37. Vector2 child1Pos = m_Child1GO.GetComponent<RectTransform>().anchoredPosition;
  38. Vector2 child2Pos = m_Child2GO.GetComponent<RectTransform>().anchoredPosition;
  39. Assert.That(child1Pos, Is.Not.EqualTo(child2Pos));
  40. Assert.That(m_Child1GO.GetComponent<CanvasRenderer>().hasMoved, Is.False, "CanvasRenderer.hasMoved should be false");
  41. m_Child2GO.transform.SetAsFirstSibling();
  42. Canvas.ForceUpdateCanvases();
  43. Assert.That(m_Child1GO.GetComponent<CanvasRenderer>().hasMoved, Is.True, "CanvasRenderer.hasMoved should be true");
  44. Vector2 newChild1Pos = m_Child1GO.GetComponent<RectTransform>().anchoredPosition;
  45. Vector2 newChild2Pos = m_Child2GO.GetComponent<RectTransform>().anchoredPosition;
  46. Assert.That(newChild1Pos, Is.EqualTo(child2Pos), "Child1 should have moved to Child2's position");
  47. Assert.That(newChild2Pos, Is.EqualTo(child1Pos), "Child2 should have moved to Child1's position");
  48. }
  49. [TearDown]
  50. public void TearDown()
  51. {
  52. GameObject.DestroyImmediate(m_CanvasGO);
  53. }
  54. // Factory method for creating UI text objects taken from the original bug repro scene:
  55. private GameObject CreateTextObject(string name)
  56. {
  57. GameObject outputTextGameObject = new GameObject("OutputContent", typeof(CanvasRenderer));
  58. RectTransform outputTextTransform = outputTextGameObject.AddComponent<RectTransform>();
  59. outputTextTransform.pivot = new Vector2(0.5f, 0);
  60. outputTextTransform.anchorMin = Vector2.zero;
  61. outputTextTransform.anchorMax = new Vector2(1, 0);
  62. outputTextTransform.anchoredPosition = Vector2.zero;
  63. outputTextTransform.sizeDelta = Vector2.zero;
  64. Text outputText = outputTextGameObject.AddComponent<Text>();
  65. outputText.text = "Hello World!";
  66. outputTextGameObject.name = name;
  67. return outputTextGameObject;
  68. }
  69. }