/// <summary> /// src用描点对齐tar的描点,tar确定src的位置 /// </summary> /// <param name="src"></param> /// <param name="srcSide"></param> /// <param name="tar"></param> /// <param name="tarSide"></param> /// <param name="area"></param> public static void AnchorTo(RectTransform src, UGUISide srcSide, RectTransform tar, UGUISide tarSide, Transform canvas) { if (null == tar || tar == src) { return; } Bounds srcBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(canvas, src); //计算src的包围盒 Bounds tarBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(canvas, tar); //计算tar的包围盒 Vector2 srcOffset = GetBoundsOffset(srcBounds, srcSide); //计算描点的偏移量 Vector2 tarOffset = GetBoundsOffset(tarBounds, tarSide); //计算描点的偏移量 Vector2 tarCenter = tarBounds.center; //计算src相对于tar的位置 src.anchoredPosition = (tarCenter - tarOffset + srcOffset); D.log("src.anchoredPosition=" + src.anchoredPosition); }
public static Vector2 GetBoundsOffset(Bounds bounds, UGUISide side) { Vector2 offset = Vector2.zero; switch (side) { case UGUISide.Bottom: offset.y = bounds.extents.y; break; case UGUISide.BottomLeft: offset.x = bounds.extents.x; offset.y = bounds.extents.y; break; case UGUISide.BottomRight: offset.x = -bounds.extents.x; offset.y = bounds.extents.y; break; case UGUISide.Left: offset.x = bounds.extents.x; break; case UGUISide.Right: offset.x = -bounds.extents.x; break; case UGUISide.Top: offset.y = -bounds.extents.y; break; case UGUISide.TopLeft: offset.x = bounds.extents.x; offset.y = -bounds.extents.y; break; case UGUISide.TopRight: offset.x = -bounds.extents.x; offset.y = -bounds.extents.y; break; } return(offset); }