using System;
using System.Collections.Generic;
using UnityEngine;
namespace UVC.Linq
{
///
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀÔ´Ï´Ù.
///
public enum TransformCloneType
{
/// ¿øº»°ú µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÕ´Ï´Ù. Add ¸Þ¼ÒµåÀÇ ±âº»°ªÀÔ´Ï´Ù.
KeepOriginal,
/// ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÕ´Ï´Ù.
FollowParent,
/// Position = ¿µº¤ÅÍ, Scale = ´ÜÀ§º¤ÅÍ, Rotation = Ç×µî ȸÀüÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù.
Origin,
/// Position/Scale/RotationÀ» ±×´ë·Î À¯ÁöÇÕ´Ï´Ù.
DoNothing
}
///
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀÔ´Ï´Ù.
///
public enum TransformMoveType
{
/// ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÕ´Ï´Ù.
FollowParent,
/// Position = ¿µº¤ÅÍ, Scale = ´ÜÀ§º¤ÅÍ, Rotation = Ç×µî ȸÀüÀ¸·Î ¼³Á¤ÇÕ´Ï´Ù.
Origin,
/// Position/Scale/RotationÀ» ±×´ë·Î À¯ÁöÇÕ´Ï´Ù.
DoNothing
}
public static partial class GameObjectExtensions
{
static UnityEngine.GameObject GetGameObject(T obj)
where T : UnityEngine.Object
{
var gameObject = obj as GameObject;
if (gameObject == null)
{
var component = obj as Component;
if (component == null)
{
return null;
}
gameObject = component.gameObject;
}
return gameObject;
}
#region Add
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ ÀÚ½ÄÀ¸·Î Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T Add(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
if (parent == null) throw new ArgumentNullException("parent");
if (childOriginal == null) throw new ArgumentNullException("childOriginal");
var child = UnityEngine.Object.Instantiate(childOriginal);
var childGameObject = GetGameObject(child);
// uGUIÀÇ °æ¿ì SetParent(parent, false)¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù
var childTransform = childGameObject.transform;
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
var rectTransform = childTransform as RectTransform;
if (rectTransform != null)
{
rectTransform.SetParent(parent.transform, worldPositionStays: false);
}
else
{
#endif
var parentTransform = parent.transform;
childTransform.parent = parentTransform;
switch (cloneType)
{
case TransformCloneType.FollowParent:
childTransform.localPosition = parentTransform.localPosition;
childTransform.localScale = parentTransform.localScale;
childTransform.localRotation = parentTransform.localRotation;
break;
case TransformCloneType.Origin:
childTransform.localPosition = Vector3.zero;
childTransform.localScale = Vector3.one;
childTransform.localRotation = Quaternion.identity;
break;
case TransformCloneType.KeepOriginal:
var co = GetGameObject(childOriginal);
var childOriginalTransform = co.transform;
childTransform.localPosition = childOriginalTransform.localPosition;
childTransform.localScale = childOriginalTransform.localScale;
childTransform.localRotation = childOriginalTransform.localRotation;
break;
case TransformCloneType.DoNothing:
default:
break;
}
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
}
#endif
if (setLayer)
{
childGameObject.layer = parent.layer;
}
if (setActive != null)
{
childGameObject.SetActive(setActive.Value);
}
if (specifiedName != null)
{
child.name = specifiedName;
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ ÀÚ½ÄÀ¸·Î Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] AddRange(this GameObject parent, IEnumerable childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
if (parent == null) throw new ArgumentNullException("parent");
if (childOriginals == null) throw new ArgumentNullException("childOriginals");
// ¹Ýº¹ ÃÖÀûÈ
{
var array = childOriginals as T[];
if (array != null)
{
var result = new T[array.Length];
for (int i = 0; i < array.Length; i++)
{
var child = Add(parent, array[i], cloneType, setActive, specifiedName, setLayer);
result[i] = child;
}
return result;
}
}
{
var iterList = childOriginals as IList;
if (iterList != null)
{
var result = new T[iterList.Count];
for (int i = 0; i < iterList.Count; i++)
{
var child = Add(parent, iterList[i], cloneType, setActive, specifiedName, setLayer);
result[i] = child;
}
return result;
}
}
{
var result = new List();
foreach (var childOriginal in childOriginals)
{
var child = Add(parent, childOriginal, cloneType, setActive, specifiedName, setLayer);
result.Add(child);
}
return result.ToArray();
}
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ Ã¹ ¹øÂ° ÀÚ½ÄÀ¸·Î Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T AddFirst(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var child = Add(parent, childOriginal, cloneType, setActive, specifiedName, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetAsFirstSibling();
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ Ã¹ ¹øÂ° ÀÚ½ÄÀ¸·Î Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] AddFirstRange(this GameObject parent, IEnumerable childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var child = AddRange(parent, childOriginals, cloneType, setActive, specifiedName, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetAsFirstSibling();
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject ¾Õ¿¡ Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T AddBeforeSelf(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("The parent root is null");
var sibilingIndex = parent.transform.GetSiblingIndex();
var child = Add(root, childOriginal, cloneType, setActive, specifiedName, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetSiblingIndex(sibilingIndex);
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject ¾Õ¿¡ Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] AddBeforeSelfRange(this GameObject parent, IEnumerable childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("The parent root is null");
var sibilingIndex = parent.transform.GetSiblingIndex();
var child = AddRange(root, childOriginals, cloneType, setActive, specifiedName, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetSiblingIndex(sibilingIndex);
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject µÚ¿¡ Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T AddAfterSelf(this GameObject parent, T childOriginal, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("The parent root is null");
var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
var child = Add(root, childOriginal, cloneType, setActive, specifiedName, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetSiblingIndex(sibilingIndex);
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject µÚ¿¡ Ãß°¡ÇÕ´Ï´Ù. ´ë»óÀÌ º¹Á¦µË´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// º¹Á¦ÇÒ ´ë»óÀÔ´Ï´Ù.
/// º¹Á¦µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ À̸§À» ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] AddAfterSelfRange(this GameObject parent, IEnumerable childOriginals, TransformCloneType cloneType = TransformCloneType.KeepOriginal, bool? setActive = null, string specifiedName = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("The parent root is null");
var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
var child = AddRange(root, childOriginals, cloneType, setActive, specifiedName, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetSiblingIndex(sibilingIndex);
}
return child;
}
#endregion
#region Move
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ ÀÚ½ÄÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T MoveToLast(this GameObject parent, T child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
if (parent == null) throw new ArgumentNullException("parent");
if (child == null) throw new ArgumentNullException("child");
var childGameObject = GetGameObject(child);
if (child == null) return child;
// uGUIÀÇ °æ¿ì SetParent(parent, false)¸¦ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù
var childTransform = childGameObject.transform;
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
var rectTransform = childTransform as RectTransform;
if (rectTransform != null)
{
rectTransform.SetParent(parent.transform, worldPositionStays: false);
}
else
{
#endif
var parentTransform = parent.transform;
childTransform.parent = parentTransform;
switch (moveType)
{
case TransformMoveType.FollowParent:
childTransform.localPosition = parentTransform.localPosition;
childTransform.localScale = parentTransform.localScale;
childTransform.localRotation = parentTransform.localRotation;
break;
case TransformMoveType.Origin:
childTransform.localPosition = Vector3.zero;
childTransform.localScale = Vector3.one;
childTransform.localRotation = Quaternion.identity;
break;
case TransformMoveType.DoNothing:
default:
break;
}
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
}
#endif
if (setLayer)
{
childGameObject.layer = parent.layer;
}
if (setActive != null)
{
childGameObject.SetActive(setActive.Value);
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ ÀÚ½ÄÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] MoveToLastRange(this GameObject parent, IEnumerable childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
if (parent == null) throw new ArgumentNullException("parent");
if (childs == null) throw new ArgumentNullException("childs");
// ¹Ýº¹ ÃÖÀûÈ
{
var array = childs as T[];
if (array != null)
{
var result = new T[array.Length];
for (int i = 0; i < array.Length; i++)
{
var child = MoveToLast(parent, array[i], moveType, setActive, setLayer);
result[i] = child;
}
return result;
}
}
{
var iterList = childs as IList;
if (iterList != null)
{
var result = new T[iterList.Count];
for (int i = 0; i < iterList.Count; i++)
{
var child = MoveToLast(parent, iterList[i], moveType, setActive, setLayer);
result[i] = child;
}
return result;
}
}
{
var result = new List();
foreach (var childOriginal in childs)
{
var child = MoveToLast(parent, childOriginal, moveType, setActive, setLayer);
result.Add(child);
}
return result.ToArray();
}
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ Ã¹ ¹øÂ° ÀÚ½ÄÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T MoveToFirst(this GameObject parent, T child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
MoveToLast(parent, child, moveType, setActive, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetAsFirstSibling();
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObjectÀÇ Ã¹ ¹øÂ° ÀÚ½ÄÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] MoveToFirstRange(this GameObject parent, IEnumerable childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
var child = MoveToLastRange(parent, childs, moveType, setActive, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetAsFirstSibling();
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject ¾ÕÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T MoveToBeforeSelf(this GameObject parent, T child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("ºÎ¸ð ·çÆ®°¡ nullÀÔ´Ï´Ù");
var sibilingIndex = parent.transform.GetSiblingIndex();
MoveToLast(root, child, moveType, setActive, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetSiblingIndex(sibilingIndex);
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject ¾ÕÀ¸·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] MoveToBeforeSelfRange(this GameObject parent, IEnumerable childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("ºÎ¸ð ·çÆ®°¡ nullÀÔ´Ï´Ù");
var sibilingIndex = parent.transform.GetSiblingIndex();
var child = MoveToLastRange(root, childs, moveType, setActive, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetSiblingIndex(sibilingIndex);
}
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject µÚ·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T MoveToAfterSelf(this GameObject parent, T child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("ºÎ¸ð ·çÆ®°¡ nullÀÔ´Ï´Ù");
var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
MoveToLast(root, child, moveType, setActive, setLayer);
var go = GetGameObject(child);
if (go == null) return child;
go.transform.SetSiblingIndex(sibilingIndex);
return child;
}
///
/// GameObject/Component¸¦ ÀÌ GameObject µÚ·Î À̵¿ÇÕ´Ï´Ù.
///
/// ºÎ¸ð GameObjectÀÔ´Ï´Ù.
/// ´ë»óÀÔ´Ï´Ù.
/// À̵¿µÈ ÀÚ½Ä GameObjectÀÇ localPosition/Scale/Rotation ¼³Á¤ ŸÀÔÀ» ¼±ÅÃÇÕ´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ È°¼º/ºñȰ¼º »óŸ¦ ¼³Á¤ÇÕ´Ï´Ù. nullÀ̸é ÁöÁ¤µÈ °ªÀ» ¼³Á¤ÇÏÁö ¾Ê½À´Ï´Ù.
/// ÀÚ½Ä GameObjectÀÇ ·¹À̾ ºÎ¸ð¿Í µ¿ÀÏÇÏ°Ô ¼³Á¤ÇÒÁö ¿©ºÎÀÔ´Ï´Ù.
public static T[] MoveToAfterSelfRange(this GameObject parent, IEnumerable childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null, bool setLayer = false)
where T : UnityEngine.Object
{
var root = parent.Parent();
if (root == null) throw new InvalidOperationException("ºÎ¸ð ·çÆ®°¡ nullÀÔ´Ï´Ù");
var sibilingIndex = parent.transform.GetSiblingIndex() + 1;
var child = MoveToLastRange(root, childs, moveType, setActive, setLayer);
for (int i = child.Length - 1; i >= 0; i--)
{
var go = GetGameObject(child[i]);
if (go == null) continue;
go.transform.SetSiblingIndex(sibilingIndex);
}
return child;
}
#endregion
/// ÀÌ GameObject¸¦ ¾ÈÀüÇϰÔ(null üũ) ÆÄ±«ÇÕ´Ï´Ù.
/// ¿¡µðÅÍ ¸ðµå¿¡¼´Â true·Î ¼³Á¤Çϰųª !Application.isPlayingÀ» Àü´ÞÇϼ¼¿ä.
/// ºÎ¸ð¸¦ null·Î ¼³Á¤ÇÕ´Ï´Ù.
public static void Destroy(this GameObject self, bool useDestroyImmediate = false, bool detachParent = false)
{
if (self == null) return;
if (detachParent)
{
#if !(UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5)
self.transform.SetParent(null);
#else
self.transform.parent = null;
#endif
}
if (useDestroyImmediate)
{
GameObject.DestroyImmediate(self);
}
else
{
GameObject.Destroy(self);
}
}
}
}