您的位置:首页 > 移动开发 > Unity3D

unity, 动态创建节点时一定要先指定父节点再设置transform

2015-12-23 16:17 585 查看
如下,设置transform的代码必须放在node.transform.parent=transform之后。否则设置将不生效。

     GameObject node = new GameObject ();

node.name=”myNode”;

node.transform.parent = transform;

node.transform.localScale = new Vector3 (1, 1, 1);

node.transform.localPosition = new Vector3 (0, 0, 0);

node.transform.localRotation = Quaternion.identity;

更新(2015-8-7):

细看了一下,控制台有个warning:

Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.

所以,应该写成下面这样才是标准的:

     GameObject node = new GameObject ();

node.name=”myNode”;

node.transform.SetParent(transform,false);

node.transform.localScale = new Vector3 (1, 1, 1);

node.transform.localPosition = new Vector3 (0, 0, 0);

node.transform.localRotation = Quaternion.identity;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: