Unity3D C#实例化后面板的定位

Unity3D C#实例化后面板的定位,c#,unity3d,transform,panel,rect,C#,Unity3d,Transform,Panel,Rect,我正在实例化一个GO,其中有一个面板组件(RectTransform)作为场景中画布的子级: _heroSelectUI = (GameObject)Instantiate (_heroSelectUIPrefab, GameObject.Find ("Canvas").GetComponent<Transform>()); _heroSelectUI=(GameObject)实例化(_heroSelectUIPrefab,GameObject.Find(“Canvas”).Get

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

_heroSelectUI = (GameObject)Instantiate (_heroSelectUIPrefab, GameObject.Find ("Canvas").GetComponent<Transform>());
_heroSelectUI=(GameObject)实例化(_heroSelectUIPrefab,GameObject.Find(“Canvas”).GetComponent());
创建时,它将获得以下值:

“Left”、“Top”、“Right”和“Bottom”会得到一些不需要的值(可能是由于现有画布似乎具有相同的值)

面板预设值为0,实例化后如何将其设置回0?我找不到正确的RectTransform变量。

根据

对于拉伸矩形变换,使用offsetMin和offsetMax属性设置位置可能更简单。该属性指定相对于左下定位点的矩形左下角的角。该属性指定相对于右上定位点的矩形右上角的角

尝试以下操作:

RectTransform rt = _heroSelectUI.GetComponent<RectTransform>();
rt.offsetMin = rt.offsetMax = Vector2.zero ;
rectcransform rt=\u heroSelectUI.GetComponent();
rt.offsetMin=rt.offsetMax=Vector2.0;

否则,根据文档,您也可以在设置父级时尝试执行以下操作:

UI元素的预置是使用实例化方法按常规实例化的。在设置实例化UI元素的父元素时,建议使用Transform.SetParent方法将worldPositionStays参数设置为false

\u heroSelectUI=(GameObject)实例化(\u heroSelectUIPrefab,GameObject.Find(“Canvas”).GetComponent(),false);
或:

\u heroSelectUI=(游戏对象)实例化(\u heroSelectUI预制);
_heroSelectUI.GetComponent().SetParent(GameObject.Find(“画布”).GetComponent(),false);

哦,是的,我的按钮设置为false,但我没有想到也要对面板执行此操作!它把worldPositionStays设置为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 ) ;