暫無描述
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.

AspectRatioFitterTests.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using UnityEditor;
  5. using UnityEngine;
  6. using UnityEngine.TestTools;
  7. using UnityEngine.UI;
  8. using UnityEngine.UI.Tests;
  9. namespace LayoutTests
  10. {
  11. class AspectRatioFitterTests : IPrebuildSetup
  12. {
  13. const string kPrefabPath = "Assets/Resources/AspectRatioFitterTestsEnvelopeParent.prefab";
  14. const string kPrefabPath2 = "Assets/Resources/AspectRatioFitterTestsFitInParent.prefab";
  15. private GameObject m_PrefabRoot;
  16. private AspectRatioFitter m_AspectRatioFitter;
  17. private RectTransform m_RectTransform;
  18. [SetUp]
  19. public void Setup()
  20. {
  21. #if UNITY_EDITOR
  22. var cameraGo = new GameObject("Cam").AddComponent<Camera>();
  23. var panelGO = new GameObject("PanelObject", typeof(RectTransform));
  24. var panelRT = panelGO.GetComponent<RectTransform>();
  25. panelRT.sizeDelta = new Vector2(200, 200);
  26. var panelGO2 = new GameObject("PanelObject", typeof(RectTransform));
  27. var panelRT2 = panelGO2.GetComponent<RectTransform>();
  28. panelRT2.sizeDelta = new Vector2(200, 200);
  29. var testGO = new GameObject("TestObject", typeof(RectTransform), typeof(AspectRatioFitter));
  30. var image = testGO.AddComponent<Image>();
  31. image.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets\\download.jpeg");
  32. m_AspectRatioFitter = testGO.GetComponent<AspectRatioFitter>();
  33. m_AspectRatioFitter.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent;
  34. m_AspectRatioFitter.aspectRatio = 1.5f;
  35. testGO.transform.SetParent(panelGO.transform);
  36. var testGO2 = new GameObject("TestObject", typeof(RectTransform), typeof(AspectRatioFitter));
  37. var image2 = testGO2.AddComponent<Image>();
  38. image2.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets\\download.jpeg");
  39. m_AspectRatioFitter = testGO2.GetComponent<AspectRatioFitter>();
  40. m_AspectRatioFitter.aspectMode = AspectRatioFitter.AspectMode.FitInParent;
  41. m_AspectRatioFitter.aspectRatio = 1.5f;
  42. testGO2.transform.SetParent(panelGO2.transform);
  43. var testRT = testGO.GetComponent<RectTransform>();
  44. testRT.sizeDelta = new Vector2(150, 100);
  45. var testRT2 = testGO2.GetComponent<RectTransform>();
  46. testRT2.sizeDelta = new Vector2(150, 100);
  47. if (!Directory.Exists("Assets/Resources/"))
  48. Directory.CreateDirectory("Assets/Resources/");
  49. PrefabUtility.SaveAsPrefabAsset(panelGO, kPrefabPath);
  50. PrefabUtility.SaveAsPrefabAsset(panelGO2, kPrefabPath2);
  51. GameObject.DestroyImmediate(panelGO);
  52. GameObject.DestroyImmediate(panelGO2);
  53. #endif
  54. }
  55. [TearDown]
  56. public void TearDown()
  57. {
  58. GameObject.DestroyImmediate(m_PrefabRoot);
  59. m_PrefabRoot = null;
  60. m_AspectRatioFitter = null;
  61. m_RectTransform = null;
  62. }
  63. [OneTimeTearDown]
  64. public void OneTimeTearDown()
  65. {
  66. #if UNITY_EDITOR
  67. AssetDatabase.DeleteAsset(kPrefabPath);
  68. AssetDatabase.DeleteAsset(kPrefabPath2);
  69. #endif
  70. }
  71. [Test]
  72. public void TestEnvelopParent()
  73. {
  74. m_PrefabRoot = Object.Instantiate(Resources.Load("AspectRatioFitterTestsEnvelopeParent")) as GameObject;
  75. m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren<AspectRatioFitter>();
  76. m_AspectRatioFitter.enabled = true;
  77. m_RectTransform = m_AspectRatioFitter.GetComponent<RectTransform>();
  78. Assert.AreEqual(300, m_RectTransform.rect.width);
  79. }
  80. [Test]
  81. public void TestFitInParent()
  82. {
  83. m_PrefabRoot = Object.Instantiate(Resources.Load("AspectRatioFitterTestsFitInParent")) as GameObject;
  84. m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren<AspectRatioFitter>();
  85. m_AspectRatioFitter.enabled = true;
  86. m_RectTransform = m_AspectRatioFitter.GetComponent<RectTransform>();
  87. Assert.AreEqual(200, m_RectTransform.rect.width);
  88. }
  89. }
  90. }