using System;
using System.Collections.Generic;
using UnityEngine;
namespace UVC.Linq
{
public static partial class GameObjectExtensions
{
/// ¼Ò½º Ä÷º¼Ç¿¡ ÀÖ´Â ¸ðµç GameObjectÀÇ Á¶»óÀ» Æ÷ÇÔÇÏ´Â GameObject Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
public static IEnumerable Ancestors(this IEnumerable source)
{
foreach (var item in source)
{
var e = item.Ancestors().GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼ÇÀÇ ¸ðµç GameObject¿Í À̵éÀÇ Á¶»ó GameObject¸¦ Æ÷ÇÔÇÏ´Â Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
public static IEnumerable AncestorsAndSelf(this IEnumerable source)
{
foreach (var item in source)
{
var e = item.AncestorsAndSelf().GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼Ç¿¡ ÀÖ´Â ¸ðµç GameObjectÀÇ ÀÚ¼Õ GameObject¸¦ Æ÷ÇÔÇÏ´Â Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
/// ÀÚ½Ä °´Ã¼¸¦ ¼øÈ¸ÇÒÁö °áÁ¤ÇÏ´Â Á¶°Ç ÇÔ¼öÀÔ´Ï´Ù. nullÀÌ¸é ¸ðµç ÀÚ½ÄÀ» ¼øÈ¸ÇÕ´Ï´Ù.
public static IEnumerable Descendants(this IEnumerable source, Func descendIntoChildren = null)
{
foreach (var item in source)
{
var e = item.Descendants(descendIntoChildren).GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼ÇÀÇ ¸ðµç GameObject¿Í À̵éÀÇ ÀÚ¼Õ GameObject¸¦ Æ÷ÇÔÇÏ´Â Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
public static IEnumerable DescendantsAndSelf(this IEnumerable source, Func descendIntoChildren = null)
{
foreach (var item in source)
{
var e = item.DescendantsAndSelf(descendIntoChildren).GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼Ç¿¡ ÀÖ´Â ¸ðµç GameObjectÀÇ ÀÚ½Ä GameObject¸¦ Æ÷ÇÔÇÏ´Â Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
public static IEnumerable Children(this IEnumerable source)
{
foreach (var item in source)
{
var e = item.Children().GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼ÇÀÇ ¸ðµç GameObject¿Í À̵éÀÇ ÀÚ½Ä GameObject¸¦ Æ÷ÇÔÇÏ´Â Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
public static IEnumerable ChildrenAndSelf(this IEnumerable source)
{
foreach (var item in source)
{
var e = item.ChildrenAndSelf().GetEnumerator();
while (e.MoveNext())
{
yield return e.Current;
}
}
}
/// ¼Ò½º Ä÷º¼Ç¿¡ ÀÖ´Â ¸ðµç GameObject¸¦ ¾ÈÀüÇϰÔ(null üũ Æ÷ÇÔ) ÆÄ±«ÇÕ´Ï´Ù.
/// ¿¡µðÅÍ ¸ðµå¿¡¼´Â true·Î ¼³Á¤Çϰųª !Application.isPlayingÀ» Àü´ÞÇϼ¼¿ä.
/// ºÎ¸ð¸¦ null·Î ¼³Á¤ÇÕ´Ï´Ù.
public static void Destroy(this IEnumerable source, bool useDestroyImmediate = false, bool detachParent = false)
{
if (detachParent)
{
var l = new List(source); // avoid halloween problem
var e = l.GetEnumerator(); // get struct enumerator for avoid unity's compiler bug(avoid boxing)
while (e.MoveNext())
{
e.Current.Destroy(useDestroyImmediate, true);
}
}
else
{
foreach (var item in source)
{
item.Destroy(useDestroyImmediate, false); // doesn't detach.
}
}
}
/// ¼Ò½º Ä÷º¼Ç¿¡¼ ÁöÁ¤µÈ ÄÄÆ÷³ÍÆ® ŸÀÔÀÇ Ä÷º¼ÇÀ» ¹ÝȯÇÕ´Ï´Ù.
/// °Ë»öÇÒ ÄÄÆ÷³ÍÆ®ÀÇ Å¸ÀÔÀÔ´Ï´Ù.
/// ¿¡µðÅÍ ¸ðµå¿¡¼´Â ¸Þ¸ð¸® ÇÒ´çÀ» ÃÖ¼ÒÈÇϱâ À§ÇÑ Ä³½Ã ¸ÞÄ¿´ÏÁòÀ» »ç¿ëÇÕ´Ï´Ù.
public static IEnumerable OfComponent(this IEnumerable source)
where T : UnityEngine.Component
{
foreach (var item in source)
{
#if UNITY_EDITOR
var cache = ComponentCache.Instance;
item.GetComponents(cache);
if (cache.Count != 0)
{
var component = cache[0];
cache.Clear();
yield return component;
}
#else
var component = item.GetComponent();
if (component != null)
{
yield return component;
}
#endif
}
}
#if UNITY_EDITOR
class ComponentCache
{
public static readonly List Instance = new List(); // for no allocate on UNITY_EDITOR
}
#endif
/// ¿ä¼Ò¸¦ ¹öÆÛ¿¡ ÀúÀåÇϰí, Å©±â¸¦ ¹ÝȯÇÕ´Ï´Ù. ¹è¿Àº ÀÚµ¿À¸·Î È®ÀåµË´Ï´Ù.
/// ÀúÀåÇÒ ¿ä¼ÒµéÀÇ ¼Ò½º Ä÷º¼ÇÀÔ´Ï´Ù.
/// ¿ä¼Ò¸¦ ÀúÀåÇÒ ¹è¿ ÂüÁ¶ÀÔ´Ï´Ù. Çʿ信 µû¶ó Å©±â°¡ Á¶Á¤µË´Ï´Ù.
/// ¹è¿¿¡ ÀúÀåµÈ ¿ä¼ÒÀÇ ¼ö¸¦ ¹ÝȯÇÕ´Ï´Ù.
public static int ToArrayNonAlloc(this IEnumerable source, ref T[] array)
{
var index = 0;
foreach (var item in source)
{
if (array.Length == index)
{
var newSize = (index == 0) ? 4 : index * 2;
Array.Resize(ref array, newSize);
}
array[index++] = item;
}
return index;
}
}
}