http://blog.csdn.net/luyuncsd123/article/details/22914497

最近在做一个项目的UI,需求是1、拖动items后当永远有一个item保存在中间位置,、点击当前item后当前item滑动到终点。 

咱看了NGUI的Scroll View例子后发现第一个要求NGUI自带了,第二个要求没有,所以自己写了个包含这2个需求的脚本。 

把相应的脚本替换成这几个就可以了。如果碰到变量保护之类的错误的话把父类中的方法改成protected就行了。 

using UnityEngine;
using System.Collections; [ExecuteInEditMode]
public class QRCUICenterOnChild : UICenterOnChild
{
private Transform moveTarget; protected QRCUIDraggablePanel mQrcDrag; void OnEnable() { Recenter(null); //Recenter();
} void OnDragFinished(GameObject obj)
{
if (enabled)
{
//if (obj != null)
Recenter(obj);
//else
// Recenter();
}
} /// <summary>
/// Recenter the draggable list on the center-most child.
/// </summary> public void Recenter(GameObject obj)
{
if (mQrcDrag == null)
{
mQrcDrag = NGUITools.FindInParents<qrcuidraggablepanel>(gameObject); if (mQrcDrag == null)
{
Debug.LogWarning(GetType() + " requires " + typeof(QRCUIDraggablePanel) + " on a parent object in order to work", this);
enabled = false;
return;
}
else
{
mQrcDrag.onDragFinished = OnDragFinished; //if (mDrag.horizontalScrollBar != null)
// mDrag.horizontalScrollBar.onDragFinished = OnDragFinished; //if (mDrag.verticalScrollBar != null)
// mDrag.verticalScrollBar.onDragFinished = OnDragFinished;
}
}
if (mQrcDrag.panel == null) return; // Calculate the panel's center in world coordinates
Vector4 clip = mQrcDrag.panel.clipRange;
Transform dt = mQrcDrag.panel.cachedTransform;
Vector3 center = dt.localPosition;
center.x += clip.x;
center.y += clip.y;
center = dt.parent.TransformPoint(center); // Offset this value by the momentum
Vector3 offsetCenter = center - mQrcDrag.currentMomentum * (mQrcDrag.momentumAmount * 0.1f);
mQrcDrag.currentMomentum = Vector3.zero; float min = float.MaxValue;
Transform closest = null;
Transform trans = transform; //Determine whether the user is to click on or drag
if (obj != null)
{
closest = obj.transform;
}
else {
closest = DetermineCloestChild(trans, offsetCenter, min, closest);
} // Spring the panel to this calculated position
MoveTargetPosition(closest, dt, center);
} /// <summary>
/// Determine the closest child
/// </summary>
public Transform DetermineCloestChild(Transform trans, Vector3 offsetCenter, float min, Transform closest)
{
for (int i = , imax = trans.childCount; i < imax; ++i)
{
Transform t = trans.GetChild(i);
float sqrDist = Vector3.SqrMagnitude(t.position - offsetCenter); if (sqrDist < min)
{
min = sqrDist;
closest = t;
}
}
return closest;
} /// <summary>
/// Spring the panel to this calculated position
/// </summary>
public void MoveTargetPosition(Transform closest, Transform dt, Vector3 center)
{
if (closest != null)
{
mCenteredObject = closest.gameObject; // Figure out the difference between the chosen child and the panel's center in local coordinates
Vector3 cp = dt.InverseTransformPoint(closest.position);
Vector3 cc = dt.InverseTransformPoint(center);
Vector3 offset = cp - cc; // Offset shouldn't occur if blocked by a zeroed-out scale
if (mQrcDrag.scale.x == 0f) offset.x = 0f;
if (mQrcDrag.scale.y == 0f) offset.y = 0f;
if (mQrcDrag.scale.z == 0f) offset.z = 0f; // Spring the panel to this calculated position
SpringPanel.Begin(mQrcDrag.gameObject, dt.localPosition - offset, 8f).onFinished = OnQrcFisished;
}
else mCenteredObject = null;
} public delegate void OnQrcMoveToTragetFisished(GameObject obj);
public event OnQrcMoveToTragetFisished OnQrcMoveToTragetFisishedHandler;
/// <summary>
/// After reaching the target this method will be used.
/// </summary>
public void OnQrcFisished() {
OnQrcMoveToTragetFisishedHandler(mCenteredObject);
} } ==================================================分割线================================================== using UnityEngine;using System.Collections; [ExecuteInEditMode]
[RequireComponent(typeof(UIPanel))]public class QRCUIDraggablePanel : UIDraggablePanel
{ public delegate void OnDragFinished(GameObject obj); /// <summary> /// Event callback to trigger when the drag process finished. Can be used for additional effects, such as centering on some object.
/// </summary> public OnDragFinished onDragFinished; public void Press(bool pressed, GameObject obj)
{ if (enabled && NGUITools.GetActive(gameObject))
{ if (!pressed && mDragID == UICamera.currentTouchID) mDragID = -; mCalculatedBounds = false;
mShouldMove = shouldMove; if (!mShouldMove) return;
mPressed = pressed; if (pressed) {
// Remove all momentum on press mMomentum = Vector3.zero;
mScroll = 0f; // Disable the spring movement DisableSpring(); // Remember the hit position
mLastPos = UICamera.lastHit.point; // Create the plane to drag along mPlane = new Plane(mTrans.rotation * Vector3.back, mLastPos);
} else
{ if (restrictWithinPanel && mPanel.clipping != UIDrawCall.Clipping.None && dragEffect == DragEffect.MomentumAndSpring)
{ RestrictWithinBounds(false);
} if (onDragFinished != null) onDragFinished(obj);
} }
}} ===============================================分割线================================================ using UnityEngine;using System.Collections; [ExecuteInEditMode]
public class QRCUIDragPanelContents : UIDragPanelContents{ private Vector3 previousTouch; /// <summary>
/// Create a plane on which we will be performing the dragging. /// </summary>
protected void OnPress(bool pressed) {
bool isClick = false; Vector3 currentPos = Vector3.zero; //Judge runtime platform
if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
if (Input.touchCount > ) {
currentPos = Input.GetTouch().position; }
} else {
currentPos = Input.mousePosition; } //Calculation is click ?
if (pressed) {
previousTouch = currentPos; }
else { if (Vector3.Distance(previousTouch, currentPos) < 20.0f) { isClick = true; }
} if (enabled && NGUITools.GetActive(gameObject) && draggablePanel != null) {
QRCUIDraggablePanel qrcDraggablePanel = draggablePanel as QRCUIDraggablePanel;
if (isClick) {
qrcDraggablePanel.Press(false, gameObject); }
else { qrcDraggablePanel.Press(pressed, null);
} } isClick = false;
} private bool IsClick() { if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
if (Input.touchCount > ) {
previousTouch = Input.GetTouch().position; }
} return true;
} //protected void OnClick() //{
// if (enabled && NGUITools.GetActive(gameObject) && draggablePanel != null) // {
// QRCUIDraggablePanel qrcDraggablePanel = draggablePanel as QRCUIDraggablePanel; // qrcDraggablePanel.Press(false, gameObject);
// } //} }

NGUI例子Scroll View场景中item添加点击后自动滑到终点的更多相关文章

  1. NGUI之scroll view制作,以及踩的坑总结

    http://blog.csdn.net/monzart7an/article/details/23878505 链接: http://game.ceeger.com/forum/read.php?t ...

  2. NGUI之scroll view的制作和踩坑总结

    之前也看了不少童鞋谢了关于NGUI的scroll view的制作下面我写下自己的制作过程以及心得,希望对童鞋们有所帮助.1.首先建立一个960*640的背景参考http://game.ceeger.c ...

  3. Unity NGUI制作scroll view

    unity版本:4.5 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/monzart7an/article/details/23878505,作者:CSDN 冬菊子   ...

  4. 第十五章、Model/View架构中Item Views部件的父类

    老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...

  5. unity NGUI点击消息不传入到场景中去

    unity NGUI点击消息不传入到场景中去 1.今天遇到的问题是点击NGUI的按钮,场景中也相应了这个消息 解决的办法是在场景中需要互动的时候,也就是在update中进行判断 是否是点击了NGUI按 ...

  6. NGUI系列教程十(Scroll View实现触摸滚动相册效果)

    NGUI中提供了两种Scroll View 一种是通过手指或鼠标滑动视图时移动平面物体,另一种则是直接移动摄像机,他们各有各的好处.但是NGUI提供的Scroll View很难实现类似Android ...

  7. 关于Unity中stretch的分开使用、预制体、Scroll View的UI节点

    一.上次讲的菊花的四个花瓣,只讲了四个花瓣和在一起的时候的作用,现在是分开的菊花的四个花瓣的作用 1.创建一个Canvas2.对Canvas进行初始化3.创建一个Image的UI节点作为Canvas的 ...

  8. Unity3D NGUI制作的Button放到场景中,按钮从2D变到3D

    通常我们使用Button都是在UI界面,即NGUI的摄像机下,如果想换到场景中,即不让按钮以UI形式显现,而是和场景中的物体一起随着摄像机移动而缩小,放大. 很简单,把Button从NGUi的摄像机中 ...

  9. UGUI 用手柄或者键盘控制选择Scroll View中的游戏对象时,滚动条跟着移动

    原预制体以及脚本的下载地址:https://download.csdn.net/download/qq_15017279/10404010 1.新建一个Scroll View,删掉横向的滚动条,并且把 ...

随机推荐

  1. Android实现两个ScrollView互相联动,同步滚动的效果

    公众号:smart_android 作者:loonggg 点击"阅读原文",可查看更多内容和干货 最近在做一个项目,用到了两个ScrollView互相联动的效果,简单来说联动效果意 ...

  2. 第四十一课:CSS3 animation详解

    animation是css3的另一个重要的模块,它成型比transition晚,吸取了Flash的关键帧的理念,实用性高. animation是一个复合样式,它可以细分为8个更细的样式. (1)ani ...

  3. 序列化类型为“System.Data.Entity.DynamicProxies.ActionInfo_”的对象时检测到循环引用。

    解决方案: 加上 db.Configuration.ProxyCreationEnabled = false;这句话搞定~

  4. Daily Scrum – 1/11

    Meeting Minutes 发现了一个新的bug,即当背诵单词过多时,会出现统计信息超出文字框的现象: 更新了tfs,明白了打包的方式: Burndown     Progress   part ...

  5. Tomcat 在win7/win8 系统下tomcat-users.xml.new(拒绝访问)解决方法

    tomcat启动报错No UserDatabase component found under key UserDatabase 也可以这样处理 Tomcat 在win7/win8 系统下tomcat ...

  6. Oracle创建表格报ORA-00906:缺失左括号错误解决办法

    来源于:http://www.linuxidc.com/Linux/2013-06/85297.htm 解决办法: create table myTable(id number(5,2),name v ...

  7. Linux 进程管理器 supervixor

    使用 supervisor 管理进程 http://www.cnblogs.com/smail-bao/p/5673434.html http://ju.outofmemory.cn/entry/20 ...

  8. IntelliJ IDEA 设置 编辑器字体大小

    1,打开File->settings 2,在Edit->colors->Fonts下创建新字体 保存即可.

  9. Pro Git 读书笔记

    一. 起步 1. 集中式版本控制缺点:中央服务器的单点故障. 分布式版本控制优点:客户端并不只提取最新版本的文件快照,而是把代码仓库完整地镜像下来. 这么一来,任何一处协同工作用的服务器发生故障,事后 ...

  10. 【BZOJ 1036】【ZJOI 2008】树的统计 树链剖分模板题

    sth神犇的模板: //bzoj1036 题目:一个n个点的树每个点有一个权值,支持修改单点权值,求某两点路径上的点权和或最大点权. #include <cstdio> using nam ...