关于unity:Unity中的SetParent和parent

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理