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

如下,设置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;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值