2022-05-08 12:09:30 +01:00
|
|
|
#nullable enable
|
|
|
|
|
2023-04-07 01:34:08 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2022-05-08 12:09:30 +01:00
|
|
|
using NUnit.Framework;
|
|
|
|
using UnityEngine;
|
2023-04-07 01:21:56 +01:00
|
|
|
using Object = UnityEngine.Object;
|
2022-05-08 12:09:30 +01:00
|
|
|
|
2022-05-08 12:17:57 +01:00
|
|
|
namespace X10D.Unity.Tests
|
2022-05-08 12:09:30 +01:00
|
|
|
{
|
|
|
|
public class GameObjectTests
|
|
|
|
{
|
2023-04-10 13:38:56 +01:00
|
|
|
[Test]
|
|
|
|
public void GetComponentsInChildrenOnly_ShouldIgnoreParent()
|
2022-05-08 12:09:30 +01:00
|
|
|
{
|
|
|
|
var parent = new GameObject();
|
|
|
|
parent.AddComponent<Rigidbody>();
|
|
|
|
|
|
|
|
var child = new GameObject();
|
|
|
|
child.transform.SetParent(parent.transform);
|
|
|
|
child.AddComponent<Rigidbody>();
|
|
|
|
|
|
|
|
Rigidbody[] components = parent.GetComponentsInChildrenOnly<Rigidbody>();
|
2023-04-07 01:34:34 +01:00
|
|
|
Assert.That(components, Has.Length.EqualTo(1));
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(child, Is.EqualTo(components[0].gameObject));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
2023-04-07 01:21:56 +01:00
|
|
|
Object.Destroy(parent);
|
|
|
|
Object.Destroy(child);
|
2022-05-08 12:09:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 13:38:56 +01:00
|
|
|
[Test]
|
2023-04-07 01:34:08 +01:00
|
|
|
[SuppressMessage("ReSharper", "Unity.InefficientPropertyAccess", Justification = "False positive.")]
|
2023-04-10 13:38:56 +01:00
|
|
|
public void LookAt_ShouldRotateSameAsTransform()
|
2022-05-08 12:09:30 +01:00
|
|
|
{
|
|
|
|
var first = new GameObject {transform = {position = Vector3.zero, rotation = Quaternion.identity}};
|
|
|
|
var second = new GameObject {transform = {position = Vector3.right, rotation = Quaternion.identity}};
|
|
|
|
Transform firstTransform = first.transform;
|
|
|
|
Transform secondTransform = second.transform;
|
|
|
|
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(Quaternion.identity));
|
|
|
|
Assert.That(secondTransform.rotation, Is.EqualTo(Quaternion.identity));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
firstTransform.LookAt(secondTransform);
|
|
|
|
Quaternion expected = firstTransform.rotation;
|
|
|
|
|
|
|
|
firstTransform.rotation = Quaternion.identity;
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(Quaternion.identity));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
first.LookAt(second);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(expected));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
firstTransform.rotation = Quaternion.identity;
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(Quaternion.identity));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
first.LookAt(second.transform);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(expected));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
firstTransform.rotation = Quaternion.identity;
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(Quaternion.identity));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
first.LookAt(Vector3.right);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(firstTransform.rotation, Is.EqualTo(expected));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
2023-04-07 01:21:56 +01:00
|
|
|
Object.Destroy(first);
|
|
|
|
Object.Destroy(second);
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:38:56 +01:00
|
|
|
[Test]
|
|
|
|
public void SetLayerRecursively_ShouldSetLayerRecursively()
|
2022-05-08 22:37:03 +01:00
|
|
|
{
|
|
|
|
var parent = new GameObject();
|
|
|
|
var child = new GameObject();
|
|
|
|
var grandChild = new GameObject();
|
|
|
|
|
|
|
|
child.transform.SetParent(parent.transform);
|
|
|
|
grandChild.transform.SetParent(child.transform);
|
|
|
|
|
|
|
|
int layer = LayerMask.NameToLayer("UI");
|
|
|
|
Assert.AreNotEqual(layer, parent.layer);
|
|
|
|
Assert.AreNotEqual(layer, child.layer);
|
|
|
|
Assert.AreNotEqual(layer, grandChild.layer);
|
|
|
|
|
|
|
|
parent.SetLayerRecursively(layer);
|
|
|
|
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(parent.layer, Is.EqualTo(layer));
|
|
|
|
Assert.That(child.layer, Is.EqualTo(layer));
|
|
|
|
Assert.That(grandChild.layer, Is.EqualTo(layer));
|
2022-05-08 22:37:03 +01:00
|
|
|
|
2023-04-07 01:21:56 +01:00
|
|
|
Object.Destroy(parent);
|
|
|
|
Object.Destroy(child);
|
|
|
|
Object.Destroy(grandChild);
|
2022-05-08 22:37:03 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 13:38:56 +01:00
|
|
|
[Test]
|
2023-04-07 01:34:08 +01:00
|
|
|
[SuppressMessage("ReSharper", "Unity.InefficientPropertyAccess", Justification = "False positive.")]
|
2023-04-10 13:38:56 +01:00
|
|
|
public void SetParent_ShouldSetParent()
|
2022-05-08 12:09:30 +01:00
|
|
|
{
|
|
|
|
var first = new GameObject {transform = {position = Vector3.zero, rotation = Quaternion.identity}};
|
|
|
|
var second = new GameObject {transform = {position = Vector3.right, rotation = Quaternion.identity}};
|
|
|
|
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(first.transform.parent, Is.EqualTo(null));
|
|
|
|
Assert.That(second.transform.parent, Is.EqualTo(null));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
first.SetParent(second);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(first.transform.parent, Is.EqualTo(second.transform));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
first.transform.SetParent(null!);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(first.transform.parent, Is.EqualTo(null));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
|
|
|
second.SetParent(first);
|
2023-04-05 22:51:04 +01:00
|
|
|
Assert.That(second.transform.parent, Is.EqualTo(first.transform));
|
2022-05-08 12:09:30 +01:00
|
|
|
|
2023-04-07 01:21:56 +01:00
|
|
|
Object.Destroy(first);
|
|
|
|
Object.Destroy(second);
|
2022-05-08 12:09:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|