一:前言
两者都可以去设置一个对象为另一个对象的子物体
SetParent方法中有两个参数,第二个参数表示是否使用世界坐标,默认为true
例如下面这段代码,如果SetParent的第二个参数为true,child的位置将会是(-1,-1,-1)
using UnityEngine; public class Test : MonoBehaviour { void Start() { GameObject parent = new GameObject("parent"); parent.transform.position = Vector3.one; GameObject child = new GameObject("child"); child.transform.SetParent(parent.transform); } }
二:总结
——如果父物体的位置、旋转、缩放不是默认值,则需要使用SetParent设置父物体并设置第二个参数为false
——在UI中,SetParent比.parent的效率高很多,这是RectTransform导致的,所以建议在UI中设置父子关系使用SetParent并使用第二个参数为false
图片来源:http://www.lyouxi.com/ 手游