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.

LayoutGroupScaling.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Collections;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. using UnityEngine.UI;
  8. // test for case 879374 - Checks that layout group children scale properly when scaleWidth / scaleHeight are toggled
  9. namespace LayoutTests
  10. {
  11. public class LayoutGroupScaling : IPrebuildSetup
  12. {
  13. GameObject m_PrefabRoot;
  14. GameObject m_CameraGO;
  15. const string kPrefabPath = "Assets/Resources/LayoutGroupScalingPrefab.prefab";
  16. public void Setup()
  17. {
  18. #if UNITY_EDITOR
  19. var rootGO = new GameObject("RootGO");
  20. var rootCanvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
  21. rootCanvasGO.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
  22. rootCanvasGO.transform.SetParent(rootGO.transform);
  23. var layoutGroupGO = new GameObject("LayoutGroup");
  24. layoutGroupGO.transform.SetParent(rootCanvasGO.transform);
  25. HorizontalLayoutGroup layoutGroup = layoutGroupGO.AddComponent<HorizontalLayoutGroup>();
  26. layoutGroup.childControlHeight = true;
  27. layoutGroup.childControlWidth = true;
  28. ContentSizeFitter contentSizeFitter = layoutGroupGO.AddComponent<ContentSizeFitter>();
  29. contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  30. contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  31. for (int i = 0; i < 10; i++)
  32. {
  33. var elementGO = new GameObject("image(" + i + ")", typeof(Image));
  34. var layoutElement = elementGO.AddComponent<LayoutElement>();
  35. layoutElement.preferredWidth = 50;
  36. layoutElement.preferredHeight = 50;
  37. elementGO.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
  38. elementGO.transform.SetParent(layoutGroupGO.transform);
  39. }
  40. if (!Directory.Exists("Assets/Resources/"))
  41. Directory.CreateDirectory("Assets/Resources/");
  42. UnityEditor.PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  43. GameObject.DestroyImmediate(rootGO);
  44. #endif
  45. }
  46. [SetUp]
  47. public void TestSetup()
  48. {
  49. m_PrefabRoot = Object.Instantiate(Resources.Load("LayoutGroupScalingPrefab")) as GameObject;
  50. m_CameraGO = new GameObject("Camera", typeof(Camera));
  51. }
  52. [UnityTest]
  53. public IEnumerator LayoutGroup_CorrectChildScaling()
  54. {
  55. GameObject layoutGroupGO = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>().gameObject;
  56. Rect dimentions = (layoutGroupGO.transform as RectTransform).rect;
  57. layoutGroupGO.GetComponent<HorizontalLayoutGroup>().childScaleWidth = true;
  58. layoutGroupGO.GetComponent<HorizontalLayoutGroup>().childScaleHeight = true;
  59. yield return null;
  60. Rect newDimentions = (layoutGroupGO.transform as RectTransform).rect;
  61. Assert.IsTrue(Mathf.Approximately(dimentions.width * 0.5f, newDimentions.width));
  62. Assert.IsTrue(Mathf.Approximately(dimentions.height * 0.5f, newDimentions.height));
  63. yield return null;
  64. Object.DestroyImmediate(layoutGroupGO.GetComponent<HorizontalLayoutGroup>());
  65. VerticalLayoutGroup layoutGroup = layoutGroupGO.AddComponent<VerticalLayoutGroup>();
  66. layoutGroup.childControlHeight = true;
  67. layoutGroup.childControlWidth = true;
  68. yield return null;
  69. dimentions = (layoutGroupGO.transform as RectTransform).rect;
  70. layoutGroup.childScaleWidth = true;
  71. layoutGroup.childScaleHeight = true;
  72. yield return null;
  73. newDimentions = (layoutGroupGO.transform as RectTransform).rect;
  74. Assert.IsTrue(Mathf.Approximately(dimentions.width * 0.5f, newDimentions.width));
  75. Assert.IsTrue(Mathf.Approximately(dimentions.height * 0.5f, newDimentions.height));
  76. }
  77. [TearDown]
  78. public void TearDown()
  79. {
  80. Object.DestroyImmediate(m_PrefabRoot);
  81. GameObject.DestroyImmediate(m_CameraGO);
  82. }
  83. [OneTimeTearDown]
  84. public void OneTimeTearDown()
  85. {
  86. #if UNITY_EDITOR
  87. AssetDatabase.DeleteAsset(kPrefabPath);
  88. #endif
  89. }
  90. }
  91. }