当前位置: 首页 > news >正文

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

相关文章:

  • 爱的十个秘密--4.给予的力量
  • 特效simulation space的设置,导致残留场景
  • unity网络线程和主线程出队列
  • C++网络编程--简单的WinSock代码
  • unity中UI界面的一些动画实现总结
  • Delphi调用CreateProcess创建进程
  • untiy打android包,报Could not resolve com.android.tools.build:gradle和Connect to 127.0.0.1:80 [/127.0.0.1
  • C#访问lua获取一个全局基本数据类型
  • C#访问lua获取一个全局基本数据类型——table映射成C#中class或struct
  • Linux学习笔记12——Unix中的进程
  • C#访问lua获取一个全局基本数据类型——table映射成C#中interface
  • C#访问lua获取全局函数——映射成delegate
  • C#访问lua获取一个table中的函数——映射成interface中的函数
  • C#访问lua获取全局函数——此函数带多个返回值
  • [置顶] sqlserver2008 新建表之后无法更改表设计的原因
  • SegmentFault for Android 3.0 发布
  • 【翻译】Mashape是如何管理15000个API和微服务的(三)
  • 【跃迁之路】【669天】程序员高效学习方法论探索系列(实验阶段426-2018.12.13)...
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • 11111111
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Apache Spark Streaming 使用实例
  • C++11: atomic 头文件
  • extjs4学习之配置
  • HTTP 简介
  • JavaScript HTML DOM
  • Java-详解HashMap
  • js递归,无限分级树形折叠菜单
  • Mac 鼠须管 Rime 输入法 安装五笔输入法 教程
  • Mac转Windows的拯救指南
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • vue.js框架原理浅析
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 前端学习笔记之观察者模式
  • 三分钟教你同步 Visual Studio Code 设置
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 我与Jetbrains的这些年
  • 延迟脚本的方式
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • ​​​​​​​​​​​​​​Γ函数
  • ​linux启动进程的方式
  • ​MPV,汽车产品里一个特殊品类的进化过程
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (27)4.8 习题课
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (LeetCode) T14. Longest Common Prefix
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (转)ObjectiveC 深浅拷贝学习
  • (转)编辑寄语:因为爱心,所以美丽
  • ***测试-HTTP方法
  • .NET 3.0 Framework已经被添加到WindowUpdate