使用UGUI ScrollView 排列不规则内容元素实现滑动效果
来源:互联网 发布:java import include 编辑:程序博客网 时间:2024/05/16 11:52
在开发Unity项目中有时会遇到滑动视图,这个一般实现方式都是使用UGUI组件ScrollView实现,搭配GridLayoutGroup,以及ContentSizeFitter实现,但是当项目中需要使用不规则内容元素动态载入的时候,GridLayoutGruop就变得不太适用。。。正好在项目中,遇到这个问题,我把我的做法分享一下,也算给自己记一个笔迹。
1,我的实现方式是,使用VerticalLayoutGroup,以及ContentSizeFitter,以及自己写的一个类似ContentSizeFitter代码实现。
实现效果简要说明:
具体实现步骤图示
步骤一:
步骤二:
步骤三:
步骤四:
步骤五:
步骤六:计算ScrollView显示区高度
using System.Collections;using UnityEngine;public class ListUIMng : MonoBehaviour { public GameObject prefab; public Transform content; // Use this for initialization void Start() { SizeInit(); } // Update is called once per frame void Update () { } private void OnGUI() { if (GUI.Button(new Rect(20,100,100,50),"添加道具")) { GameObject tool = GameObject.Instantiate(prefab); tool.transform.parent = content.GetChild(1); StartCoroutine(SetHight()); } if (GUI.Button(new Rect(20, 300, 100, 50), "添加应用场景")) { GameObject scence = GameObject.Instantiate(prefab); scence.transform.parent = content.GetChild(content.childCount - 1); StartCoroutine(SetHight()); } } public void SizeInit() { StartCoroutine(SetHight()); } IEnumerator SetHight() { yield return new WaitForSeconds(0.1f); //采用高度累加的方式,避免动态加入元素,最后元素位置不刷新问题(通过第一个元素和最后元素计算,刷新延迟会出问题) float hight = 0; for (int i = 0; i < content.childCount; i++) { hight += content.GetChild(i).GetComponent<RectTransform>().sizeDelta.y; } Debug.Log("======hight==" + hight); RectTransform rect = content.GetComponent<RectTransform>(); rect.sizeDelta = new Vector2(rect.sizeDelta.x, hight); }}
阅读全文
0 0
- 使用UGUI ScrollView 排列不规则内容元素实现滑动效果
- ScrollView实现滑动效果
- ScrollView滑动监听实现界面动画效果
- ScrollView滑动悬停ToolBar效果实现
- ugui scrollview使用
- 使用ScrollView实现滚动效果
- 使用ScrollView实现滚动效果
- ScrollView滑动悬停效果
- UGUI实现页面滑动
- UGUI之滑动翻页效果
- UGUI中ScrollView滑动适应问题解决办法
- UGUI实现不规则区域点击响应
- 【Unity3d】UGUI小贴士:使用不规则按钮
- 【Unity3d】UGUI小贴士:使用不规则按钮
- 如何使用UGUI创建制作不规则按钮
- Android ScrollView向上滑动控件顶部悬浮效果实现
- Android ScrollView向上滑动控件顶部悬浮效果实现
- 实现ScrollView滑动时标题栏的隐藏效果
- API(get/post)
- 关于Java中的继承,包括重写、构造器、访问权限、构造过程等知识总结
- webstorm项目运行到apache服务器
- POJ
- 微积分公式大全
- 使用UGUI ScrollView 排列不规则内容元素实现滑动效果
- 分析SYSTEMSTATS DUMP
- 关于时间处理
- org.springframework.orm.hibernate4.HibernateSystemException: could not deserialize
- 解决ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server 方法
- 第一天:SQL简介
- hadoop多次格式化namenode造成datanode无法启动问题解决
- MySQL数据导出与导入
- 引用类型(三)——基本包装类型(一)