From df0257f4981a15b3bcb15262bffa0f0c4096d399 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 8 May 2022 22:37:03 +0100 Subject: [PATCH] Add GameObject.SetLayerRecursively (#57) --- CHANGELOG.md | 1 + .../Assets/Tests/GameObjectTests.cs | 24 ++++++++++++ X10D.Unity/src/GameObjectExtensions.cs | 38 +++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5656b63..0b5e657 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - X10D.Unity: Added `GameObject.LookAt(GameObject[, Vector3])` - X10D.Unity: Added `GameObject.LookAt(Transform[, Vector3])` - X10D.Unity: Added `GameObject.LookAt(Vector3[, Vector3])` +- X10D.Unity: Added `GameObject.SetLayerRecursively(int)` - X10D.Unity: Added `GameObject.SetParent(GameObject[, bool])` - X10D.Unity: Added `GameObject.SetParent(Transform[, bool])` - X10D.Unity: Added `Random.NextColorArgb()` diff --git a/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs b/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs index 9e4636c..79e649e 100644 --- a/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs @@ -61,6 +61,30 @@ namespace X10D.Unity.Tests yield break; } + [UnityTest] + public IEnumerator SetLayerRecursively_ShouldSetLayerRecursively() + { + 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); + + Assert.AreEqual(layer, parent.layer); + Assert.AreEqual(layer, child.layer); + Assert.AreEqual(layer, grandChild.layer); + + yield break; + } + [UnityTest] public IEnumerator SetParent_ShouldSetParent() { diff --git a/X10D.Unity/src/GameObjectExtensions.cs b/X10D.Unity/src/GameObjectExtensions.cs index 095ab7b..8d2b9d8 100644 --- a/X10D.Unity/src/GameObjectExtensions.cs +++ b/X10D.Unity/src/GameObjectExtensions.cs @@ -171,6 +171,44 @@ public static class GameObjectExtensions gameObject.transform.LookAt(target, worldUp); } + /// + /// Sets the new layer of this game object and its children, recursively. + /// + /// The game object whose layer, and that of its children recursively, to change. + /// The new layer. + /// is . + public static void SetLayerRecursively(this GameObject gameObject, int layer) + { + if (gameObject == null) + { + throw new ArgumentNullException(nameof(gameObject)); + } + + gameObject.layer = layer; + + if (gameObject.transform.childCount == 0) + { + return; + } + + var children = new Stack(gameObject.transform.Cast()); + while (children.Count > 0) + { + Transform child = children.Pop(); + child.gameObject.layer = layer; + + if (child.childCount <= 0) + { + continue; + } + + foreach (Transform grandChild in child) + { + children.Push(grandChild); + } + } + } + /// /// Sets the parent of this game object. ///