NGUI 相片循环滚动效果
来源:互联网 发布:mac系统能玩的网络游戏 编辑:程序博客网 时间:2024/05/21 09:16
将NGUI 自带Demo Example14的Vertical2抽出来,UICenterOnChild.cs自带的这个类里nextPageThreshold(滑动灵敏参数)无效
整个类替换之后即可实现效果。
//----------------------------------------------// NGUI: Next-Gen UI kit// Copyright © 2011-2014 Tasharen Entertainment//----------------------------------------------using UnityEngine;/// <summary>/// Ever wanted to be able to auto-center on an object within a draggable panel?/// Attach this script to the container that has the objects to center on as its children./// </summary>[AddComponentMenu("NGUI/Interaction/Center Scroll View on Child")]public class UICenterOnChild : MonoBehaviour{ /// <summary> /// The strength of the spring. /// </summary> public float springStrength = 8f; /// <summary> /// If set to something above zero, it will be possible to move to the next page after dragging past the specified threshold. /// </summary> public float nextPageThreshold = 0f; /// <summary> /// Callback to be triggered when the centering operation completes. /// </summary> public SpringPanel.OnFinished onFinished; UIScrollView mScrollView; GameObject mCenteredObject; /// <summary> /// Game object that the draggable panel is currently centered on. /// </summary> public GameObject centeredObject { get { return mCenteredObject; } } void OnEnable() { Recenter(); } void OnDragFinished() { if (enabled) Recenter(); } /// <summary> /// Ensure that the threshold is always positive. /// </summary> void OnValidate() { nextPageThreshold = Mathf.Abs(nextPageThreshold); } /// <summary> /// Recenter the draggable list on the center-most child. /// </summary> public void Recenter() { if (mScrollView == null) { mScrollView = NGUITools.FindInParents<UIScrollView>(gameObject); if (mScrollView == null) { Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this); enabled = false; return; } else { mScrollView.onDragFinished = OnDragFinished; if (mScrollView.horizontalScrollBar != null) mScrollView.horizontalScrollBar.onDragFinished = OnDragFinished; if (mScrollView.verticalScrollBar != null) mScrollView.verticalScrollBar.onDragFinished = OnDragFinished; } } if (mScrollView.panel == null) return; // Calculate the panel's center in world coordinates Vector3[] corners = mScrollView.panel.worldCorners; Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f; // Offset this value by the momentum Vector3 pickingPoint = panelCenter - mScrollView.currentMomentum * (mScrollView.momentumAmount * 0.1f); mScrollView.currentMomentum = Vector3.zero; float min = float.MaxValue; Transform closest = null; Transform trans = transform; int index = 0; // Determine the closest child for (int i = 0, imax = trans.childCount; i < imax; ++i) { Transform t = trans.GetChild(i); float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint); if (sqrDist < min) { min = sqrDist; closest = t; index = i; } } // If we have a touch in progress and the next page threshold set if (nextPageThreshold > 0f && UICamera.currentTouch != null) { // If we're still on the same object if (mCenteredObject != null && mCenteredObject.transform == trans.GetChild(index)) { Vector2 delta = UICamera.currentTouch.totalDelta; Debug.Log("delta="+ delta+ " nextPageThreshold="+ nextPageThreshold+" index= "+index); if (delta.x > nextPageThreshold) { // Next page if (index > 0) closest = trans.GetChild(index - 1); else { closest= trans.GetChild(trans.childCount - 1); } } else if (delta.x < -nextPageThreshold) { // Previous page if (index < trans.childCount - 1) closest = trans.GetChild(index + 1); else closest = trans.GetChild(0); } } } CenterOn(closest, panelCenter); } /// <summary> /// Center the panel on the specified target. /// </summary> void CenterOn(Transform target, Vector3 panelCenter) { if (target != null && mScrollView != null && mScrollView.panel != null) { Transform panelTrans = mScrollView.panel.cachedTransform; mCenteredObject = target.gameObject; // Figure out the difference between the chosen child and the panel's center in local coordinates Vector3 cp = panelTrans.InverseTransformPoint(target.position); Vector3 cc = panelTrans.InverseTransformPoint(panelCenter); Vector3 localOffset = cp - cc; // Offset shouldn't occur if blocked if (!mScrollView.canMoveHorizontally) localOffset.x = 0f; if (!mScrollView.canMoveVertically) localOffset.y = 0f; localOffset.z = 0f; // Spring the panel to this calculated position SpringPanel.Begin(mScrollView.panel.cachedGameObject, panelTrans.localPosition - localOffset, springStrength).onFinished = onFinished; } else mCenteredObject = null; } /// <summary> /// Center the panel on the specified target. /// </summary> public void CenterOn(Transform target) { if (mScrollView != null && mScrollView.panel != null) { Vector3[] corners = mScrollView.panel.worldCorners; Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f; CenterOn(target, panelCenter); } }}
0 0
- NGUI 相片循环滚动效果
- NGUI Scroll View 循环滚动
- NGUI学习笔记(二):UIWrapCotent实现循环滚动(优化排行榜等效果)
- 循环滚动得效果
- 图片循环滚动效果
- Web_JavaScript_JS循环滚动效果;
- 图片循环滚动效果
- 【NGUI】Unity中ScrollView3D滚动效果
- 基于jquery循环滚动效果
- js文字循环滚动效果
- [Unity3d]NGUI- Scroll View实现触摸滚动相册效果
- NGUI|如何做出用鼠标滚动切换武器的效果
- 很简洁的循环滚动效果
- Flex 文本内容循环滚动效果
- 向上循环滚动的图片展示效果
- 新闻列表上下循环滚动效果
- jQuery 循环图片滚动切换效果代码
- div+js实现首尾相连循环滚动效果
- ios耗时操作
- Android下拉加载,上拉刷新的实现
- Redis学习记录之keyspace notification简析(二十三)
- 如何在指定的动作中加载 Yii-bootstrap 扩展
- JDBC连接各种数据库的方法
- NGUI 相片循环滚动效果
- socket一对一通信学习
- 《MongoDB in action》2012版 翻译 1.1
- 发送短信工具类(亿美短信平台接口)
- lldb调试命令之-基础篇
- c++ 程序设计 wee4 作业 大整数四则运算
- Git push保留空文件
- DataTables前端分页插件使用
- 2015 SegmentFault 黑客马拉松记录