# TryFitInsideCoplanarRectTransform(RectTransform, bool)

> Checks that this RectTransform is coplanar with the given RectTransform, then moves and optionally shrinks it to fit inside.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public RectTransform.FitResult TryFitInsideCoplanarRectTransform(RectTransform target, bool allowShrink = false)
```

### Parameters

**** (\[RectTransform]\(/engine/6000.7/script-reference/unityengine/recttransform)): The RectTransform to fit this RectTransform inside.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): 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](/engine/6000.7/script-reference/unityengine/recttransform/fitresult/faillargerthantarget.md).

### Returns

| Type                                                                                | Description                                                                                                                                                   |
| ----------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [FitResult](/engine/6000.7/script-reference/unityengine/recttransform/fitresult.md) | The result of fitting. See [RectTransform.FitResult](/engine/6000.7/script-reference/unityengine/recttransform/fitresult.md) enumeration for possible values. |

### Remarks

This method performs the same fit operation as [RectTransform.FitInsideCoplanarRectTransform](/engine/6000.7/script-reference/unityengine/recttransform/fitinsidecoplanarrecttransform.md) with additional checks for target size, coplanarity and Z-rotation. If the target RectTransform has an invalid size, returns [RectTransform.FitResult.FailInvalidSizeTarget](/engine/6000.7/script-reference/unityengine/recttransform/fitresult/failinvalidsizetarget.md) without making any changes. If this RectTransform and `target` are not coplanar, returns [RectTransform.FitResult.FailNotCoplanar](/engine/6000.7/script-reference/unityengine/recttransform/fitresult/failnotcoplanar.md) without making any changes. If their Z rotations do not match, returns [RectTransform.FitResult.FailZRotationMismatch](/engine/6000.7/script-reference/unityengine/recttransform/fitresult/failzrotationmismatch.md) 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](/engine/6000.7/script-reference/unityengine/recttransform/fitinsidecoplanarrecttransform.md) to skip the checks.

Additional Resources: [RectTransform.FitInsideCoplanarRectTransform](/engine/6000.7/script-reference/unityengine/recttransform/fitinsidecoplanarrecttransform.md), [RectTransform.FitResult](/engine/6000.7/script-reference/unityengine/recttransform/fitresult.md).

### Examples

```csharp
//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;
        }
    }
}
```
