关于unity:Unity中的SetParent和parent

10次阅读

共计 524 个字符,预计需要花费 2 分钟才能阅读完成。

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

正文完
 0