首页 > 代码库 > 设置父物体方法(包括层级)
设置父物体方法(包括层级)
/// <summary>
/// 设置父物体
/// </summary>
/// <param name="parent"></param>
/// <param name="child"></param>
/// <param name="worldPositionStay"></param>
/// <param name="changeLayer"></param>
public static void SetParent(GameObject parent, GameObject child, bool worldPositionStay, bool changeLayer)
{
if (child == null) return;
if (parent == null)
{
child.transform.SetParent(null, worldPositionStay);
}
else
{
child.transform.SetParent(parent.transform, worldPositionStay);
if (changeLayer) SetLayer(parent.layer, child.transform, true);
}
}
/// <summary>
/// 设置layer
/// </summary>
/// <param name="layer"></param>
/// <param name="t"></param>
/// <param name="recursive"></param>
public static void SetLayer(int layer, Transform t, bool recursive)
{
if (t != null)
{
t.gameObject.layer = layer;
if (recursive)
{
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
SetLayer(layer, t.GetChild(i), recursive);
}
}
}
}
设置父物体方法(包括层级)