static public void UpdateVerticalAnchor (UIRect r, UIRect.AnchorPoint anchor, bool resetRelative)
{
// Update the target
if (anchor.target == null) return;
// Update the rect
anchor.rect = anchor.target.GetComponent<UIRect>();
// Continue only if we have a parent to work with
Transform parent = r.cachedTransform.parent;
if (parent == null) return;
bool inverted = (anchor == r.topAnchor);
int i0 = inverted ? 1 : 0;
int i1 = inverted ? 2 : 3;
// Calculate the bottom side
Vector3[] myCorners = r.worldCorners;
Vector3 localPos = parent.InverseTransformPoint(Vector3.Lerp(myCorners[i0], myCorners[i1], 0.5f));
if (anchor.rect != null)
{
// Anchored to a rectangle -- must anchor to the same side
Vector3[] targetCorners = anchor.rect.worldCorners;
// We want to choose the side with the shortest offset
Vector3 side0 = parent.InverseTransformPoint(Vector3.Lerp(targetCorners[0], targetCorners[3], 0.5f));
Vector3 side1 = parent.InverseTransformPoint(Vector3.Lerp(targetCorners[1], targetCorners[2], 0.5f));
float val0 = localPos.y - side0.y;
float val2 = localPos.y - side1.y;
if (resetRelative)
{
float val1 = localPos.y - Vector3.Lerp(side0, side1, 0.5f).y;
anchor.SetToNearest(val0, val1, val2);
}
else
{
float val = localPos.y - Vector3.Lerp(side0, side1, anchor.relative).y;
anchor.Set(anchor.relative, val);
}
}
else if (anchor.target.camera != null)
{
Vector3[] sides = anchor.target.camera.GetSides(parent);
Vector3 side0 = sides[3];
Vector3 side1 = sides[1];
float val0 = localPos.y - side0.y;
float val2 = localPos.y - side1.y;
if (resetRelative)
{
float val1 = localPos.y - Vector3.Lerp(side0, side1, 0.5f).y;
anchor.SetToNearest(val0, val1, val2);
}
else
{
float val = localPos.y - Vector3.Lerp(side0, side1, anchor.relative).y;
anchor.Set(anchor.relative, val);
}
}
else
{
// Anchored to a simple transform
Vector3 remotePos = anchor.target.position;
if (anchor.targetCam != null) remotePos = anchor.targetCam.WorldToViewportPoint(remotePos);
if (r.anchorCamera != null) remotePos = r.anchorCamera.ViewportToWorldPoint(remotePos);
remotePos = parent.InverseTransformPoint(remotePos);
anchor.absolute = Mathf.FloorToInt(localPos.y - remotePos.y + 0.5f);
anchor.relative = inverted ? 1f : 0f;
}
}
}