Skip to content

Instantly share code, notes, and snippets.

@dmitry1100
Last active March 12, 2024 13:09

Revisions

  1. dmitry1100 revised this gist Nov 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion TransformAllChildren.cs
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ public static IEnumerable<Transform> AllChildren ( this Transform t )

    private class DeepEnumerator : IEnumerator<Transform>, IEnumerable<Transform>
    {
    private Transform _root;
    private readonly Transform _root;
    private Transform _current;
    private Stack<Transform> _stack;

  2. dmitry1100 revised this gist Nov 14, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion TransformAllChildren.cs
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    using System.Collections.Generic;
    using UnityEngine;


    namespace Fiftytwo
    {
    public static class TransformAllChildren
  3. dmitry1100 revised this gist Nov 14, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions TransformAllChildrenTest.cs
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ namespace Fiftytwo
    {
    public class TransformAllChildrenTest : MonoBehaviour
    {
    // Will be called just after component attached to GameObject
    private void Reset ()
    {
    foreach ( var child in transform.AllChildren() )
  4. dmitry1100 renamed this gist Nov 14, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. dmitry1100 renamed this gist Nov 14, 2019. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    namespace Fiftytwo
    {
    public class TestTransformAllChildren : MonoBehaviour
    public class TransformAllChildrenTest : MonoBehaviour
    {
    private void Reset ()
    {
  6. dmitry1100 created this gist Nov 14, 2019.
    15 changes: 15 additions & 0 deletions TestTransformAllChildren.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    using UnityEngine;

    namespace Fiftytwo
    {
    public class TestTransformAllChildren : MonoBehaviour
    {
    private void Reset ()
    {
    foreach ( var child in transform.AllChildren() )
    {
    Debug.Log( child.name );
    }
    }
    }
    }
    66 changes: 66 additions & 0 deletions TransformAllChildren
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    namespace Fiftytwo
    {
    public static class TransformAllChildren
    {
    public static IEnumerable<Transform> AllChildren ( this Transform t )
    {
    return new DeepEnumerator( t );
    }

    private class DeepEnumerator : IEnumerator<Transform>, IEnumerable<Transform>
    {
    private Transform _root;
    private Transform _current;
    private Stack<Transform> _stack;

    internal DeepEnumerator ( Transform root )
    {
    _root = root;
    _current = root;
    _stack = new Stack<Transform>();
    }

    public Transform Current { get { return _current; } }

    object IEnumerator.Current { get { return _current; } }

    void IDisposable.Dispose () { }

    public bool MoveNext ()
    {
    for( var i = _current.childCount; --i >= 0; )
    _stack.Push( _current.GetChild( i ) );

    if( _stack.Count == 0 )
    return false;

    _current = _stack.Pop();

    return true;
    }

    public void Reset ()
    {
    _current = _root;
    _stack.Clear();
    }

    public IEnumerator<Transform> GetEnumerator ()
    {
    Reset();
    return this;
    }

    IEnumerator IEnumerable.GetEnumerator ()
    {
    return GetEnumerator();
    }
    }
    }
    }