설명 없음
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.

NestedCanvas.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.TestTools;
  5. using NUnit.Framework;
  6. using System.Collections;
  7. using UnityEditor;
  8. public class NestedCanvas : IPrebuildSetup
  9. {
  10. Object m_GO1;
  11. Object m_GO2;
  12. const string kPrefabPath = "Assets/Resources/NestedCanvasPrefab.prefab";
  13. public void Setup()
  14. {
  15. #if UNITY_EDITOR
  16. var rootGO = new GameObject("RootGO");
  17. var rootCanvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasGroup));
  18. rootCanvasGO.transform.SetParent(rootGO.transform);
  19. var nestedCanvas = new GameObject("Nested Canvas", typeof(Canvas), typeof(Image));
  20. nestedCanvas.transform.SetParent(rootCanvasGO.transform);
  21. var nestedCanvasCamera = new GameObject("Nested Canvas Camera", typeof(Camera));
  22. nestedCanvasCamera.transform.SetParent(rootCanvasGO.transform);
  23. var rootCanvas = rootCanvasGO.GetComponent<Canvas>();
  24. rootCanvas.renderMode = RenderMode.WorldSpace;
  25. rootCanvas.worldCamera = nestedCanvasCamera.GetComponent<Camera>();
  26. if (!Directory.Exists("Assets/Resources/"))
  27. Directory.CreateDirectory("Assets/Resources/");
  28. UnityEditor.PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  29. GameObject.DestroyImmediate(rootGO);
  30. #endif
  31. }
  32. [UnityTest]
  33. [Description("[UI] Button does not interact after nested canvas is used(case 892913)")]
  34. public IEnumerator WorldCanvas_CanFindCameraAfterDisablingAndEnablingRootCanvas()
  35. {
  36. m_GO1 = Object.Instantiate(Resources.Load("NestedCanvasPrefab"));
  37. yield return null;
  38. var nestedCanvasGo = GameObject.Find("Nested Canvas");
  39. var nestedCanvas = nestedCanvasGo.GetComponent<Canvas>();
  40. Assert.IsNotNull(nestedCanvas.worldCamera, "Expected the nested canvas worldCamera to NOT be null after loading the scene.");
  41. nestedCanvasGo.transform.parent.gameObject.SetActive(false);
  42. nestedCanvasGo.transform.parent.gameObject.SetActive(true);
  43. Assert.IsNotNull(nestedCanvas.worldCamera, "Expected the nested canvas worldCamera to NOT be null after the parent canvas has been re-enabled.");
  44. }
  45. [UnityTest]
  46. public IEnumerator WorldCanvas_CanFindTheSameCameraAfterDisablingAndEnablingRootCanvas()
  47. {
  48. m_GO2 = Object.Instantiate(Resources.Load("NestedCanvasPrefab"));
  49. yield return null;
  50. var nestedCanvasGo = GameObject.Find("Nested Canvas");
  51. var nestedCanvas = nestedCanvasGo.GetComponent<Canvas>();
  52. var worldCamera = nestedCanvas.worldCamera;
  53. nestedCanvasGo.transform.parent.gameObject.SetActive(false);
  54. nestedCanvasGo.transform.parent.gameObject.SetActive(true);
  55. Assert.AreEqual(worldCamera, nestedCanvas.worldCamera, "Expected the same world camera to be returned after the root canvas was disabled and then re-enabled.");
  56. }
  57. [UnityTest]
  58. public IEnumerator NestedCanvasHasProperInheritedAlpha()
  59. {
  60. GameObject root = (GameObject)Object.Instantiate(Resources.Load("NestedCanvasPrefab"));
  61. CanvasGroup group = root.GetComponentInChildren<CanvasGroup>();
  62. Image image = root.GetComponentInChildren<Image>();
  63. group.alpha = 0.5f;
  64. yield return null;
  65. Assert.True(image.canvasRenderer.GetInheritedAlpha() == 0.5f);
  66. GameObject.DestroyImmediate(root);
  67. }
  68. [TearDown]
  69. public void TearDown()
  70. {
  71. GameObject.DestroyImmediate(m_GO1);
  72. GameObject.DestroyImmediate(m_GO2);
  73. }
  74. [OneTimeTearDown]
  75. public void OneTimeTearDown()
  76. {
  77. #if UNITY_EDITOR
  78. AssetDatabase.DeleteAsset(kPrefabPath);
  79. #endif
  80. }
  81. }