first commit
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
|
||||
namespace LayoutTests
|
||||
{
|
||||
class AspectRatioFitterTests : IPrebuildSetup
|
||||
{
|
||||
const string kPrefabPath = "Assets/Resources/AspectRatioFitterTestsEnvelopeParent.prefab";
|
||||
const string kPrefabPath2 = "Assets/Resources/AspectRatioFitterTestsFitInParent.prefab";
|
||||
|
||||
private GameObject m_PrefabRoot;
|
||||
private AspectRatioFitter m_AspectRatioFitter;
|
||||
private RectTransform m_RectTransform;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var cameraGo = new GameObject("Cam").AddComponent<Camera>();
|
||||
|
||||
var panelGO = new GameObject("PanelObject", typeof(RectTransform));
|
||||
var panelRT = panelGO.GetComponent<RectTransform>();
|
||||
panelRT.sizeDelta = new Vector2(200, 200);
|
||||
|
||||
var panelGO2 = new GameObject("PanelObject", typeof(RectTransform));
|
||||
var panelRT2 = panelGO2.GetComponent<RectTransform>();
|
||||
panelRT2.sizeDelta = new Vector2(200, 200);
|
||||
|
||||
var testGO = new GameObject("TestObject", typeof(RectTransform), typeof(AspectRatioFitter));
|
||||
var image = testGO.AddComponent<Image>();
|
||||
image.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets\\download.jpeg");
|
||||
m_AspectRatioFitter = testGO.GetComponent<AspectRatioFitter>();
|
||||
m_AspectRatioFitter.aspectMode = AspectRatioFitter.AspectMode.EnvelopeParent;
|
||||
m_AspectRatioFitter.aspectRatio = 1.5f;
|
||||
testGO.transform.SetParent(panelGO.transform);
|
||||
|
||||
var testGO2 = new GameObject("TestObject", typeof(RectTransform), typeof(AspectRatioFitter));
|
||||
var image2 = testGO2.AddComponent<Image>();
|
||||
image2.sprite = AssetDatabase.LoadAssetAtPath<Sprite>("Assets\\download.jpeg");
|
||||
m_AspectRatioFitter = testGO2.GetComponent<AspectRatioFitter>();
|
||||
m_AspectRatioFitter.aspectMode = AspectRatioFitter.AspectMode.FitInParent;
|
||||
m_AspectRatioFitter.aspectRatio = 1.5f;
|
||||
testGO2.transform.SetParent(panelGO2.transform);
|
||||
|
||||
var testRT = testGO.GetComponent<RectTransform>();
|
||||
testRT.sizeDelta = new Vector2(150, 100);
|
||||
|
||||
var testRT2 = testGO2.GetComponent<RectTransform>();
|
||||
testRT2.sizeDelta = new Vector2(150, 100);
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(panelGO, kPrefabPath);
|
||||
PrefabUtility.SaveAsPrefabAsset(panelGO2, kPrefabPath2);
|
||||
GameObject.DestroyImmediate(panelGO);
|
||||
GameObject.DestroyImmediate(panelGO2);
|
||||
#endif
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
GameObject.DestroyImmediate(m_PrefabRoot);
|
||||
m_PrefabRoot = null;
|
||||
m_AspectRatioFitter = null;
|
||||
m_RectTransform = null;
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
AssetDatabase.DeleteAsset(kPrefabPath2);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestEnvelopParent()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("AspectRatioFitterTestsEnvelopeParent")) as GameObject;
|
||||
m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren<AspectRatioFitter>();
|
||||
|
||||
m_AspectRatioFitter.enabled = true;
|
||||
|
||||
m_RectTransform = m_AspectRatioFitter.GetComponent<RectTransform>();
|
||||
Assert.AreEqual(300, m_RectTransform.rect.width);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFitInParent()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("AspectRatioFitterTestsFitInParent")) as GameObject;
|
||||
m_AspectRatioFitter = m_PrefabRoot.GetComponentInChildren<AspectRatioFitter>();
|
||||
|
||||
m_AspectRatioFitter.enabled = true;
|
||||
|
||||
m_RectTransform = m_AspectRatioFitter.GetComponent<RectTransform>();
|
||||
Assert.AreEqual(200, m_RectTransform.rect.width);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: be1b04bc4d8a415469493848e6501020
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,138 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
|
||||
namespace LayoutTests
|
||||
{
|
||||
class ContentSizeFitterTests : IPrebuildSetup
|
||||
{
|
||||
const string kPrefabPath = "Assets/Resources/ContentSizeFitterTests.prefab";
|
||||
|
||||
private GameObject m_PrefabRoot;
|
||||
private ContentSizeFitter m_ContentSizeFitter;
|
||||
private RectTransform m_RectTransform;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
GameObject canvasGO = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var canvas = canvasGO.GetComponent<Canvas>();
|
||||
canvas.referencePixelsPerUnit = 100;
|
||||
|
||||
var testGO = new GameObject("TestObject", typeof(RectTransform), typeof(ContentSizeFitter));
|
||||
testGO.transform.SetParent(canvasGO.transform);
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("ContentSizeFitterTests")) as GameObject;
|
||||
m_ContentSizeFitter = m_PrefabRoot.GetComponentInChildren<ContentSizeFitter>();
|
||||
|
||||
m_ContentSizeFitter.enabled = true;
|
||||
|
||||
m_RectTransform = m_ContentSizeFitter.GetComponent<RectTransform>();
|
||||
m_RectTransform.sizeDelta = new Vector2(50, 50);
|
||||
|
||||
GameObject testObject = m_ContentSizeFitter.gameObject;
|
||||
// set up components
|
||||
var componentA = testObject.AddComponent<LayoutElement>();
|
||||
componentA.minWidth = 5;
|
||||
componentA.minHeight = 10;
|
||||
componentA.preferredWidth = 100;
|
||||
componentA.preferredHeight = 105;
|
||||
componentA.flexibleWidth = 0;
|
||||
componentA.flexibleHeight = 0;
|
||||
componentA.enabled = true;
|
||||
|
||||
var componentB = testObject.AddComponent<LayoutElement>();
|
||||
componentB.minWidth = 15;
|
||||
componentB.minHeight = 20;
|
||||
componentB.preferredWidth = 110;
|
||||
componentB.preferredHeight = 115;
|
||||
componentB.flexibleWidth = 0;
|
||||
componentB.flexibleHeight = 0;
|
||||
componentB.enabled = true;
|
||||
|
||||
var componentC = testObject.AddComponent<LayoutElement>();
|
||||
componentC.minWidth = 25;
|
||||
componentC.minHeight = 30;
|
||||
componentC.preferredWidth = 120;
|
||||
componentC.preferredHeight = 125;
|
||||
componentC.flexibleWidth = 0;
|
||||
componentC.flexibleHeight = 0;
|
||||
componentC.enabled = true;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
GameObject.DestroyImmediate(m_PrefabRoot);
|
||||
m_PrefabRoot = null;
|
||||
m_ContentSizeFitter = null;
|
||||
m_RectTransform = null;
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFitModeUnconstrained()
|
||||
{
|
||||
m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
|
||||
m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.Unconstrained;
|
||||
|
||||
m_ContentSizeFitter.SetLayoutHorizontal();
|
||||
m_ContentSizeFitter.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(50, m_RectTransform.rect.width);
|
||||
Assert.AreEqual(50, m_RectTransform.rect.height);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFitModeMinSize()
|
||||
{
|
||||
m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.MinSize;
|
||||
m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.MinSize;
|
||||
|
||||
m_ContentSizeFitter.SetLayoutHorizontal();
|
||||
m_ContentSizeFitter.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(25, m_RectTransform.rect.width);
|
||||
Assert.AreEqual(30, m_RectTransform.rect.height);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFitModePreferredSize()
|
||||
{
|
||||
m_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
m_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
m_ContentSizeFitter.SetLayoutHorizontal();
|
||||
m_ContentSizeFitter.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(120, m_RectTransform.rect.width);
|
||||
Assert.AreEqual(125, m_RectTransform.rect.height);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 309b0604924786544a3786ec4073c5a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,475 @@
|
||||
using NUnit.Framework;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.TestTools;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
|
||||
class GridLayoutGroupTests : IPrebuildSetup
|
||||
{
|
||||
const string kPrefabPath = "Assets/Resources/GridLayoutGroupTests.prefab";
|
||||
private GameObject m_PrefabRoot;
|
||||
private GridLayoutGroup m_LayoutGroup;
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
|
||||
var canvasGO = new GameObject("Canvas");
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
Canvas canvas = canvasGO.AddComponent<Canvas>();
|
||||
canvas.referencePixelsPerUnit = 100;
|
||||
|
||||
var groupGO = new GameObject("Group", typeof(RectTransform), typeof(GridLayoutGroup));
|
||||
groupGO.transform.SetParent(canvas.transform);
|
||||
|
||||
var rectTransform = groupGO.GetComponent<RectTransform>();
|
||||
rectTransform.sizeDelta = new Vector2(400, 500);
|
||||
rectTransform.anchoredPosition = new Vector2(0, 0);
|
||||
rectTransform.pivot = new Vector2(0, 0);
|
||||
|
||||
var layoutGroup = groupGO.GetComponent<GridLayoutGroup>();
|
||||
layoutGroup.spacing = new Vector2(10, 0);
|
||||
layoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
|
||||
layoutGroup.cellSize = new Vector2(90, 50);
|
||||
layoutGroup.constraint = GridLayoutGroup.Constraint.Flexible;
|
||||
layoutGroup.startAxis = GridLayoutGroup.Axis.Horizontal;
|
||||
layoutGroup.childAlignment = TextAnchor.UpperLeft;
|
||||
layoutGroup.enabled = false;
|
||||
layoutGroup.enabled = true;
|
||||
|
||||
var el1 = new GameObject("Element1");
|
||||
el1.transform.SetParent(rectTransform);
|
||||
var element1 = el1.AddComponent<LayoutElement>();
|
||||
(el1.transform as RectTransform).pivot = Vector2.zero;
|
||||
element1.minWidth = 5;
|
||||
element1.minHeight = 10;
|
||||
element1.preferredWidth = 100;
|
||||
element1.preferredHeight = 50;
|
||||
element1.flexibleWidth = 0;
|
||||
element1.flexibleHeight = 0;
|
||||
element1.enabled = true;
|
||||
|
||||
var el2 = new GameObject("Element2");
|
||||
el2.transform.SetParent(rectTransform);
|
||||
var element2 = el2.AddComponent<LayoutElement>();
|
||||
(el2.transform as RectTransform).pivot = Vector2.zero;
|
||||
element2.minWidth = 10;
|
||||
element2.minHeight = 5;
|
||||
element2.preferredWidth = -1;
|
||||
element2.preferredHeight = -1;
|
||||
element2.flexibleWidth = 0;
|
||||
element2.flexibleHeight = 0;
|
||||
element2.enabled = true;
|
||||
|
||||
var el3 = new GameObject("Element3");
|
||||
el3.transform.SetParent(rectTransform);
|
||||
var element3 = el3.AddComponent<LayoutElement>();
|
||||
(el3.transform as RectTransform).pivot = Vector2.zero;
|
||||
element3.minWidth = 60;
|
||||
element3.minHeight = 25;
|
||||
element3.preferredWidth = 120;
|
||||
element3.preferredHeight = 40;
|
||||
element3.flexibleWidth = 1;
|
||||
element3.flexibleHeight = 1;
|
||||
element3.enabled = true;
|
||||
|
||||
var el4 = new GameObject("Element4");
|
||||
el4.transform.SetParent(rectTransform);
|
||||
var element4 = el4.AddComponent<LayoutElement>();
|
||||
(el4.transform as RectTransform).pivot = Vector2.zero;
|
||||
element4.minWidth = 60;
|
||||
element4.minHeight = 25;
|
||||
element4.preferredWidth = 120;
|
||||
element4.preferredHeight = 40;
|
||||
element4.flexibleWidth = 1;
|
||||
element4.flexibleHeight = 1;
|
||||
element4.enabled = true;
|
||||
|
||||
var el5 = new GameObject("Element5");
|
||||
el5.transform.SetParent(rectTransform);
|
||||
var element5 = el5.AddComponent<LayoutElement>();
|
||||
(el5.transform as RectTransform).pivot = Vector2.zero;
|
||||
element5.minWidth = 60;
|
||||
element5.minHeight = 25;
|
||||
element5.preferredWidth = 120;
|
||||
element5.preferredHeight = 40;
|
||||
element5.flexibleWidth = 1;
|
||||
element5.flexibleHeight = 1;
|
||||
element5.enabled = true;
|
||||
|
||||
var el6 = new GameObject("Element6");
|
||||
el6.transform.SetParent(rectTransform);
|
||||
var element6 = el6.AddComponent<LayoutElement>();
|
||||
(el6.transform as RectTransform).pivot = Vector2.zero;
|
||||
element6.minWidth = 60;
|
||||
element6.minHeight = 25;
|
||||
element6.preferredWidth = 120;
|
||||
element6.preferredHeight = 40;
|
||||
element6.flexibleWidth = 1;
|
||||
element6.flexibleHeight = 1;
|
||||
element6.enabled = true;
|
||||
|
||||
var el7 = new GameObject("Element7");
|
||||
el7.transform.SetParent(rectTransform);
|
||||
var element7 = el7.AddComponent<LayoutElement>();
|
||||
(el7.transform as RectTransform).pivot = Vector2.zero;
|
||||
element7.minWidth = 60;
|
||||
element7.minHeight = 25;
|
||||
element7.preferredWidth = 120;
|
||||
element7.preferredHeight = 40;
|
||||
element7.flexibleWidth = 1;
|
||||
element7.flexibleHeight = 1;
|
||||
element7.enabled = true;
|
||||
|
||||
var el8 = new GameObject("Element8");
|
||||
el8.transform.SetParent(rectTransform);
|
||||
var element8 = el8.AddComponent<LayoutElement>();
|
||||
(el8.transform as RectTransform).pivot = Vector2.zero;
|
||||
element8.minWidth = 60;
|
||||
element8.minHeight = 25;
|
||||
element8.preferredWidth = 120;
|
||||
element8.preferredHeight = 40;
|
||||
element8.flexibleWidth = 1;
|
||||
element8.flexibleHeight = 1;
|
||||
element8.enabled = true;
|
||||
|
||||
var el9 = new GameObject("Element9");
|
||||
el9.transform.SetParent(rectTransform);
|
||||
var element9 = el9.AddComponent<LayoutElement>();
|
||||
(el9.transform as RectTransform).pivot = Vector2.zero;
|
||||
element9.minWidth = 500;
|
||||
element9.minHeight = 300;
|
||||
element9.preferredWidth = 1000;
|
||||
element9.preferredHeight = 600;
|
||||
element9.flexibleWidth = 1;
|
||||
element9.flexibleHeight = 1;
|
||||
element9.enabled = true;
|
||||
element9.ignoreLayout = true;
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = GameObject.Instantiate(Resources.Load("GridLayoutGroupTests")) as GameObject;
|
||||
m_LayoutGroup = m_PrefabRoot.GetComponentInChildren<GridLayoutGroup>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
GameObject.DestroyImmediate(m_PrefabRoot);
|
||||
m_LayoutGroup = null;
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestFlexibleCalculateLayout()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.Flexible;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.Flexible, m_LayoutGroup.constraint);
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
|
||||
|
||||
Assert.AreEqual(90, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(290, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(200, -50),
|
||||
new Vector2(300, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(200, -100),
|
||||
new Vector2(300, -100),
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHorizontallyContrainedCalculateLayoutHorizontal()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
|
||||
m_LayoutGroup.constraintCount = 2;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
|
||||
|
||||
Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(200, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(200, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(0, -150),
|
||||
new Vector2(100, -150),
|
||||
new Vector2(0, -200),
|
||||
new Vector2(100, -200),
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestVerticallyContrainedCalculateLayoutHorizontal()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
|
||||
m_LayoutGroup.constraintCount = 2;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
|
||||
|
||||
Assert.AreEqual(390, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(390, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(200, -50),
|
||||
new Vector2(300, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(200, -100),
|
||||
new Vector2(300, -100),
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHorizontallyContrainedCalculateLayoutVertical()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
|
||||
m_LayoutGroup.constraintCount = 2;
|
||||
m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
LayoutRebuilder.ForceRebuildLayoutImmediate(m_LayoutGroup.GetComponent<RectTransform>());
|
||||
|
||||
Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(200, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(200, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(0, -150),
|
||||
new Vector2(0, -200),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(100, -150),
|
||||
new Vector2(100, -200),
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestVerticallyContrainedCalculateLayoutVertical()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
|
||||
m_LayoutGroup.constraintCount = 2;
|
||||
m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
|
||||
m_LayoutGroup.startCorner = GridLayoutGroup.Corner.LowerRight;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(2, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
m_LayoutGroup.CalculateLayoutInputHorizontal();
|
||||
m_LayoutGroup.SetLayoutHorizontal();
|
||||
m_LayoutGroup.CalculateLayoutInputVertical();
|
||||
m_LayoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(390, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(390, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(300, -100),
|
||||
new Vector2(300, -50),
|
||||
new Vector2(200, -100),
|
||||
new Vector2(200, -50),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(0, -50),
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestHorizontallyContrainedCalculateLayoutHorizontal_WithChildrenToMove()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
|
||||
m_LayoutGroup.constraintCount = 5;
|
||||
m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Horizontal;
|
||||
m_LayoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedRowCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(5, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
m_LayoutGroup.CalculateLayoutInputHorizontal();
|
||||
m_LayoutGroup.SetLayoutHorizontal();
|
||||
m_LayoutGroup.CalculateLayoutInputVertical();
|
||||
m_LayoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(190, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(250, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(190, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(250, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(0, -150),
|
||||
new Vector2(100, -150),
|
||||
new Vector2(0, -200),
|
||||
new Vector2(0, -250)
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestVerticallyContrainedCalculateLayoutVertical_WithChildrenToMove()
|
||||
{
|
||||
m_LayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
|
||||
m_LayoutGroup.constraintCount = 5;
|
||||
m_LayoutGroup.startAxis = GridLayoutGroup.Axis.Vertical;
|
||||
m_LayoutGroup.startCorner = GridLayoutGroup.Corner.UpperLeft;
|
||||
Assert.AreEqual(GridLayoutGroup.Constraint.FixedColumnCount, m_LayoutGroup.constraint, "Expected layout group constraint mode to match but it did not");
|
||||
Assert.AreEqual(5, m_LayoutGroup.constraintCount, "Expected layout group constraint count mode to match but it did not");
|
||||
m_LayoutGroup.CalculateLayoutInputHorizontal();
|
||||
m_LayoutGroup.SetLayoutHorizontal();
|
||||
m_LayoutGroup.CalculateLayoutInputVertical();
|
||||
m_LayoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(490, m_LayoutGroup.minWidth, "Expected layout group min width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.minHeight, "Expected layout group min height to match but it did not");
|
||||
Assert.AreEqual(490, m_LayoutGroup.preferredWidth, "Expected layout group preferred width to match but it did not");
|
||||
Assert.AreEqual(100, m_LayoutGroup.preferredHeight, "Expected layout group preferred height to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleWidth, "Expected layout group flexiblle width to match but it did not");
|
||||
Assert.AreEqual(-1, m_LayoutGroup.flexibleHeight, "Expected layout group flexiblle height to match but it did not");
|
||||
|
||||
Vector2[] expectedPositions =
|
||||
{
|
||||
new Vector2(0, -50),
|
||||
new Vector2(0, -100),
|
||||
new Vector2(100, -50),
|
||||
new Vector2(100, -100),
|
||||
new Vector2(200, -50),
|
||||
new Vector2(200, -100),
|
||||
new Vector2(300, -50),
|
||||
new Vector2(400, -50)
|
||||
};
|
||||
|
||||
Vector2 expectedSize = new Vector2(90, 50);
|
||||
|
||||
for (int i = 0; i < expectedPositions.Length; ++i)
|
||||
{
|
||||
var element = m_LayoutGroup.transform.Find("Element" + (i + 1));
|
||||
var rectTransform = element.GetComponent<RectTransform>();
|
||||
|
||||
Assert.AreEqual(expectedPositions[i], rectTransform.anchoredPosition, $"Expected Element { i + 1 } position to match but it did not");
|
||||
Assert.AreEqual(expectedSize, rectTransform.sizeDelta, $"Expected Element { i + 1 } size to match but it did not");
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9eb962217e00bf4fbc734e109991fca
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,165 @@
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.TestTools;
|
||||
|
||||
|
||||
namespace LayoutTests
|
||||
{
|
||||
public class HorizontalLayoutGroupTests : IPrebuildSetup
|
||||
{
|
||||
GameObject m_PrefabRoot;
|
||||
const string kPrefabPath = "Assets/Resources/HorizontalLayoutGroupPrefab.prefab";
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
var canvasGO = new GameObject("Canvas", typeof(Canvas));
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var groupGO = new GameObject("Group", typeof(RectTransform), typeof(HorizontalLayoutGroup));
|
||||
groupGO.transform.SetParent(canvasGO.transform);
|
||||
var horizontalLayoutGroup = groupGO.GetComponent<HorizontalLayoutGroup>();
|
||||
horizontalLayoutGroup.padding = new RectOffset(2, 4, 3, 5);
|
||||
horizontalLayoutGroup.spacing = 1;
|
||||
horizontalLayoutGroup.childForceExpandWidth = false;
|
||||
horizontalLayoutGroup.childForceExpandHeight = false;
|
||||
horizontalLayoutGroup.childControlWidth = true;
|
||||
horizontalLayoutGroup.childControlHeight = true;
|
||||
|
||||
var element1GO = new GameObject("Element1", typeof(RectTransform), typeof(LayoutElement));
|
||||
element1GO.transform.SetParent(groupGO.transform);
|
||||
var layoutElement1 = element1GO.GetComponent<LayoutElement>();
|
||||
layoutElement1.minWidth = 5;
|
||||
layoutElement1.minHeight = 10;
|
||||
layoutElement1.preferredWidth = 100;
|
||||
layoutElement1.preferredHeight = 50;
|
||||
layoutElement1.flexibleWidth = 0;
|
||||
layoutElement1.flexibleHeight = 0;
|
||||
|
||||
var element2GO = new GameObject("Element2", typeof(RectTransform), typeof(LayoutElement));
|
||||
element2GO.transform.SetParent(groupGO.transform);
|
||||
var layoutElement2 = element2GO.GetComponent<LayoutElement>();
|
||||
layoutElement2.minWidth = 10;
|
||||
layoutElement2.minHeight = 5;
|
||||
layoutElement2.preferredWidth = -1;
|
||||
layoutElement2.preferredHeight = -1;
|
||||
layoutElement2.flexibleWidth = 0;
|
||||
layoutElement2.flexibleHeight = 0;
|
||||
|
||||
var element3GO = new GameObject("Element3", typeof(RectTransform), typeof(LayoutElement));
|
||||
element3GO.transform.SetParent(groupGO.transform);
|
||||
var layoutElement3 = element3GO.GetComponent<LayoutElement>();
|
||||
layoutElement3.minWidth = 25;
|
||||
layoutElement3.minHeight = 15;
|
||||
layoutElement3.preferredWidth = 200;
|
||||
layoutElement3.preferredHeight = 80;
|
||||
layoutElement3.flexibleWidth = 1;
|
||||
layoutElement3.flexibleHeight = 1;
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("HorizontalLayoutGroupPrefab")) as GameObject;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
GameObject.DestroyImmediate(m_PrefabRoot);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutInputHorizontal()
|
||||
{
|
||||
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(48, layoutGroup.minWidth);
|
||||
Assert.AreEqual(318, layoutGroup.preferredWidth);
|
||||
Assert.AreEqual(1, layoutGroup.flexibleWidth);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutInputVertical()
|
||||
{
|
||||
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(23, layoutGroup.minHeight);
|
||||
Assert.AreEqual(88, layoutGroup.preferredHeight);
|
||||
Assert.AreEqual(1, layoutGroup.flexibleHeight);
|
||||
Assert.AreEqual(1, layoutGroup.flexibleHeight);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutHorizontal()
|
||||
{
|
||||
var parentGO = m_PrefabRoot.transform.GetChild(0).GetChild(0);
|
||||
var element1GO = parentGO.GetChild(0);
|
||||
var element1Trans = element1GO.GetComponent<RectTransform>();
|
||||
var element2GO = parentGO.GetChild(1);
|
||||
var element2Trans = element2GO.GetComponent<RectTransform>();
|
||||
var element3GO = parentGO.GetChild(2);
|
||||
var element3Trans = element3GO.GetComponent<RectTransform>();
|
||||
|
||||
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(13.6f, element1Trans.anchoredPosition.x, 0.1f);
|
||||
Assert.AreEqual(31.3f, element2Trans.anchoredPosition.x, 0.1f);
|
||||
Assert.AreEqual(66.6f, element3Trans.anchoredPosition.x, 0.1f);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutHorizontalReversed()
|
||||
{
|
||||
var parentGO = m_PrefabRoot.transform.GetChild(0).GetChild(0);
|
||||
var element1GO = parentGO.GetChild(0);
|
||||
var element1Trans = element1GO.GetComponent<RectTransform>();
|
||||
var element2GO = parentGO.GetChild(1);
|
||||
var element2Trans = element2GO.GetComponent<RectTransform>();
|
||||
var element3GO = parentGO.GetChild(2);
|
||||
var element3Trans = element3GO.GetComponent<RectTransform>();
|
||||
|
||||
HorizontalLayoutGroup layoutGroup = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>();
|
||||
layoutGroup.reverseArrangement = true;
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(84.4f, element1Trans.anchoredPosition.x, 0.1f);
|
||||
Assert.AreEqual(66.7f, element2Trans.anchoredPosition.x, 0.1f);
|
||||
Assert.AreEqual(31.4f, element3Trans.anchoredPosition.x, 0.1f);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5af3322745e78aa488fca5a2090f8755
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,103 @@
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEngine.UI;
|
||||
|
||||
// test for case 879374 - Checks that layout group children scale properly when scaleWidth / scaleHeight are toggled
|
||||
namespace LayoutTests
|
||||
{
|
||||
public class LayoutGroupScaling : IPrebuildSetup
|
||||
{
|
||||
GameObject m_PrefabRoot;
|
||||
GameObject m_CameraGO;
|
||||
|
||||
const string kPrefabPath = "Assets/Resources/LayoutGroupScalingPrefab.prefab";
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("RootGO");
|
||||
var rootCanvasGO = new GameObject("Canvas", typeof(Canvas), typeof(CanvasScaler));
|
||||
rootCanvasGO.GetComponent<Canvas>().renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
rootCanvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var layoutGroupGO = new GameObject("LayoutGroup");
|
||||
layoutGroupGO.transform.SetParent(rootCanvasGO.transform);
|
||||
HorizontalLayoutGroup layoutGroup = layoutGroupGO.AddComponent<HorizontalLayoutGroup>();
|
||||
layoutGroup.childControlHeight = true;
|
||||
layoutGroup.childControlWidth = true;
|
||||
ContentSizeFitter contentSizeFitter = layoutGroupGO.AddComponent<ContentSizeFitter>();
|
||||
contentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
contentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
var elementGO = new GameObject("image(" + i + ")", typeof(Image));
|
||||
var layoutElement = elementGO.AddComponent<LayoutElement>();
|
||||
layoutElement.preferredWidth = 50;
|
||||
layoutElement.preferredHeight = 50;
|
||||
elementGO.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
|
||||
elementGO.transform.SetParent(layoutGroupGO.transform);
|
||||
}
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
UnityEditor.PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("LayoutGroupScalingPrefab")) as GameObject;
|
||||
m_CameraGO = new GameObject("Camera", typeof(Camera));
|
||||
}
|
||||
|
||||
[UnityTest]
|
||||
public IEnumerator LayoutGroup_CorrectChildScaling()
|
||||
{
|
||||
GameObject layoutGroupGO = m_PrefabRoot.GetComponentInChildren<HorizontalLayoutGroup>().gameObject;
|
||||
Rect dimentions = (layoutGroupGO.transform as RectTransform).rect;
|
||||
layoutGroupGO.GetComponent<HorizontalLayoutGroup>().childScaleWidth = true;
|
||||
layoutGroupGO.GetComponent<HorizontalLayoutGroup>().childScaleHeight = true;
|
||||
yield return null;
|
||||
Rect newDimentions = (layoutGroupGO.transform as RectTransform).rect;
|
||||
Assert.IsTrue(Mathf.Approximately(dimentions.width * 0.5f, newDimentions.width));
|
||||
Assert.IsTrue(Mathf.Approximately(dimentions.height * 0.5f, newDimentions.height));
|
||||
yield return null;
|
||||
Object.DestroyImmediate(layoutGroupGO.GetComponent<HorizontalLayoutGroup>());
|
||||
VerticalLayoutGroup layoutGroup = layoutGroupGO.AddComponent<VerticalLayoutGroup>();
|
||||
layoutGroup.childControlHeight = true;
|
||||
layoutGroup.childControlWidth = true;
|
||||
yield return null;
|
||||
dimentions = (layoutGroupGO.transform as RectTransform).rect;
|
||||
layoutGroup.childScaleWidth = true;
|
||||
layoutGroup.childScaleHeight = true;
|
||||
yield return null;
|
||||
newDimentions = (layoutGroupGO.transform as RectTransform).rect;
|
||||
Assert.IsTrue(Mathf.Approximately(dimentions.width * 0.5f, newDimentions.width));
|
||||
Assert.IsTrue(Mathf.Approximately(dimentions.height * 0.5f, newDimentions.height));
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Object.DestroyImmediate(m_PrefabRoot);
|
||||
GameObject.DestroyImmediate(m_CameraGO);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 589533fb2413e3e4fba7df13a6a75bf2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,177 @@
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.TestTools;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.UI.Tests;
|
||||
|
||||
namespace LayoutTests
|
||||
{
|
||||
public class VerticalLayoutGroupTests : IPrebuildSetup
|
||||
{
|
||||
GameObject m_PrefabRoot;
|
||||
const string kPrefabPath = "Assets/Resources/VerticalLayoutGroupPrefab.prefab";
|
||||
|
||||
public void Setup()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var rootGO = new GameObject("rootGo");
|
||||
GameObject canvasGO = new GameObject("Canvas", typeof(RectTransform), typeof(Canvas));
|
||||
canvasGO.transform.SetParent(rootGO.transform);
|
||||
|
||||
var canvas = canvasGO.GetComponent<Canvas>();
|
||||
canvas.referencePixelsPerUnit = 100;
|
||||
|
||||
var groupGO = new GameObject("Group", typeof(RectTransform), typeof(VerticalLayoutGroup));
|
||||
groupGO.transform.SetParent(canvasGO.transform);
|
||||
|
||||
var element1GO = new GameObject("Element1", typeof(RectTransform), typeof(LayoutElement));
|
||||
element1GO.transform.SetParent(groupGO.transform);
|
||||
|
||||
var element2GO = new GameObject("Element2", typeof(RectTransform), typeof(LayoutElement));
|
||||
element2GO.transform.SetParent(groupGO.transform);
|
||||
|
||||
var element3GO = new GameObject("Element3", typeof(RectTransform), typeof(LayoutElement));
|
||||
element3GO.transform.SetParent(groupGO.transform);
|
||||
|
||||
VerticalLayoutGroup layoutGroup = groupGO.GetComponent<VerticalLayoutGroup>();
|
||||
layoutGroup.padding = new RectOffset(2, 4, 3, 5);
|
||||
layoutGroup.spacing = 1;
|
||||
layoutGroup.childForceExpandWidth = false;
|
||||
layoutGroup.childForceExpandHeight = false;
|
||||
layoutGroup.childControlWidth = true;
|
||||
layoutGroup.childControlHeight = true;
|
||||
|
||||
var element1 = element1GO.GetComponent<LayoutElement>();
|
||||
element1.minWidth = 5;
|
||||
element1.minHeight = 10;
|
||||
element1.preferredWidth = 100;
|
||||
element1.preferredHeight = 50;
|
||||
element1.flexibleWidth = 0;
|
||||
element1.flexibleHeight = 0;
|
||||
element1.enabled = true;
|
||||
|
||||
var element2 = element2GO.GetComponent<LayoutElement>();
|
||||
element2.minWidth = 10;
|
||||
element2.minHeight = 5;
|
||||
element2.preferredWidth = -1;
|
||||
element2.preferredHeight = -1;
|
||||
element2.flexibleWidth = 0;
|
||||
element2.flexibleHeight = 0;
|
||||
element2.enabled = true;
|
||||
|
||||
var element3 = element3GO.GetComponent<LayoutElement>();
|
||||
element3.minWidth = 25;
|
||||
element3.minHeight = 15;
|
||||
element3.preferredWidth = 200;
|
||||
element3.preferredHeight = 80;
|
||||
element3.flexibleWidth = 1;
|
||||
element3.flexibleHeight = 1;
|
||||
element3.enabled = true;
|
||||
|
||||
if (!Directory.Exists("Assets/Resources/"))
|
||||
Directory.CreateDirectory("Assets/Resources/");
|
||||
|
||||
PrefabUtility.SaveAsPrefabAsset(rootGO, kPrefabPath);
|
||||
GameObject.DestroyImmediate(rootGO);
|
||||
#endif
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void TestSetup()
|
||||
{
|
||||
m_PrefabRoot = Object.Instantiate(Resources.Load("VerticalLayoutGroupPrefab")) as GameObject;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
Object.DestroyImmediate(m_PrefabRoot);
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
AssetDatabase.DeleteAsset(kPrefabPath);
|
||||
#endif
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutInputHorizontal()
|
||||
{
|
||||
var layoutGroup = m_PrefabRoot.GetComponentInChildren<VerticalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(31, layoutGroup.minWidth);
|
||||
Assert.AreEqual(206, layoutGroup.preferredWidth);
|
||||
Assert.AreEqual(1, layoutGroup.flexibleWidth);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutInputVertical()
|
||||
{
|
||||
var layoutGroup = m_PrefabRoot.GetComponentInChildren<VerticalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(40, layoutGroup.minHeight);
|
||||
Assert.AreEqual(145, layoutGroup.preferredHeight);
|
||||
Assert.AreEqual(1, layoutGroup.flexibleHeight);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutVertical()
|
||||
{
|
||||
var parentGO = m_PrefabRoot.transform.GetChild(0).GetChild(0);
|
||||
var element1GO = parentGO.GetChild(0);
|
||||
var element1Trans = element1GO.GetComponent<RectTransform>();
|
||||
var element2GO = parentGO.GetChild(1);
|
||||
var element2Trans = element2GO.GetComponent<RectTransform>();
|
||||
var element3GO = parentGO.GetChild(2);
|
||||
var element3Trans = element3GO.GetComponent<RectTransform>();
|
||||
|
||||
var layoutGroup = m_PrefabRoot.GetComponentInChildren<VerticalLayoutGroup>();
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
Assert.AreEqual(-19.4f, element1Trans.anchoredPosition.y, 0.1f);
|
||||
Assert.AreEqual(-39.4f, element2Trans.anchoredPosition.y, 0.1f);
|
||||
Assert.AreEqual(-68.9f, element3Trans.anchoredPosition.y, 0.1f);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCalculateLayoutVerticalReversed()
|
||||
{
|
||||
var parentGO = m_PrefabRoot.transform.GetChild(0).GetChild(0);
|
||||
var element1GO = parentGO.GetChild(0);
|
||||
var element1Trans = element1GO.GetComponent<RectTransform>();
|
||||
var element2GO = parentGO.GetChild(1);
|
||||
var element2Trans = element2GO.GetComponent<RectTransform>();
|
||||
var element3GO = parentGO.GetChild(2);
|
||||
var element3Trans = element3GO.GetComponent<RectTransform>();
|
||||
|
||||
var layoutGroup = m_PrefabRoot.GetComponentInChildren<VerticalLayoutGroup>();
|
||||
layoutGroup.reverseArrangement = true;
|
||||
layoutGroup.CalculateLayoutInputHorizontal();
|
||||
layoutGroup.SetLayoutHorizontal();
|
||||
layoutGroup.CalculateLayoutInputVertical();
|
||||
layoutGroup.SetLayoutVertical();
|
||||
|
||||
|
||||
//Assert.AreEqual(-78.6f, element1Trans.anchoredPosition.y, 0.1f);
|
||||
Assert.AreEqual(-58.6f, element2Trans.anchoredPosition.y, 0.1f);
|
||||
Assert.AreEqual(-29.1f, element3Trans.anchoredPosition.y, 0.1f);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a4b6301a5afaec4cb87d21383ceb0b3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user