/// <summary> /// <para>Move the GameObject/Component as the first children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T MoveToFirst <T>(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); }
/// <summary> /// <para>Move the GameObject/Component after this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T MoveToAfterSelf <T>(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("The parent root is 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); }
/// <summary> /// <para>Move the GameObject/Component as the first children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T[] MoveToFirstRange <T>(this GameObject parent, IEnumerable <T> 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); }
/// <summary> /// <para>Move the GameObject/Component as children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of moved child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T[] MoveToLastRange <T>(this GameObject parent, IEnumerable <T> 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"); } // iteration optimize { 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 <T>; 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 <T>(); foreach (var childOriginal in childs) { var child = MoveToLast(parent, childOriginal, moveType, setActive, setLayer); result.Add(child); } return(result.ToArray()); } }
/// <summary> /// <para>Move the GameObject/Component as children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T MoveToLast <T>(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); } // for uGUI, should use 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); }
public static List <GameObject> MoveToAfterSelf(this GameObject parent, IEnumerable <GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { var root = parent.Parent(); if (root == null) { throw new InvalidOperationException("The parent root is null"); } var sibilingIndex = parent.transform.GetSiblingIndex() + 1; var child = MoveToLast(root, childs, moveType, setActive); for (int i = child.Count - 1; i >= 0; i--) { child[i].transform.SetSiblingIndex(sibilingIndex); } return(child); }
public static GameObject MoveToAfterSelf(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { var root = parent.Parent(); if (root == null) { throw new InvalidOperationException("The parent root is null"); } var sibilingIndex = parent.transform.GetSiblingIndex() + 1; MoveToLast(root, child, moveType, setActive); child.transform.SetSiblingIndex(sibilingIndex); return(child); }
/// <summary> /// <para>Move the GameObject before this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static GameObject MoveToBeforeSelf(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { var root = parent.Parent(); if (root == null) throw new InvalidOperationException("The parent root is null"); var sibilingIndex = parent.transform.GetSiblingIndex(); MoveToLast(root, child, moveType, setActive); child.transform.SetSiblingIndex(sibilingIndex); return child; }
public static GameObject MoveToFirst(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { MoveToLast(parent, child, moveType, setActive); child.transform.SetAsFirstSibling(); return(child); }
public static List <GameObject> MoveToLast(this GameObject parent, IEnumerable <GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { if (parent == null) { throw new ArgumentNullException("parent"); } if (childs == null) { throw new ArgumentNullException("childs"); } var list = new List <GameObject>(); foreach (var childOriginal in childs) { var child = MoveToLast(parent, childOriginal, moveType); list.Add(child); } return(list); }
/// <summary> /// <para>Move the GameObject as children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of moved child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static List<GameObject> MoveToLast(this GameObject parent, IEnumerable<GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { if (parent == null) throw new ArgumentNullException("parent"); if (childs == null) throw new ArgumentNullException("childs"); var list = new List<GameObject>(); foreach (var childOriginal in childs) { var child = MoveToLast(parent, childOriginal, moveType); list.Add(child); } return list; }
/// <summary> /// <para>Move the GameObject as children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static GameObject MoveToLast(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { if (parent == null) throw new ArgumentNullException("parent"); if (child == null) throw new ArgumentNullException("child"); // for uGUI, should use SetParent(parent, false) var childTransform = child.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 child.layer = parent.layer; if (setActive != null) { child.SetActive(setActive.Value); } return child; }
/// <summary> /// <para>Move the GameObject as the first children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static List<GameObject> MoveToFirst(this GameObject parent, IEnumerable<GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { var child = MoveToLast(parent, childs, moveType, setActive); for (int i = child.Count - 1; i >= 0; i--) { child[i].transform.SetAsFirstSibling(); } return child; }
/// <summary> /// <para>Move the GameObject as the first children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static GameObject MoveToFirst(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { MoveToLast(parent, child, moveType, setActive); child.transform.SetAsFirstSibling(); return child; }
/// <summary> /// <para>Move the GameObject before GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static List<GameObject> MoveToBeforeSelf(this GameObject parent, IEnumerable<GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool? setActive = null) { var root = parent.Parent(); if (root == null) throw new InvalidOperationException("The parent root is null"); var sibilingIndex = parent.transform.GetSiblingIndex(); var child = MoveToLast(root, childs, moveType, setActive); for (int i = child.Count - 1; i >= 0; i--) { child[i].transform.SetSiblingIndex(sibilingIndex); } return child; }
public static List <GameObject> MoveToFirst(this GameObject parent, IEnumerable <GameObject> childs, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { var child = MoveToLast(parent, childs, moveType, setActive); for (int i = child.Count - 1; i >= 0; i--) { child[i].transform.SetAsFirstSibling(); } return(child); }
/// <summary> /// <para>Move the GameObject/Component after this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="childs">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> /// <param name="setLayer">Set layer of child GameObject same with parent.</param> public static T[] MoveToAfterSelfRange <T>(this GameObject parent, IEnumerable <T> 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("The parent root is 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); }
/// <summary> /// <para>Move the GameObject as children of this GameObject.</para> /// </summary> /// <param name="parent">Parent GameObject.</param> /// <param name="child">Target.</param> /// <param name="moveType">Choose set type of cloned child GameObject's localPosition/Scale/Rotation.</param> /// <param name="setActive">Set activates/deactivates child GameObject. If null, doesn't set specified value.</param> public static GameObject MoveToLast(this GameObject parent, GameObject child, TransformMoveType moveType = TransformMoveType.DoNothing, bool?setActive = null) { if (parent == null) { throw new ArgumentNullException("parent"); } if (child == null) { throw new ArgumentNullException("child"); } // Unity 4.6, we can use SetParent but before that can't therefore use set localPosition/Rotation/Scale. child.transform.parent = parent.transform; child.layer = parent.layer; switch (moveType) { case TransformMoveType.FollowParent: child.transform.localPosition = parent.transform.localPosition; child.transform.localScale = parent.transform.localScale; child.transform.localRotation = parent.transform.localRotation; break; case TransformMoveType.Origin: child.transform.localPosition = Vector3.zero; child.transform.localScale = Vector3.one; child.transform.localRotation = Quaternion.identity; break; case TransformMoveType.DoNothing: default: break; } if (setActive != null) { child.SetActive(setActive.Value); } return(child); }