
通过拖拽或者点击图片 使图片居中放大
使用scroll view组件 并为子节点conter添加 Grid layout Group 组件
实现逻辑
记录每个item到scroll中心点的距离的数据
在拖拽过程中 通过当前conter的localpostion位置 去判断离中心点距离最短的item
找到对应item 让conter的localpostion通过插值lerp 移动到对应的位置 使其item居中并缩放
水平拖拽 这里知晓localpostion关于x的信息即可
垂直拖拽 即y。

//初始化函数
private void InitView()
{
//获取组件ScrollRect
scrollRect = transform.GetComponent<ScrollRect>();
//获取Scrollview下的conter
content = scrollRect.content;
//设置svcoll属性为无限制
scrollRect.movementType = ScrollRect.MovementType.Unrestricted;
//对应的GridLayoutGroup组件
layoutGroup = content.GetComponent<GridLayoutGroup>();

本文介绍了如何在Unity UGUI中创建一个列表,使得用户可以通过拖拽或点击图片,使图片居中并放大。通过使用Scroll View组件和Grid Layout Group,记录每个列表项到滚动视图中心点的距离,拖拽时动态判断并更新最接近中心的项,使用插值Lerp平滑移动和缩放。同时,通过Button组件监听点击事件,也能实现列表项的居中放大功能。
7591

被折叠的 条评论
为什么被折叠?



