白色方块为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;}}
}

RectTransform的Bounds相关推荐

  1. 关于Unity中RectTransform和Transform

    以前一直以为在Inspector面板上的是Transform,后来才发现原来2D是RectTransform,3D是Transform 3D面板上显示的是位置坐标组件Transform,2D面板上显示 ...

  2. Unity进阶技巧 - RectTransform详解

    一.Pivot属性详解 首先为了让大家更好的理解内容,我在Unity中创建了两个UI控件,一个Plane控件,作为父对象,一个Image控件,作为子对象. 然后我们选中子对象,来看看它的RectTra ...

  3. iOS bug 日志 -frame 和 bounds的区别

    使用显式动画,如果设置layer 的bounds和frame效果是不一样的 如果设置 self.layer.bounds = CGRectMake(0, 0, width, width); 效果如图 ...

  4. (0046) iOS开发之View的frame和bounds之解惑

    iOS view的frame和bounds之解惑 frame: 该view在父view坐标系统中的位置和大小.(参照点是,父亲的坐标系统) bounds:该view在本地坐标系统中的位置和大小.(参照 ...

  5. 动态获取UILabel的bounds

    动态获取UILabel的bounds 在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度  CGS ...

  6. linux 内核模块 编译错误 include/linux/mmzone.h:18:26: fatal error: linux/bounds.h: No such file or director

    解决方案:内核编译后 不要执行make clean 出现的问题:编译的时候提示缺少头文件 mmzone.h ....bounds.h...等 include/linux/mmzone.h:18:26: ...

  7. index 4 is out of bounds for dimension 1 with size 4

    index 4 is out of bounds for dimension 1 with size 4 原因:数组越界了

  8. ios开发之UIView的frame、bounds跟center属性的区别(附图)

    博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...

  9. 技术分享-bounds的深入认识

    MAKE BY - LJW -------转载请注明出处------- 前言:初学者对bounds的认识是以自己的左上角为坐标原点,而且x/y一般是0,0 但是对bounds深入认识之后,我们应该对它 ...

  10. iOS开发UI篇—手写控件,frame,center和bounds属性

    一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View ...

最新文章

  1. 重磅!商汤港中文等将开源ECCV2018MS COCO检测比赛冠军代码库
  2. Android视图状态及重绘流程分析,带你一步步深入了解View(三)
  3. nodejs的事件循环1
  4. 基于I2C协议的EEPROM驱动控制
  5. docker create_Docker动手教程2.2:容器基本操作2
  6. js 控制 Windows Media Player
  7. XSLT2.0---多文件输出
  8. MyBatis理解与掌握(输入与输出)
  9. aliyun windows开启远程_阿里云物联网平台远程配置功能JAVA 示例参考
  10. 从DWG导入SKP后的封面问题
  11. 计算机组成原理-总线(系统总线、总线仲裁、总线操作和定时)
  12. LM393实现简易PWM调压电路
  13. 音乐标签修改 android,音乐标签Android版
  14. 小程序数据缓存机制应用
  15. 开源RapidScada插件开发---短信报警插件
  16. 为什么非功能性需求很重要?
  17. 微信域名防封防屏蔽 微信APP下载链接如何做防封防屏蔽
  18. ECSHOP goods表字段分析
  19. 软考系统分析师倒计时第5天
  20. nrf51822蓝牙学习笔记一

热门文章

  1. Linux账户管理详解
  2. 基于android的影音设计,基于Android的车载影音导航系统软件设计与实现
  3. 大闹天宫 页游 架设教程 自玩 单机
  4. Windows环境下搭建SQL注入环境(sqli-labs)
  5. 2015年史玉柱演讲实录
  6. 奇异谱分析(SSA)的matlab实现
  7. 研发人员需要什么知识和能力
  8. layui表格时间类型显示问题
  9. 计算机管理为什么不能扩展卷,Win10 C盘不能扩展卷怎么解决?
  10. 闪瞎眼低效贪吃蛇:数组实现