一:前言
两者都能够去设置一个对象为另一个对象的子物体
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.walajiao.com/ 游戏加盟