NGUI有一个UICenterOnChild脚本,可以轻松实现ScrollView中拖动子物体后保持一个子物体位于中心位置。然而UGUI就没这么方便了,官方并没有类似功能的脚本。网上找到一些运行效果都不对,可能因为UGUI需要配置的东西太多,RectTransfrom不同设置效果就不一样。故自己实现了该功能,使用时的配置如下:

1. 仅适用于水平方向拖动的ScrollRect。
2. ScrollRect中的Grid必须使用GridLayoutGroup。
3. 由于需要知道ScrollRect的宽度以便计算中心位置,故ScrollRect的Anchors的四个小三角中的上面或者下面的一对角不得分离,不然宽度计算出错,即需要:Anchors.Min.x == Anchors.Max.x。最好四角合一。
4. 由于是通过设置ScrollRect's content的localPosition实现,故需要将ScrollRect的中心点Pivot与content的中心点均置于自身最左边(0, 0.5)。
5. 由于第一个与最后一个子物体需要停留在中间,故ScrollRect的Movement Type需要设置为Unrestricted。该项会在运行时自动设置。

代码如下:

     /// <summary>
///
/// 拖动ScrollRect结束时始终让一个子物体位于中心位置。
///
/// </summary>
public class CenterOnChild : MonoBehaviour, IEndDragHandler, IDragHandler
{
//将子物体拉到中心位置时的速度
public float centerSpeed = 9f; //注册该事件获取当拖动结束时位于中心位置的子物体
public delegate void OnCenterHandler (GameObject centerChild);
public event OnCenterHandler onCenter; private ScrollRect _scrollView;
private Transform _container; private List<float> _childrenPos = new List<float> ();
private float _targetPos;
private bool _centering = false; void Awake ()
{
_scrollView = GetComponent<ScrollRect> ();
if (_scrollView == null)
{
Debug.LogError ("CenterOnChild: No ScrollRect");
return;
}
_container = _scrollView.content; GridLayoutGroup grid;
grid = _container.GetComponent<GridLayoutGroup> ();
if (grid == null)
{
Debug.LogError ("CenterOnChild: No GridLayoutGroup on the ScrollRect's content");
return;
} _scrollView.movementType = ScrollRect.MovementType.Unrestricted; //计算第一个子物体位于中心时的位置
float childPosX = _scrollView.GetComponent<RectTransform> ().rect.width * 0.5f - grid.cellSize.x * 0.5f;
_childrenPos.Add (childPosX);
//缓存所有子物体位于中心时的位置
for (int i = ; i < _container.childCount - ; i++)
{
childPosX -= grid.cellSize.x + grid.spacing.x;
_childrenPos.Add (childPosX);
}
} void Update ()
{
if (_centering)
{
Vector3 v = _container.localPosition;
v.x = Mathf.Lerp (_container.localPosition.x, _targetPos, centerSpeed * Time.deltaTime);
_container.localPosition = v;
if (Mathf.Abs (_container.localPosition.x - _targetPos) < 0.01f)
{
_centering = false;
}
}
} public void OnEndDrag (PointerEventData eventData)
{
_centering = true;
_targetPos = FindClosestPos (_container.localPosition.x);
} public void OnDrag (PointerEventData eventData)
{
_centering = false;
} private float FindClosestPos (float currentPos)
{
int childIndex = ;
float closest = ;
float distance = Mathf.Infinity; for (int i = ; i < _childrenPos.Count; i++)
{
float p = _childrenPos[i];
float d = Mathf.Abs (p - currentPos);
if (d < distance)
{
distance = d;
closest = p;
childIndex = i;
}
} GameObject centerChild = _container.GetChild (childIndex).gameObject;
if (onCenter != null)
onCenter (centerChild); return closest;
}
}

由于已缓存所有子物体的位置信息,故该代码简单修改可以扩展功能,如增加到上一项、到下一项、跳转到指定项等功能。

UGUI ScrollRect滑动居中CenterOnChild实现的更多相关文章

  1. UGUI ScrollRect 滑动

    运行环境 Unity3D 5.3.7 p4 在我之前的博客中,写过一些Unity4.6的UGUI,现这篇是基于Unity 5.3的 推荐结构 推荐使用三层来组织,如下所示: ScrollRect :S ...

  2. 横向滑动页面,导航条滑动居中的 js 实现思路

    最近在做新闻咨询页的项目,各个新闻频道通过横向滑动切换,顶部的导航active栏需要跟着切换到对应频道,并且active到达中部时,要一直处在中间. 类似效果就是uc浏览器<UC头条>的导 ...

  3. [unity]UGUI界面滑动,ScrollRect嵌套滑动

    原因:老板蛋痛,让我去抄皇室战争. 思路:我大概知道ngui(后来改成UGUI的)里面有个ScrollView.于是我就想一个横着的SV加上5个竖的SV不就好了吗. 过程: 于是 但是有个问题就是UI ...

  4. UGUI ScrollRect 鼠标滑动灵敏度调节

  5. [Unity UGUI]ScrollRect效果大全

    UGUI各种优化效果 本文所实现的UGUI效果需求如下: - 支持缩放滑动效果 - 支持动态缩放循环加载 - 支持大数据固定Item复用加载 - 支持不用Mask遮罩无限循环加载 - 支持Object ...

  6. UICollectionView 图片横向滑动居中偏移量的解决

    1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分. 2.使用方法前不要开启 ...

  7. UGUI ScrollRect 性能优化

    测试环境 操作系统:Windows8.1 开发工具:Unity5.5.2 1.问题描述,在实际开发过程中经常会使用ScrollRect实现滚动列表,当初次加载数据比较多的情形时,Unity3D会出现比 ...

  8. UGUI 分页渐变居中效果

    代码相当冗长,仅作自己记录 在此分页上修改的https://blog.csdn.net/qinyuanpei/article/details/49781133 using UnityEngine;us ...

  9. UGUI ScrollRect 各参数的代码引用以及作用

随机推荐

  1. 视频服务之ffmpeg部署

    FFmpeg介绍 FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序. 采用LGPL或GPL许可证.它提供了录制.转换以及流化音视频的完整解决方案. 它包含了非常先进 ...

  2. VMware unrecoverable error解决方法

    把开发环境部署在虚拟机里面,重装系统后不须要再反复部署开发环境. 可是有时候异常退出虚拟机会导致错误.之前出现打开虚拟机之后,系统分辨率错误,就是点击的位置和显示的位置不一样. 于是又一次关了虚拟机, ...

  3. inclusion_tag 看图

  4. 之前博客中的代码都放到github上

    之前一直把代码托管在taocode上,现在已经不能用了,所以把代码整理了一下,统一都放在gibhub上了. LALR(1)语法分析生成器:https://github.com/kiven-li/xby ...

  5. 九度OJ 1149:子串计算 (计数、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1026 解决:571 题目描述: 给出一个01字符串(长度不超过100),求其每一个子串出现的次数. 输入: 输入包含多行,每行一个字符串. ...

  6. git拉取远程分支到本地分支或者创建本地新分支

    git fetch origin branchname:branchname 可以把远程某各分支拉去到本地的branchname下,如果没有branchname,则会在本地新建branchname g ...

  7. 【题解】 P5021赛道修建

    [题解]P5021 赛道修建 二分加贪心,轻松拿省一(我没有QAQ) 题干有提示: 输出格式: 输出共一行,包含一个整数,表示长度最小的赛道长度的最大值. 注意到没,最小的最大值,还要多明显? 那么我 ...

  8. ABAP screen

    Instance One : SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-100. SELECTION-SCREEN BEGIN ...

  9. 利用wxpython显示OpenCV图像

    核心代码 import wx, cv2 import numpy as np # Start with a numpy array style image I'll call "source ...

  10. Java for LeetCode 087 Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...