/// <summary> /// Convenience function that switches the anchor mode and ensures that dimensions are kept intact. /// </summary> static void UpdateVerticalAnchor (UIRect r, UIRect.AnchorPoint anchor, bool relative, bool chooseClosest) { // 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 simple transform Vector3 remotePos = parent.InverseTransformPoint(anchor.target.position); anchor.absolute = Mathf.FloorToInt(localPos.y - remotePos.y + 0.5f); anchor.relative = inverted ? 1f : 0f; } else { // Anchored to a rectangle -- must anchor to the same side Vector3[] targetCorners = anchor.rect.worldCorners; if (relative) { Vector3 remotePos = parent.InverseTransformPoint(Vector3.Lerp(targetCorners[i0], targetCorners[i1], 0.5f)); float offset = localPos.y - remotePos.y; targetCorners = anchor.rect.localCorners; float remoteSize = targetCorners[1].y - targetCorners[0].y; anchor.absolute = 0; anchor.relative = offset / remoteSize; if (inverted) anchor.relative += 1f; } else { // 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; float val1 = localPos.y - Vector3.Lerp(side0, side1, 0.5f).y; if (chooseClosest) anchor.SetToNearest(val0, val1, val2); else if (inverted) anchor.Set(1f, val2); else anchor.Set(0f, val0); } } }
/// <summary> /// Convenience function that switches the anchor mode and ensures that dimensions are kept intact. /// </summary> 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; } }
/// <summary> /// Convenience function that switches the anchor mode and ensures that dimensions are kept intact. /// </summary> static void UpdateHorizontalAnchor(UIRect r, UIRect.AnchorPoint anchor) { // 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.rightAnchor); int i0 = inverted ? 2 : 0; int i1 = inverted ? 3 : 1; // Calculate the left side Vector3[] myCorners = r.worldCorners; Vector3 localPos = parent.InverseTransformPoint(Vector3.Lerp(myCorners[i0], myCorners[i1], 0.5f)); if (anchor.rect == null) { // Anchored to a simple transform Vector3 remotePos = parent.InverseTransformPoint(anchor.target.position); anchor.absolute = Mathf.FloorToInt(localPos.x - remotePos.x + 0.5f); anchor.relative = inverted ? 1f : 0f; } else { // 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[1], 0.5f)); Vector3 side1 = parent.InverseTransformPoint(Vector3.Lerp(targetCorners[2], targetCorners[3], 0.5f)); float val0 = localPos.x - side0.x; float val2 = localPos.x - side1.x; float val1 = localPos.x - Vector3.Lerp(side0, side1, 0.5f).x; anchor.SetToNearest(val0, val1, val2); } }