在ngui中,经常会用到anchors来设置位置,在一般情况下,无需用到脚本,直接在Inspector面板中设置即可,然而,当动态实例一个预制体时,它原有的Anchor属性会丢失,这时候就要用脚本来手动设置Anchor。
![这里写图片描述](https://img-blog.csdn.net/20150519111022366)
相关代码如下:
GetComponent<UISprite> ().SetAnchor (transform.parent.gameObject, 10, 10, -10, 150); //这里没有相对target的位置设置,它默认设置成上图红框中那样。下面两行代码针对左右上下其中的一个进行详细的设置。
GetComponent<UISprite> ().topAnchor.relative = 0f; //0:目标的bottom, 0.5:center, 1:top
GetComponent<UISprite> ().topAnchor.absolute = 150;
以上代码的设置结果如下图: