Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


RectTransform.FitResult

Indicates the result of a RectTransform fit operation.
Read time 2 minutesLast updated 6 days ago

Definition

  • Type: Enum
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public enum RectTransform.FitResult

Remarks

The FitResult enumeration identifies the outcome of RectTransform.TryFitInsideCoplanarRectTransform and RectTransform.FitInsideCoplanarRectTransform. It contains the result of the fit operation, indicating whether it succeeded or failed, and if it failed, the reason for the failure.

Examples

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; case RectTransform.FitResult.FailInvalidSizeTarget: Debug.LogWarning("Target container has invalid size and cannot be fit inside.", this); break; case RectTransform.FitResult.FailLargerThanTarget: Debug.LogWarning("Tooltip is larger than container and cannot be shrunk to fit.", this); break; case RectTransform.FitResult.FailNotCoplanar: Debug.LogWarning("Tooltip and container are not on the same canvas plane.", this); break; case RectTransform.FitResult.FailZRotationMismatch: Debug.LogWarning("Tooltip and container have mismatched Z rotations.", this); break; } }}

Fields

Value

Description

AlreadyInsideThe RectTransform is already fully inside the target. No change is made.
FailInvalidSizeTargetThe target RectTransform has a zero or negative width or height. No change is made.
FailLargerThanTargetThe RectTransform is too large to fit inside the target and
allowShrink
is false. No change is made.
FailNotCoplanarThe RectTransform is not coplanar with the target. No change is made.
FailZRotationMismatchThe RectTransform and the target have different Z rotations. No change is made.
SuccessThe RectTransform is moved, shrunk, or both, to fit inside the target.