Brak opisu
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.

RectMask2DClipping.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /*
  9. This test checks that a maskableGraphic within a RectMask2D will be properly clipped.
  10. Test for case (1013182 - [RectMask2D] Child gameObject is masked if there are less than 2 corners of GO that matches RectMask's x position)
  11. */
  12. namespace UnityEngine.UI.Tests
  13. {
  14. public class RectMask2DClipping : IPrebuildSetup
  15. {
  16. GameObject m_PrefabRoot;
  17. GameObject m_CameraGO;
  18. const string kPrefabPath = "Assets/Resources/Mask2DRectCullingPrefab.prefab";
  19. public void Setup()
  20. {
  21. #if UNITY_EDITOR
  22. var rootGO = new GameObject("RootGO");
  23. var rootCanvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
  24. rootCanvasGO.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
  25. rootCanvasGO.transform.SetParent(rootGO.transform);
  26. var maskGO = new GameObject("Mask", typeof(RectTransform), typeof(RectMask2D));
  27. var maskTransform = maskGO.GetComponent<RectTransform>();
  28. maskTransform.SetParent(rootCanvasGO.transform);
  29. maskTransform.localPosition = Vector3.zero;
  30. maskTransform.sizeDelta = new Vector2(200, 200);
  31. maskTransform.localScale = Vector3.one;
  32. var imageGO = new GameObject("Image", typeof(RectTransform), typeof(ImageHook));
  33. var imageTransform = imageGO.GetComponent<RectTransform>();
  34. imageTransform.SetParent(maskTransform);
  35. imageTransform.localPosition = new Vector3(-125, 0, 0);
  36. imageTransform.sizeDelta = new Vector2(100, 100);
  37. imageTransform.localScale = Vector3.one;
  38. if (!Directory.Exists("Assets/Resources/"))
  39. Directory.CreateDirectory("Assets/Resources/");
  40. UnityEditor.PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
  41. GameObject.DestroyImmediate(rootGO);
  42. #endif
  43. }
  44. [SetUp]
  45. public void TestSetup()
  46. {
  47. m_PrefabRoot = Object.Instantiate(Resources.Load("Mask2DRectCullingPrefab")) as GameObject;
  48. m_CameraGO = new GameObject("Camera", typeof(Camera));
  49. }
  50. [UnityTest]
  51. public IEnumerator Mask2DRect_CorrectClipping()
  52. {
  53. //root->canvas->mask->image
  54. RectTransform t = m_PrefabRoot.transform.GetChild(0).GetChild(0).GetChild(0) as RectTransform;
  55. CanvasRenderer cr = t.GetComponent<CanvasRenderer>();
  56. bool cull = false;
  57. for (int i = 0; i < 360; i += 45)
  58. {
  59. t.localEulerAngles = new Vector3(0, 0, i);
  60. cull |= cr.cull;
  61. yield return null;
  62. }
  63. Assert.IsFalse(cull);
  64. }
  65. [Test]
  66. public void Mask2DRect_NonZeroPaddingMasksProperly()
  67. {
  68. var mask = m_PrefabRoot.GetComponentInChildren<RectMask2D>();
  69. mask.padding = new Vector4(10, 10, 10, 10);
  70. Canvas.ForceUpdateCanvases();
  71. var image = m_PrefabRoot.GetComponentInChildren<ImageHook>();
  72. // The mask rect is 200, and padding of 10 all around makes it so its 180 in size.
  73. Assert.AreEqual(180, image.cachedClipRect.height);
  74. Assert.AreEqual(180, image.cachedClipRect.width);
  75. Assert.AreEqual(-90f, image.cachedClipRect.x);
  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. }