NGUI例子Scroll View场景中item添加点击后自动滑到终点
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添加点击后自动滑到终点的更多相关文章
- NGUI之scroll view制作,以及踩的坑总结
http://blog.csdn.net/monzart7an/article/details/23878505 链接: http://game.ceeger.com/forum/read.php?t ...
- NGUI之scroll view的制作和踩坑总结
之前也看了不少童鞋谢了关于NGUI的scroll view的制作下面我写下自己的制作过程以及心得,希望对童鞋们有所帮助.1.首先建立一个960*640的背景参考http://game.ceeger.c ...
- Unity NGUI制作scroll view
unity版本:4.5 NGUI版本:3.6.5 参考链接:http://blog.csdn.net/monzart7an/article/details/23878505,作者:CSDN 冬菊子 ...
- 第十五章、Model/View架构中Item Views部件的父类
老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...
- unity NGUI点击消息不传入到场景中去
unity NGUI点击消息不传入到场景中去 1.今天遇到的问题是点击NGUI的按钮,场景中也相应了这个消息 解决的办法是在场景中需要互动的时候,也就是在update中进行判断 是否是点击了NGUI按 ...
- NGUI系列教程十(Scroll View实现触摸滚动相册效果)
NGUI中提供了两种Scroll View 一种是通过手指或鼠标滑动视图时移动平面物体,另一种则是直接移动摄像机,他们各有各的好处.但是NGUI提供的Scroll View很难实现类似Android ...
- 关于Unity中stretch的分开使用、预制体、Scroll View的UI节点
一.上次讲的菊花的四个花瓣,只讲了四个花瓣和在一起的时候的作用,现在是分开的菊花的四个花瓣的作用 1.创建一个Canvas2.对Canvas进行初始化3.创建一个Image的UI节点作为Canvas的 ...
- Unity3D NGUI制作的Button放到场景中,按钮从2D变到3D
通常我们使用Button都是在UI界面,即NGUI的摄像机下,如果想换到场景中,即不让按钮以UI形式显现,而是和场景中的物体一起随着摄像机移动而缩小,放大. 很简单,把Button从NGUi的摄像机中 ...
- UGUI 用手柄或者键盘控制选择Scroll View中的游戏对象时,滚动条跟着移动
原预制体以及脚本的下载地址:https://download.csdn.net/download/qq_15017279/10404010 1.新建一个Scroll View,删掉横向的滚动条,并且把 ...
随机推荐
- 同步git修改文件到远端服务器脚本
#!/usr/bin/perl -w @files=`git status -s` ; @sync_files = (); foreach (@files) { ); # 固定前2个字符为状态 + 1 ...
- [BZOJ 1297][SCOI 2009]迷路(矩阵快速幂)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1297 分析:如果每条边的边权都是1,那么就相当于对邻接矩阵自乘T次(因为写一下递推式子 ...
- “耐撕”团队 2016.03.30 站立会议
1. 时间: 16:45--17:05 总计:20分钟 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...
- Web前端开发Chrome插件
参考:http://www.cnblogs.com/sosoft/p/3490481.html 越来越多的前端开发人员喜欢在Chrome里开发调试代码,Chrome有许多优秀的插件可以帮助前端开发人员 ...
- Eclipse_调试技巧
一.使用Display视图实时计算变量结果(带智能提示) windows-->show view-->display http://stackoverflow.com/questions ...
- Eclipse属性文件编辑器---Properties Editor
今天在用 Eclipse 来编辑 .properties 文件时,写的中文会自动转为 Unicode 编码,完全不知道自己的中文写的是什么!! 于是查了一下,网上推荐,在Eclipse 中 安装一个 ...
- 【ZOJ 3844】Easy Task
题意 每次把序列中最大的数a的一个和最小的数b的一个变成a-b.求最后是否能使序列里的数全部相同,能则输出这个相同的数. 分析 一定是有解的,不断减少最大数的个数,最大数减少为0个时,就是减少了不同数 ...
- BZOJ-1934 Vote 善意的投票 最大流+建图
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1551 Solved: 951 [Submit][S ...
- POJ1995(整数快速幂)
http://poj.org/problem?id=1995 题意:求(A1^B1 + A2^B2 + .....Ah^Bh)%M 直接快速幂,以前对快速幂了解不深刻,今天重新学了一遍so easy ...
- POJ1860Currency Exchange(Bellman + 正权回路)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23938 Accepted: 867 ...