Unity Ugui 实现 列表item居中放大效果

Unity Ugui 实现 列表item居中放大效果_第1张图片

通过拖拽或者点击图片 使图片居中放大

使用scroll view组件 并为子节点conter添加 Grid layout Group 组件

实现逻辑

记录每个item到scroll中心点的距离的数据

在拖拽过程中 通过当前conter的localpostion位置 去判断离中心点距离最短的item

找到对应item 让conter的localpostion通过插值lerp 移动到对应的位置 使其item居中并缩放

水平拖拽 这里知晓localpostion关于x的信息即可

垂直拖拽 即y。

Unity Ugui 实现 列表item居中放大效果_第2张图片

 

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

你可能感兴趣的:(unity,ui,游戏引擎)