public void SetParent (Transform parent);
public void SetParent (Transform parent, bool worldPositionStays);

参数

parent要使用的父变换。
worldPositionStays如果为 true, 则修改相对于父级的位置、缩放和旋转, 使对象保持与之前相同的世界空间位置、旋转和缩放。

描述

设置变换的父级。

该方法与 parent 属性相同, 但它可以使 Transform 保持其本地方向而不是其全局方向。 这可通过将 worldPositionStays 参数设置为 false 来实现。 在只使用单个 Transform 参数调用 SetParent 时,worldPositionStays 参数 设置为 true。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public GameObject player;

//Invoked when a button is clicked. public void Example(Transform newParent) { //Sets "newParent" as the new parent of the player GameObject. player.transform.SetParent(newParent);

//Same as above, except this makes the player keep its local orientation rather than its global orientation. player.transform.SetParent(newParent, false); } }