Vector3 GetRelateeLocalPos(RelativeLocationDirection direction = RelativeLocationDirection.right) { Vector3 delta = Vector3.zero; switch (direction) { case RelativeLocationDirection.left: delta.x -= this.relatee.GetSelfWidth() / 2; break; case RelativeLocationDirection.right: delta.x += this.relatee.GetSelfWidth() / 2; break; case RelativeLocationDirection.center: break; case RelativeLocationDirection.top: delta.y += this.relatee.GetSelfHeight() / 2; break; case RelativeLocationDirection.bottom: delta.y -= this.relatee.GetSelfHeight() / 2; break; default: break; } return(relatee.localPosition + delta); }
Vector3 AdjustRelatorLocalPos(Vector3 localPos, RelativeLocationDirection direction = RelativeLocationDirection.right) { Vector3 delta = Vector3.zero; switch (direction) { case RelativeLocationDirection.left: delta.x -= selfRectrans.GetSelfWidth() / 2; break; case RelativeLocationDirection.right: delta.x += selfRectrans.GetSelfWidth() / 2; break; case RelativeLocationDirection.center: break; case RelativeLocationDirection.top: delta.y += selfRectrans.GetSelfHeight() / 2; break; case RelativeLocationDirection.bottom: delta.y -= selfRectrans.GetSelfHeight() / 2; break; default: break; } return(localPos + delta); }
public Vector3 FixLocalPosition(RectTransform relatee = null, RelativeLocationDirection direction = RelativeLocationDirection.right) { this.relatee = relatee ?? this.relatee; Vector3 result = GetRelateeLocalPos(direction); result = this.relatee.LocalPosToCenterPos(result); result = this.relatee.parent.TransformPoint(result); result = transform.parent.InverseTransformPoint(result); result = selfRectrans.CenterPosToLocalPos(result); result = AdjustRelatorLocalPos(result, direction); selfRectrans.localPosition = result; return(result); }