【小松教你手游开发】【系统模块开发】父节点下的各个子节点居中摆放
来源:互联网 发布:python beaker 编辑:程序博客网 时间:2024/05/01 03:08
有过一个需求需要一个item,两个item,三个item(不一定有多少个子节点)不同情况都要居中
有两个方法解决:
1.只需要在uiscrollview上的resetPosition 设为0.5,调一下resetPosition自动居中
2.如果本身在uiScrollview上就不能这么用了,两个uiscrollview不能重叠嘛。
所以写了个脚本负责计算每个子节点的localposition。
using UnityEngine;using System.Collections;using System.Collections.Generic;public class CenterChildTransfroms{ UIAnchor m_anchor; private Transform m_transform; private GameObject m_containerGameObject; private float m_spacing; public CenterChildTransfroms(Transform transform, GameObject containerGameObject,Camera camera, float spacing =0) { m_transform = transform; m_containerGameObject = containerGameObject; m_spacing = spacing; m_anchor = m_transform.GetComponent<UIAnchor>(); if (m_anchor == null) m_anchor = m_transform.gameObject.AddComponent<UIAnchor>(); m_anchor.uiCamera = camera; m_anchor.container = containerGameObject; m_anchor.side = UIAnchor.Side.Center; m_anchor.enabled = false; } public void RepositionChildTransfroms() { float totalWidth = 0; List<Transform> childTrans = new List<Transform>(); List<float> childTransBoundSizeX = new List<float>(); foreach (Transform t in m_transform) { if(t.gameObject.activeSelf) { childTrans.Add(t); float width = (NGUIMath.CalculateRelativeWidgetBounds(t).size.x * t.localScale.x) + m_spacing; totalWidth += width; childTransBoundSizeX.Add(width); } } if(childTrans.Count > 0) { //先计算好第一个transform坐标 childTrans[0].localPosition = new Vector3((-totalWidth / 2 + childTransBoundSizeX[0] / 2), childTrans[0].localPosition.y, childTrans[0].localPosition.z); for(int i =1;i<childTrans.Count;i++) { float lastTransBoundX = childTransBoundSizeX[i - 1] / 2; float transBoundX = childTransBoundSizeX[i] / 2; childTrans[i].localPosition = new Vector3(childTrans[i - 1].localPosition.x + lastTransBoundX + transBoundX , childTrans[i].localPosition.y,childTrans[i].localPosition.z); } } CenterTransform(); } public void CenterTransform() { m_anchor.enabled = true; }}
这里可以看到使用前是需要传递父节点transform,居中在哪个GameObject的那个gameObject,传入所属UIcamera,和每个子节点之间间隙。
其实也就是在父节点上添加一个锚点UIAnchor,把需要的参数(cotainerGameObject,Camera)塞进去.
计算时先计算总宽,算出并摆放第一个transform的localPosition,之后根据这个坐标进行一个个计算
0 0
- 【小松教你手游开发】【系统模块开发】父节点下的各个子节点居中摆放
- 【小松教你手游开发】【系统模块开发】根据上一个GameObject坐标生成的tips界面
- 【小松教你手游开发】【unity系统模块开发】Unity动画系统项目实战
- 【小松教你手游开发】【系统模块开发】ngui做聊天系统
- 【小松教你手游开发】【系统模块开发】u3d c# 写一个通用倒计时器
- 【小松教你手游开发】【系统模块开发】做一个3d旋转菜单
- 【小松教你手游开发】【系统模块开发】动态可拖动列表DynmicList,ScrollView
- 【小松教你手游开发】【系统模块开发】射线触发按钮
- 通用获取父节点/子节点/子节点下所有节点ID的存储过程
- 通用获取父节点/子节点/子节点下所有节点ID的存储过程
- sql 查找节点下的所有子节点,查找节点的所有父节点
- 【小松教你手游开发】【系统模块开发】图文混排 (在label中插入表情)
- 【小松教你手游开发】【系统模块开发】unity 数据储存到本地为二进制文件(聊天记录本地储存)
- 删除父节点下的所有子节点
- 删除父节点下的所有子节点
- 后台如何获取所有选中的子节点或者不同父节点下的子节点
- Linux系统开发:文件的i节点
- 父节点获取子节点的字符串
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C 模拟
- RSA算法原理(一)
- Git之撤销修改
- rman异机增量备份恢复
- ocalparam和parameter的区别 --引用特权博客
- 【小松教你手游开发】【系统模块开发】父节点下的各个子节点居中摆放
- android搜索热词(热门标签)流式布局的实现
- 深入浅出RxJava就这一篇就够了
- Cordova 混合应用处理输入法相关事件
- 非构造函数的继承 深拷贝
- 同一个textview设置不同文字样式
- MySQL:常用函数
- [JZOJ4624] 字符串匹配
- 安装spark,基于hadoop的