微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

实例化后Unity3D C#面板定位

我正在实例化一个GO,它有一个面板组件(RectTransform)作为场景中存在的画布的子代:

_heroSelectUI = (GameObject)Instantiate (_heroSelectUIPrefab, GameObject.Find ("Canvas").GetComponent<Transform>());

创建它时,它获得以下值:

Panel Values

“左”,“上”,“右”和“下”得到一些不需要的值(可能是由于现有的画布似乎具有相同的值).

面板预制值为0,如何在实例化后将它们设置为0?我找不到RectTransform的正确变量.

解决方法:

根据documentation

For a stretching Rect Transform, it can be simpler to set the position using the offsetMin and offsetMax properties. The 07001 property specifies the corner of the lower left corner of the rect relative to the lower left anchor. The 07002 property specifies the corner of the upper right corner of the rect relative to the upper right anchor.

尝试做如下:

RectTransform rt = _heroSelectUI.GetComponent<RectTransform>();
rt.offsetMin = rt.offsetMax = Vector2.zero ;

另外,根据文档,您可以在设置父级时尝试执行此操作:

Prefabs of UI elements are instantiated as normal using the Instantiate method. When setting the parent of the instantiated UI element, it’s recommended to do it using the Transform.SetParent method with the worldPositionStays parameter set to false.

_heroSelectUI = (GameObject)Instantiate (_heroSelectUIPrefab, GameObject.Find ("Canvas").GetComponent<RectTransform>(), false);

要么 :

_heroSelectUI = (GameObject)Instantiate (_heroSelectUIPrefab );
_heroSelectUI.GetComponent<Transform>().SetParent( GameObject.Find ("Canvas").GetComponent<RectTransform>(), false ) ;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐