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,删掉横向的滚动条,并且把 ...
随机推荐
- 内置模块加载器(commonjs规范)的使用
index9.html <html><head> <title>模块加载器</title> <script src="jquery-1. ...
- Bootstrap系列 -- 24. 下拉菜单基本用法
在使用Bootstrap框架的下拉菜单时,必须调用Bootstrap框架提供的bootstrap.js文件.当然,如果你使用的是未编译版本,在js文件夹下你能找到一个名为“dropdown.js”的文 ...
- AngularJs-指令和指令之间的交互(动感超人)
前言: 上节我们学习到了指令和控制器之间的交互,通过给指令添加动作,调用了控制器中的方法.本节我们学习指令和指令之间是如何交互的,我们通过一个小游戏来和大家一起学习,听大漠老师说这是国外的人写的dem ...
- Xamarin.Forms——WebView技术研究
在Xamarin中有一些Forms原生不太好实现的内容可以考虑使用HTML.Javascript.CSS那一套前端技术来实现,使用WebView来承载显示本地或网络上的HTML文件.不像OpenUri ...
- HTML布局篇之双飞翼(圣杯)布局
最近在写页面的时候,总是为布局头疼,倒不是不能布出来,就是感觉不系统,没有成一个体系的感觉.所以决定自己写博文,梳理一下思路. 常用的布局方式大致可以分为三种: 浮动布局 Float 负边距(双飞翼) ...
- CXF WebService 资料收集
Java Web 服务专题 :http://www.ibm.com/developerworks/cn/java/j-ws/ APACHE CXF官网 :http://cxf.apache.org/d ...
- Java基础-父类-子类执行顺序
代码解析 子类 package com; /** * 子类 * @author huage * */ public class Test extends Test1{ public static vo ...
- selenium ide 录制回放link链接报错
回放是出现以下错误: 也就是回放点击打开新的链接时出现这个错误, 这个问题说的是 点击此链接会新打开一个窗口 selenium1是不支持多窗口切换的 因此会卡在这里,也就录制不支持这个操作,但是很多书 ...
- Unix/Linux 命令技巧
锁定一个文件夹 为了我的数据隐私,我想要锁定我文件服务器下的/downloads文件夹.因此我运行了: chmod 0000 /downloads root用户仍旧可以访问,而ls和cd命令则不工作. ...
- 【poj1067】 取石子游戏
http://poj.org/problem?id=1067 (题目链接) 题意 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走 ...