| Parameter | Description |
|---|---|
| target | The RectTransform to fit this RectTransform inside. |
| allowShrink | Whether to scale this RectTransform down uniformly when it is larger than target. If false and the RectTransform doesn't fit, returns RectTransform.FitResult.FailLargerThanTarget. |
FitResult The result of fitting. See FitResult enumeration for possible values.
Checks that this RectTransform is coplanar with the given RectTransform, then moves and optionally shrinks it to fit inside.
This method performs the same fit operation as RectTransform.FitInsideCoplanarRectTransform with additional checks for target size, coplanarity and Z-rotation. If the target RectTransform has an invalid size, returns RectTransform.FitResult.FailInvalidSizeTarget without making any changes. If this RectTransform and target are not coplanar, returns RectTransform.FitResult.FailNotCoplanar without making any changes. If their Z rotations do not match, returns RectTransform.FitResult.FailZRotationMismatch without making any changes.
Use this method when you want to check that two RectTransforms are coplanar and share the same Z rotation. For example, when constraining a tooltip or popup panel to a container. Use RectTransform.FitInsideCoplanarRectTransform to skip the checks.
Additional resources: FitInsideCoplanarRectTransform, FitResult.
//Attach this script to a tooltip panel that is a child of a screen-space canvas. //When the button is clicked, the tooltip will attempt to fit inside the canvas at the button's position. using UnityEngine; using UnityEngine.UI;
public class TooltipController : MonoBehaviour { [SerializeField] RectTransform m_ScreenRectTransform;
[SerializeField] Button m_Button;
RectTransform m_TooltipRectTransform;
void Awake() { m_TooltipRectTransform = GetComponent<RectTransform>(); gameObject.SetActive(false); m_Button.onClick.AddListener(() => { ShowAt(m_Button.GetComponent<RectTransform>()); }); }
public void ShowAt(RectTransform anchor) { gameObject.SetActive(true); m_TooltipRectTransform.position = anchor.position;
// Constrain the tooltip to the container. RectTransform.FitResult fitResult = m_TooltipRectTransform.TryFitInsideCoplanarRectTransform(m_ScreenRectTransform, true); switch (fitResult) { case RectTransform.FitResult.Success: case RectTransform.FitResult.AlreadyInside: Debug.Log("Tooltip fit inside container successfully.", this); break; default: Debug.LogWarning("Tooltip failed to fit inside container: " + fitResult, this); break; } } }