1
NGUI 3 Support / Advanced Anchors at runtime
« on: June 18, 2014, 04:19:07 PM »
I haven't been able to find anything on setting advanced anchors at runtime. SetAnchor seems to only setup a unified anchor. I added an overload to fix this. If it is the correct way, could we get it added to the next update?
/// <summary>
/// Anchor this rectangle to the specified transform using an advanced anchor system.
/// </summary>
public void SetAnchor (GameObject leftGO, int left, GameObject bottomGO, int bottom, GameObject rightGO, int right, GameObject topGO, int top)
{
Transform tl = (leftGO != null) ? leftGO.transform : null;
Transform tr = (rightGO != null) ? rightGO.transform : null;
Transform tt = (topGO != null) ? topGO.transform : null;
Transform tb = (bottomGO != null) ? bottomGO.transform : null;
leftAnchor.target = tl;
rightAnchor.target = tr;
topAnchor.target = tt;
bottomAnchor.target = tb;
leftAnchor.relative = 0f;
rightAnchor.relative = 1f;
bottomAnchor.relative = 0f;
topAnchor.relative = 1f;
leftAnchor.absolute = left;
rightAnchor.absolute = right;
bottomAnchor.absolute = bottom;
topAnchor.absolute = top;
ResetAnchors();
UpdateAnchors();
}
/// <summary>
/// Anchor this rectangle to the specified transform using an advanced anchor system.
/// </summary>
public void SetAnchor (GameObject leftGO, int left, GameObject bottomGO, int bottom, GameObject rightGO, int right, GameObject topGO, int top)
{
Transform tl = (leftGO != null) ? leftGO.transform : null;
Transform tr = (rightGO != null) ? rightGO.transform : null;
Transform tt = (topGO != null) ? topGO.transform : null;
Transform tb = (bottomGO != null) ? bottomGO.transform : null;
leftAnchor.target = tl;
rightAnchor.target = tr;
topAnchor.target = tt;
bottomAnchor.target = tb;
leftAnchor.relative = 0f;
rightAnchor.relative = 1f;
bottomAnchor.relative = 0f;
topAnchor.relative = 1f;
leftAnchor.absolute = left;
rightAnchor.absolute = right;
bottomAnchor.absolute = bottom;
topAnchor.absolute = top;
ResetAnchors();
UpdateAnchors();
}