Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

RectTransform.TryFitInsideCoplanarRectTransform

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public FitResult TryFitInsideCoplanarRectTransform(RectTransform target, bool allowShrink);

Parameters

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.

Returns

FitResult The result of fitting. See FitResult enumeration for possible values.

Description

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; } } }