Skip to content

Instantly share code, notes, and snippets.

@JakubNei
Last active June 29, 2016 07:32
Show Gist options
  • Save JakubNei/c6cbb6895d8d98fbe583 to your computer and use it in GitHub Desktop.
Save JakubNei/c6cbb6895d8d98fbe583 to your computer and use it in GitHub Desktop.
What needs to be changed in Unity's Transform to implement IEnumerable<Transform> plus IList<Transform> GetChildren() method
public sealed class Transform : IEnumerable, IEnumerable<Transform>
{
private sealed class Enumerator : IEnumerator, IEnumerator<Transform>
{
private Transform outer;
private int currentIndex = -1;
object IEnumerator.Current
{
get
{
return this.outer.GetChild(this.currentIndex);
}
}
Transform IEnumerator<Transform>.Current
{
get
{
return this.outer.GetChild(this.currentIndex);
}
}
internal Enumerator(Transform outer)
{
this.outer = outer;
}
public bool MoveNext()
{
int childCount = this.outer.childCount;
return ++this.currentIndex < childCount;
}
public void Reset()
{
this.currentIndex = -1;
}
public void Dispose()
{
}
}
IEnumerator<Transform> IEnumerable<Transform>.GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
public IList<Transform> GetChildren()
{
var childs = new Transform[childCount];
for (int i = 0; i < childs.Length; i++)
{
childs[i] = GetChild(i);
}
return childs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment