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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961