【UGUI】一键排版对齐
来源:互联网 发布:sql语句 数据类型 编辑:程序博客网 时间:2024/06/02 02:26
好久没有写东西了,因为最近忙,做一些项目上具体的功能。由于偶尔还是要拼一下UI,感觉对齐还是一个问题。虽然在以前写过一个一键对齐父节点的东西,可还是不够用,Unity中没有其他工具一样的辅助线。。。so,如果要让两个UI项以某种方式对齐排列,自己手动摆毛事总有那么一点点误差,除非你改坐标。我的功能是这样的:
支持 缩放、锚点、中心点 不同,全部一键搞定。
using System.Collections;using System.Collections.Generic;using UnityEditor;using UnityEngine;/// <summary>/// Introduction: GUIAlignOthers/// Author: Cheng/// Time: /// </summary>public class GUIAlignOthers :Editor{ [MenuItem("GameObject/UI/Align/Top")] static void AlignTop() { Align(1); } [MenuItem("GameObject/UI/Align/Left")] static void AlignLeft() { Align(2); } [MenuItem("GameObject/UI/Align/Right")] static void AlignRight() { Align(3); } [MenuItem("GameObject/UI/Align/Bottom")] static void AlignBottom() { Align(4); } [MenuItem("GameObject/UI/Align/Horizontal")] static void AlignHorizontal() { Align(5); } [MenuItem("GameObject/UI/Align/Vertical")] static void AlignVertical() { Align(6); } static void Align(int type) { List<RectTransform> rects = new List<RectTransform>(); GameObject[] objects = Selection.gameObjects; foreach (var obj in objects) { RectTransform rect = obj.GetComponent<RectTransform>(); if (rect != null) rects.Add(rect); } if (rects.Count > 1) { Align(type, rects); } } static void Align(int type, List<RectTransform> rects) { RectTransform tenplate = rects[0]; float w = tenplate.sizeDelta.x * tenplate.lossyScale.x;//消除缩放影响 float h = tenplate.sizeDelta.y * tenplate.localScale.y; float x = tenplate.position.x - tenplate.pivot.x * w; //消除中心点并非pivot非(0.5,0.5)影响 float y = tenplate.position.y - tenplate.pivot.y * h; switch (type) { case 1://上对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float th = trans.sizeDelta.y * trans.localScale.y; Vector3 pos = trans.position; pos.y = y + h - th + trans.pivot.y * th; trans.position = pos; } break; case 2://左对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float tw = trans.sizeDelta.x * trans.lossyScale.x; Vector3 pos = trans.position; pos.x = x + tw*trans.pivot.x; trans.position = pos; } break; case 3://右对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float tw = trans.sizeDelta.x * trans.lossyScale.x; Vector3 pos = trans.position; pos.x = x+ w - tw + tw * trans.pivot.x; trans.position = pos; } break; case 4://下对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float th = trans.sizeDelta.y * trans.localScale.y; Vector3 pos = trans.position; pos.y = y + th*trans.pivot.y; trans.position = pos; } break; case 5://水平对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float th = trans.sizeDelta.y * trans.localScale.y; Vector3 pos = trans.position; pos.y = y + 0.5f * h - 0.5f*th + th * trans.pivot.y; trans.position = pos; } break; case 6://垂直对齐 for (int i = 1; i < rects.Count; i++) { RectTransform trans = rects[i]; float tw = trans.sizeDelta.x * trans.lossyScale.x; Vector3 pos = trans.position; pos.x = x + 0.5f * w - 0.5f * tw + tw * trans.pivot.x; trans.position = pos; } break; } }}
阅读全文
1 0
- 【UGUI】一键排版对齐
- 【UGUI】一键对齐父节点
- 【Html】段落排版--对齐
- 段落排版--对齐
- Bootstrap3 排版-对齐
- 关于公式对齐的排版
- 文本左右对齐排版--shell
- 一键生成UGUI艺术字体
- UGUI之Text对齐至格子
- Unity UGUI学习(一)
- UGUI<一>:canvas
- uGUI那些事儿一
- Unity-UGUI框架<一>
- 阅读UGUI源码 一
- word论文排版之参考文献的对齐
- 实现TextView 文字排版,分散两端对齐
- Android 实现两端对齐的文字排版
- 个人信息排版左右对齐的TextView
- 互联网创业网站决定成败
- SQL Server 触发器
- 网络编程常用函数
- cocos ScrollView(滚动容器)加载大量item导致的卡顿问题解决方案
- ubuntu alt tab 无法切换应用程序
- 【UGUI】一键排版对齐
- java进行opc连接之四 java jeasyOPC 连接
- A
- C++ function、bind以及lamda表达式
- 生成人脸修复模型:同时使用两个鉴别器,直接合成逼真人脸
- php合并数组的两种方式
- leetcode 274. H-Index
- hql由于关键字报错
- vi编辑器【常用操作】