private static void SetLayerRecursively(GameObject go, int layer) { go.layer = layer; Transform transform = go.transform; for (int index = 0; index < transform.childCount; ++index) { GameObjectUtility.SetLayerRecursively(transform.GetChild(index).gameObject, layer); } }
public static void SetParentAndAlign(GameObject child, GameObject parent) { if (!(parent == null)) { child.transform.SetParent(parent.transform, false); RectTransform rectTransform = child.transform as RectTransform; if (rectTransform) { rectTransform.anchoredPosition = Vector2.zero; Vector3 localPosition = rectTransform.localPosition; localPosition.z = 0f; rectTransform.localPosition = localPosition; } else { child.transform.localPosition = Vector3.zero; } child.transform.localRotation = Quaternion.identity; child.transform.localScale = Vector3.one; GameObjectUtility.SetLayerRecursively(child, parent.layer); } }
/// <summary> /// <para>Sets the parent and gives the child the same layer and position.</para> /// </summary> /// <param name="child">The GameObject that should have a new parent set.</param> /// <param name="parent">The GameObject that the child should get as a parent and have position and layer copied from. If null, this function does nothing.</param> public static void SetParentAndAlign(GameObject child, GameObject parent) { if ((UnityEngine.Object)parent == (UnityEngine.Object)null) { return; } child.transform.SetParent(parent.transform, false); RectTransform transform = child.transform as RectTransform; if ((bool)((UnityEngine.Object)transform)) { transform.anchoredPosition = Vector2.zero; Vector3 localPosition = transform.localPosition; localPosition.z = 0.0f; transform.localPosition = localPosition; } else { child.transform.localPosition = Vector3.zero; } child.transform.localRotation = Quaternion.identity; child.transform.localScale = Vector3.one; GameObjectUtility.SetLayerRecursively(child, parent.layer); }