mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:28:48 +00:00
Add GameObject.SetLayerRecursively (#57)
This commit is contained in:
parent
330be689b6
commit
df0257f498
@ -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()`
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -171,6 +171,44 @@ public static class GameObjectExtensions
|
||||
gameObject.transform.LookAt(target, worldUp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the new layer of this game object and its children, recursively.
|
||||
/// </summary>
|
||||
/// <param name="gameObject">The game object whose layer, and that of its children recursively, to change.</param>
|
||||
/// <param name="layer">The new layer.</param>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="gameObject" /> is <see langword="null" />.</exception>
|
||||
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<Transform>(gameObject.transform.Cast<Transform>());
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the parent of this game object.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user