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.

SceneWithNestedLayoutElementsLoads.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using UnityEngine.UI;
  3. using UnityEngine.TestTools;
  4. using NUnit.Framework;
  5. using System.Collections;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using UnityEditor;
  9. [TestFixture]
  10. [Category("RegressionTest")]
  11. public class SceneWithNestedLayoutElementsLoad : IPrebuildSetup
  12. {
  13. Scene m_InitScene;
  14. const string aspectRatioFitterSceneName = "AspectRatioFitter";
  15. const string contentSizeFitterSceneName = "ContentSizeFitter";
  16. const string layoutGroupSceneName = "LayoutGroup";
  17. public void Setup()
  18. {
  19. #if UNITY_EDITOR
  20. Action aspectRatioFitterSceneCreation = delegate()
  21. {
  22. var canvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
  23. var panelGO = new GameObject("Panel", typeof(UnityEngine.UI.Image), typeof(AspectRatioFitter));
  24. var imageGO = new GameObject("Image", typeof(UnityEngine.UI.RawImage), typeof(AspectRatioFitter));
  25. panelGO.transform.SetParent(canvasGO.transform);
  26. imageGO.transform.SetParent(panelGO.transform);
  27. var panelARF = panelGO.GetComponent<AspectRatioFitter>();
  28. panelARF.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent;
  29. panelARF.aspectRatio = 1.98f;
  30. var iamgeARF = imageGO.GetComponent<AspectRatioFitter>();
  31. iamgeARF.aspectMode = AspectRatioFitter.AspectMode.None;
  32. iamgeARF.aspectRatio = 1f;
  33. new GameObject("GameObject", typeof(SceneWithNestedLayoutElementsLoadScript));
  34. };
  35. CreateSceneUtility.CreateScene(aspectRatioFitterSceneName, aspectRatioFitterSceneCreation);
  36. Action contentSizeFitterSceneCreation = delegate()
  37. {
  38. var canvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
  39. var panelGO = new GameObject("Panel", typeof(UnityEngine.UI.Image), typeof(ContentSizeFitter), typeof(LayoutElement));
  40. var imageGO = new GameObject("Image", typeof(UnityEngine.UI.RawImage), typeof(ContentSizeFitter), typeof(LayoutElement));
  41. panelGO.transform.SetParent(canvasGO.transform);
  42. imageGO.transform.SetParent(panelGO.transform);
  43. var panelCSF = panelGO.GetComponent<ContentSizeFitter>();
  44. panelCSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  45. panelCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  46. var panelLE = panelGO.GetComponent<LayoutElement>();
  47. panelLE.preferredWidth = 200f;
  48. panelLE.preferredHeight = 200f;
  49. var imageCSF = imageGO.GetComponent<ContentSizeFitter>();
  50. imageCSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
  51. imageCSF.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
  52. var imageLE = imageGO.GetComponent<LayoutElement>();
  53. imageLE.preferredWidth = 100f;
  54. imageLE.preferredHeight = 100f;
  55. new GameObject("GameObject", typeof(SceneWithNestedLayoutElementsLoadScript));
  56. };
  57. CreateSceneUtility.CreateScene(contentSizeFitterSceneName, contentSizeFitterSceneCreation);
  58. Action layoutGroupSceneCreation = delegate()
  59. {
  60. var canvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GridLayoutGroup));
  61. var panelGO = new GameObject("Panel (0)", typeof(UnityEngine.UI.Image), typeof(GridLayoutGroup), typeof(LayoutElement));
  62. var imageGOOne = new GameObject("Image", typeof(UnityEngine.UI.RawImage), typeof(LayoutElement));
  63. var imageGOTwo = new GameObject("Image", typeof(UnityEngine.UI.RawImage), typeof(LayoutElement));
  64. panelGO.transform.SetParent(canvasGO.transform);
  65. imageGOOne.transform.SetParent(panelGO.transform);
  66. imageGOTwo.transform.SetParent(panelGO.transform);
  67. var panelLE = panelGO.GetComponent<LayoutElement>();
  68. panelLE.preferredWidth = 100f;
  69. panelLE.preferredHeight = 100f;
  70. var imageOneLE = imageGOOne.GetComponent<LayoutElement>();
  71. imageOneLE.preferredWidth = 100f;
  72. imageOneLE.preferredHeight = 100f;
  73. var imageTwoLE = imageGOOne.GetComponent<LayoutElement>();
  74. imageTwoLE.preferredWidth = 100f;
  75. imageTwoLE.preferredHeight = 100f;
  76. // Duplicate the first panel we created.
  77. GameObject.Instantiate(panelGO, canvasGO.transform);
  78. new GameObject("GameObject", typeof(SceneWithNestedLayoutElementsLoadScript));
  79. };
  80. CreateSceneUtility.CreateScene(layoutGroupSceneName, layoutGroupSceneCreation);
  81. #endif
  82. }
  83. [UnityTest]
  84. public IEnumerator SceneWithNestedAspectRatioFitterLoads()
  85. {
  86. AsyncOperation operation = SceneManager.LoadSceneAsync(aspectRatioFitterSceneName, LoadSceneMode.Additive);
  87. yield return operation;
  88. SceneManager.SetActiveScene(SceneManager.GetSceneByName(aspectRatioFitterSceneName));
  89. var go = GameObject.Find("GameObject");
  90. var component = go.GetComponent<SceneWithNestedLayoutElementsLoadScript>();
  91. yield return new WaitUntil(() => component.isStartCalled);
  92. operation = SceneManager.UnloadSceneAsync(aspectRatioFitterSceneName);
  93. yield return operation;
  94. }
  95. [UnityTest]
  96. public IEnumerator SceneWithNestedContentSizeFitterLoads()
  97. {
  98. AsyncOperation operation = SceneManager.LoadSceneAsync(contentSizeFitterSceneName, LoadSceneMode.Additive);
  99. yield return operation;
  100. SceneManager.SetActiveScene(SceneManager.GetSceneByName(contentSizeFitterSceneName));
  101. var go = GameObject.Find("GameObject");
  102. var component = go.GetComponent<SceneWithNestedLayoutElementsLoadScript>();
  103. yield return new WaitUntil(() => component.isStartCalled);
  104. operation = SceneManager.UnloadSceneAsync(contentSizeFitterSceneName);
  105. yield return operation;
  106. }
  107. [UnityTest]
  108. public IEnumerator SceneWithNestedLayoutGroupLoads()
  109. {
  110. AsyncOperation operation = SceneManager.LoadSceneAsync(layoutGroupSceneName, LoadSceneMode.Additive);
  111. yield return operation;
  112. SceneManager.SetActiveScene(SceneManager.GetSceneByName(layoutGroupSceneName));
  113. var go = GameObject.Find("GameObject");
  114. var component = go.GetComponent<SceneWithNestedLayoutElementsLoadScript>();
  115. yield return new WaitUntil(() => component.isStartCalled);
  116. operation = SceneManager.UnloadSceneAsync(layoutGroupSceneName);
  117. yield return operation;
  118. }
  119. [SetUp]
  120. public void TestSetup()
  121. {
  122. m_InitScene = SceneManager.GetActiveScene();
  123. }
  124. [TearDown]
  125. public void TearDown()
  126. {
  127. SceneManager.SetActiveScene(m_InitScene);
  128. }
  129. [OneTimeTearDown]
  130. public void OnTimeTearDown()
  131. {
  132. //Manually add Assets/ and .unity as CreateSceneUtility does that for you.
  133. #if UNITY_EDITOR
  134. AssetDatabase.DeleteAsset("Assets/" + aspectRatioFitterSceneName + ".unity");
  135. AssetDatabase.DeleteAsset("Assets/" + contentSizeFitterSceneName + ".unity");
  136. AssetDatabase.DeleteAsset("Assets/" + layoutGroupSceneName + ".unity");
  137. #endif
  138. }
  139. }