Skip to content

Commit

Permalink
if RectTransform, use SetParent(parent, false) for for uGUI
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Mar 10, 2015
1 parent 823989d commit 0f76474
Showing 1 changed file with 68 additions and 41 deletions.
109 changes: 68 additions & 41 deletions Assets/LINQtoGameObject/Scripts/GameObjectExtensions.Operate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,45 @@ public static GameObject Add(this GameObject parent, GameObject childOriginal, T
if (childOriginal == null) throw new ArgumentNullException("childOriginal");

var child = UnityEngine.Object.Instantiate(childOriginal) as GameObject;
// 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 (cloneType)
// 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
{
case TransformCloneType.FollowParent:
child.transform.localPosition = parent.transform.localPosition;
child.transform.localScale = parent.transform.localScale;
child.transform.localRotation = parent.transform.localRotation;
break;
case TransformCloneType.Origin:
child.transform.localPosition = Vector3.zero;
child.transform.localScale = Vector3.one;
child.transform.localRotation = Quaternion.identity;
break;
case TransformCloneType.KeepOriginal:
child.transform.localPosition = childOriginal.transform.localPosition;
child.transform.localScale = childOriginal.transform.localScale;
child.transform.localRotation = childOriginal.transform.localRotation;
break;
case TransformCloneType.DoNothing:
default:
break;
#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 childOriginalTransform = childOriginal.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
child.layer = parent.layer;

if (setActive != null)
{
Expand Down Expand Up @@ -245,26 +259,39 @@ public static GameObject MoveToLast(this GameObject parent, GameObject child, Tr
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)
// 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
{
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;
#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)
{
Expand Down

0 comments on commit 0f76474

Please sign in to comment.