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

unity GridLayoutGroup真正的居中

GridLayoutGroup默认的居中效果:

  

不是真正的居中 加上代码:
namespace UnityEngine.UI
{/// <summary>/// GridLayoutGroup拓展,使支持自定义内容/// </summary>internal class GridLayoutGroupEx : GridLayoutGroup{/// <summary>/// 启用居中/// </summary>bool m_enableMiddle = true;/// <summary>/// 超过行数采用默认的/// </summary>int m_surpassLine = 2;public bool EnableMiddle{set { m_enableMiddle = value; }get { return m_enableMiddle; }}public int SurpassLine{set { m_surpassLine = value; }get { return m_surpassLine; }}/// <summary>/// Called by the layout system/// Also see ILayoutElement/// </summary>public override void SetLayoutHorizontal(){SetCellsAlongAxis(0);}/// <summary>/// Called by the layout system/// Also see ILayoutElement/// </summary>public override void SetLayoutVertical(){SetCellsAlongAxis(1);}private void SetCellsAlongAxis(int axis){// Normally a Layout Controller should only set horizontal values when invoked for the horizontal axis// and only vertical values when invoked for the vertical axis.// However, in this case we set both the horizontal and vertical position when invoked for the vertical axis.// Since we only set the horizontal position and not the size, it shouldn't affect children's layout,// and thus shouldn't break the rule that all horizontal layout must be calculated before all vertical layout.if (axis == 0){// Only set the sizes when invoked for horizontal axis, not the positions.for (int i = 0; i < rectChildren.Count; i++){RectTransform rect = rectChildren[i];m_Tracker.Add(this, rect,DrivenTransformProperties.Anchors |DrivenTransformProperties.AnchoredPosition |DrivenTransformProperties.SizeDelta);rect.anchorMin = Vector2.up;rect.anchorMax = Vector2.up;rect.sizeDelta = cellSize;}return;}float width = rectTransform.rect.size.x;float height = rectTransform.rect.size.y;int cellCountX = 1;int cellCountY = 1;if (m_Constraint == Constraint.FixedColumnCount){cellCountX = m_ConstraintCount;if (rectChildren.Count > cellCountX)cellCountY = rectChildren.Count / cellCountX + (rectChildren.Count % cellCountX > 0 ? 1 : 0);}else if (m_Constraint == Constraint.FixedRowCount){cellCountY = m_ConstraintCount;if (rectChildren.Count > cellCountY)cellCountX = rectChildren.Count / cellCountY + (rectChildren.Count % cellCountY > 0 ? 1 : 0);}else{if (cellSize.x + spacing.x <= 0)cellCountX = int.MaxValue;elsecellCountX = Mathf.Max(1, Mathf.FloorToInt((width - padding.horizontal + spacing.x + 0.001f) / (cellSize.x + spacing.x)));if (cellSize.y + spacing.y <= 0)cellCountY = int.MaxValue;elsecellCountY = Mathf.Max(1, Mathf.FloorToInt((height - padding.vertical + spacing.y + 0.001f) / (cellSize.y + spacing.y)));}int cornerX = (int)startCorner % 2;int cornerY = (int)startCorner / 2;int cellsPerMainAxis, actualCellCountX, actualCellCountY;if (startAxis == Axis.Horizontal){cellsPerMainAxis = cellCountX;actualCellCountX = Mathf.Clamp(cellCountX, 1, rectChildren.Count);actualCellCountY = Mathf.Clamp(cellCountY, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));}else{cellsPerMainAxis = cellCountY;actualCellCountY = Mathf.Clamp(cellCountY, 1, rectChildren.Count);actualCellCountX = Mathf.Clamp(cellCountX, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));}Vector2 requiredSpace = new Vector2(actualCellCountX * cellSize.x + (actualCellCountX - 1) * spacing.x,actualCellCountY * cellSize.y + (actualCellCountY - 1) * spacing.y);Vector2 startOffset = new Vector2(GetStartOffset(0, requiredSpace.x),GetStartOffset(1, requiredSpace.y));int customActualCellCountX, customActualCellCountY;if (startAxis == Axis.Horizontal){customActualCellCountX = rectChildren.Count % cellCountX;customActualCellCountY = Mathf.Clamp(cellCountY, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));}else{customActualCellCountY = rectChildren.Count % cellCountY;customActualCellCountX = Mathf.Clamp(cellCountX, 1, Mathf.CeilToInt(rectChildren.Count / (float)cellsPerMainAxis));}Vector2 customRequiredSpace = new Vector2(customActualCellCountX * cellSize.x + (customActualCellCountX - 1) * spacing.x,customActualCellCountY * cellSize.y + (customActualCellCountY - 1) * spacing.y);Vector2 customStartOffset = new Vector2(GetStartOffset(0, customRequiredSpace.x),GetStartOffset(1, customRequiredSpace.y));//Debug.Log("actualCellCountX: " + actualCellCountX);//Debug.Log("actualCellCountY: " + actualCellCountY);//Debug.Log("requiredSpace.x: " + requiredSpace.x);//Debug.Log("requiredSpace.y: " + requiredSpace.y);//Debug.Log("startOffset.x: " + startOffset.x);//Debug.Log("startOffset.y: " + startOffset.y);//Debug.Log("-------------------------------");//Debug.Log("customActualCellCountX: " + customActualCellCountX);//Debug.Log("customActualCellCountY: " + customActualCellCountY);//Debug.Log("customRequiredSpace.x: " + customRequiredSpace.x);//Debug.Log("customRequiredSpace.y: " + customRequiredSpace.y);//Debug.Log("customStartOffset.x: " + customStartOffset.x);//Debug.Log("customStartOffset.y: " + customStartOffset.y);int startCount = rectChildren.Count % cellsPerMainAxis;int totalLine = startAxis == Axis.Horizontal ? customActualCellCountY : customActualCellCountX;for (int i = 0; i < rectChildren.Count; i++){int positionX;int positionY;if (m_enableMiddle && startCount > 0 && totalLine <= m_surpassLine){if (i < startCount){if (startAxis == Axis.Horizontal){positionX = i;positionY = 0;if (cornerX == 1)positionX = startCount - 1 - positionX;if (cornerY == 1)positionY = actualCellCountY - 1 - positionY;}else{positionX = 0;positionY = i;if (cornerX == 1)positionX = actualCellCountX - 1 - positionX;if (cornerY == 1)positionY = startCount - 1 - positionY;}SetChildAlongAxis(rectChildren[i], 0, customStartOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);SetChildAlongAxis(rectChildren[i], 1, customStartOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);}else{int index = i - startCount;if (startAxis == Axis.Horizontal){positionX = index % cellsPerMainAxis;positionY = index / cellsPerMainAxis + 1;}else{positionX = index / cellsPerMainAxis + 1;positionY = index % cellsPerMainAxis;}if (cornerX == 1)positionX = actualCellCountX - 1 - positionX;if (cornerY == 1)positionY = actualCellCountY - 1 - positionY;SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);SetChildAlongAxis(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);}}else{if (startAxis == Axis.Horizontal){positionX = i % cellsPerMainAxis;positionY = i / cellsPerMainAxis;}else{positionX = i / cellsPerMainAxis;positionY = i % cellsPerMainAxis;}if (cornerX == 1)positionX = actualCellCountX - 1 - positionX;if (cornerY == 1)positionY = actualCellCountY - 1 - positionY;SetChildAlongAxis(rectChildren[i], 0, startOffset.x + (cellSize[0] + spacing[0]) * positionX, cellSize[0]);SetChildAlongAxis(rectChildren[i], 1, startOffset.y + (cellSize[1] + spacing[1]) * positionY, cellSize[1]);}//Debuger.Log("positionX: " + positionX + "positionY: " + positionY + ",cellsPerMainAxis: " + cellsPerMainAxis);}}}
}

效果:

  

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 前段框架有哪些
  • 【Anaconda】修改jupyter notebook默认打开的工作目录、jupyter notebook快捷键
  • 装饰器模式(Decorator Pattern)
  • 4.2 Sensors -- onKeyStroke
  • 网络安全:键盘记录器
  • 算法训练营|图论第8天 拓扑排序 dijkstra
  • (二十六)Java 数据结构
  • 山东省行政执法证照片要求及图像处理方法
  • 暑期学习总结
  • Android 11添加系统服务,并封装jar包供第三方应用使用
  • Kafka【五】Buffer Cache (缓冲区缓存)、Page Cache (页缓存)和零拷贝技术
  • python与pytroch相关
  • linux 下一跳缓存,early demux(‌早期解复用)‌介绍
  • 探索PDF的奥秘:pdfrw库的神奇之旅
  • 32 配置多路由的静态路由
  • 9月CHINA-PUB-OPENDAY技术沙龙——IPHONE
  • 【知识碎片】第三方登录弹窗效果
  • 10个最佳ES6特性 ES7与ES8的特性
  • 2017届校招提前批面试回顾
  • gf框架之分页模块(五) - 自定义分页
  • hadoop集群管理系统搭建规划说明
  • happypack两次报错的问题
  • java2019面试题北京
  • js正则,这点儿就够用了
  • overflow: hidden IE7无效
  • Python实现BT种子转化为磁力链接【实战】
  • spring cloud gateway 源码解析(4)跨域问题处理
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 观察者模式实现非直接耦合
  • 盘点那些不知名却常用的 Git 操作
  • 区块链共识机制优缺点对比都是什么
  • 使用 @font-face
  • 一道闭包题引发的思考
  • 用quicker-worker.js轻松跑一个大数据遍历
  • Android开发者必备:推荐一款助力开发的开源APP
  • ​​​​​​​开发面试“八股文”:助力还是阻力?
  • # 利刃出鞘_Tomcat 核心原理解析(七)
  • #LLM入门|Prompt#1.7_文本拓展_Expanding
  • #nginx配置案例
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (1)SpringCloud 整合Python
  • (C#)获取字符编码的类
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (二十四)Flask之flask-session组件
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (七)Appdesigner-初步入门及常用组件的使用方法说明
  • (十六)一篇文章学会Java的常用API
  • (微服务实战)预付卡平台支付交易系统卡充值业务流程设计
  • (转)ORM
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • **CI中自动类加载的用法总结
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET IoC 容器(三)Autofac
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .NetCore项目nginx发布