Indicates the result of a RectTransform fit operation.
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.
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; } } }
| Property | Description |
|---|---|
| Success | The RectTransform is moved, shrunk, or both, to fit inside the target. |
| AlreadyInside | The RectTransform is already fully inside the target. No change is made. |
| FailLargerThanTarget | The RectTransform is too large to fit inside the target and allowShrink is false. No change is made. |
| FailNotCoplanar | The RectTransform is not coplanar with the target. No change is made. |
| FailZRotationMismatch | The RectTransform and the target have different Z rotations. No change is made. |
| FailInvalidSizeTarget | The target RectTransform has a zero or negative width or height. No change is made. |