RectTransform的Bounds

RectTransform的Bounds

白色方块为view:
红色方块为content:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoundsTest : MonoBehaviour
{
    public RectTransform m_view;
    public RectTransform m_content;

    public void ShowBounds()
    {
        Bounds bounds = new Bounds(m_view.rect.center, m_view.rect.size);
        Debug.LogError("view bounds" + bounds + "  " + bounds.min + "  " + bounds.max);

        Bounds bounds2 = new Bounds(m_content.rect.center, m_content.rect.size);
        Debug.LogError("content bounds" + bounds2 + "  " + bounds2.min + "  " + bounds2.max);

        Bounds bounds3 = GetBounds();
        Debug.LogError("bounds3 bounds" + bounds3 + "  " + bounds3.min + "  " + bounds3.max);
    }

    public void AdjustBounds()
    {
        Bounds m_ViewBounds = new Bounds(m_view.rect.center, m_view.rect.size);
        Bounds m_ContentBounds = GetBounds();
        Vector3 contentSize = m_ContentBounds.size;
        Vector3 contentPos = m_ContentBounds.center;
        var contentPivot = m_content.pivot;
        AdjustBounds(ref m_ViewBounds, ref contentPivot, ref contentSize, ref contentPos);
    }

    private readonly Vector3[] m_Corners = new Vector3[4];
    private Bounds GetBounds()
    {
        if (m_content == null)
            return new Bounds();
        m_content.GetWorldCorners(m_Corners);
        var viewWorldToLocalMatrix = m_view.worldToLocalMatrix;
        return InternalGetBounds(m_Corners, ref viewWorldToLocalMatrix);
    }

    internal static Bounds InternalGetBounds(Vector3[] corners, ref Matrix4x4 viewWorldToLocalMatrix)
    {
        var vMin = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
        var vMax = new Vector3(float.MinValue, float.MinValue, float.MinValue);

        for (int j = 0; j < 4; j++)
        {
            Vector3 v = viewWorldToLocalMatrix.MultiplyPoint3x4(corners[j]);
            vMin = Vector3.Min(v, vMin);
            vMax = Vector3.Max(v, vMax);
        }

        var bounds = new Bounds(vMin, Vector3.zero);
        bounds.Encapsulate(vMax);
        return bounds;
    }

    internal static void AdjustBounds(ref Bounds viewBounds, ref Vector2 contentPivot, ref Vector3 contentSize, ref Vector3 contentPos)
    {
        // Make sure content bounds are at least as large as view by adding padding if not.
        // One might think at first that if the content is smaller than the view, scrolling should be allowed.
        // However, that's not how scroll views normally work.
        // Scrolling is *only* possible when content is *larger* than view.
        // We use the pivot of the content rect to decide in which directions the content bounds should be expanded.
        // E.g. if pivot is at top, bounds are expanded downwards.
        // This also works nicely when CFContentSizeFitter is used on the content.
        Vector3 excess = viewBounds.size - contentSize;
        if (excess.x > 0)
        {
            contentPos.x -= excess.x * (contentPivot.x - 0.5f);
            contentSize.x = viewBounds.size.x;
        }
        if (excess.y > 0)
        {
            contentPos.y -= excess.y * (contentPivot.y - 0.5f);
            contentSize.y = viewBounds.size.y;
        }
    }
}
上一篇:Unity手机端手势基本操作


下一篇:Unity 之坐标系