unity 基于scrollRect实现翻页显示,并定为到某一页,而不是某一页的中间方法(第二个脚本采用实际位置计算,并在update里实现平滑过渡):

组场景时,经常需要获取鼠标(或者点击)开始结束时讯息(包括开始拖动时的事件以及对应的坐标),unity为我们提供了两个接口来实现相关方法之前在Unity基础命令中也提到过。

在此在重申一下,两个接口对应两个方法,即可以对应开始结束拖动时的事件,数据则为eventData中。

 public void OnBeginDrag(PointerEventData eventData)
public void OnEndDrag(PointerEventData eventData)
public class NewMove : MonoBehaviour,IBeginDragHandler,IEndDragHandler {

    private ScrollRect scrollRect;
public Toggle[] toggleArr;
//存储特定的位置坐标
private float[] pageArr=new float[]{,0.5f,1.0f};
public void OnBeginDrag(PointerEventData eventData)
{
// Debug.Log("Begin:");
// Debug.Log(eventData.position);
//获取rect的初始坐标值
Vector2 pos = scrollRect.normalizedPosition;
Debug.Log("Begin:"+pos);
}
public void OnEndDrag(PointerEventData eventData)
{
//Debug.Log("End:");
//Debug.Log(eventData.position);
//获取rect的拖动后的坐标值
Vector2 pos = scrollRect.normalizedPosition;
Debug.Log("End:"+pos);
//获取rect拖动后的水平坐标值
float posX = scrollRect.horizontalNormalizedPosition;
int index = ;
//计算与特定点的偏差
float offset = Math.Abs(pageArr[index] - posX);
//与每一个坐标点进行比较,确定应该存在的位置
//偏差最小的位置即为rect的位置点
for(int i=;i<pageArr.Length;i++)
{
float newOffset = Math.Abs(pageArr[i] - posX);
if(newOffset<= offset)
{
index = i;
offset = newOffset;
}
}
scrollRect.horizontalNormalizedPosition = pageArr[index];
toggleArr[index].isOn = true;
Debug.Log("End:" + scrollRect.horizontalNormalizedPosition);
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events; public class DragController : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
[SerializeField]
private GameObject scrollRect;
[SerializeField]
private GameObject scrollRectSmall; public float distance = 600f;
public int smoothing = ;
public float deltaWidth = 3840f;
public float deltaSmallWidth = 95f; private List<GameObject> pages;
private List<GameObject> smallScreens;
private float horizontalPos = ;
private float horizontalPosSmall = ;
private List<float> postions = new List<float>(); private bool onDrag = false;
private float beginPos = ;
private float endPos = ;
private int pageIndex = ; public void Init(List<GameObject> pages)
{
this.pages = new List<GameObject>(pages);
//this.smallScreens = smallScreens;
pageIndex = ;
horizontalPos = ;
horizontalPosSmall = ;
//for (int i=0;i<pages.Count;i++)
//{
// postions.Add(i);
//}
} public void OnBeginDrag(PointerEventData eventData)
{
onDrag = true;
beginPos = eventData.position.x;
} public void OnEndDrag(PointerEventData eventData)
{
onDrag = false;
endPos = eventData.position.x;
float newDis = endPos - beginPos; if (newDis < -distance)
{
pageIndex++;
if (pageIndex >= pages.Count)
{
pageIndex = pages.Count - ;
}
}
if(newDis > distance)
{
pageIndex--;
if(pageIndex < )
{
pageIndex = ;
}
}
horizontalPos = -deltaWidth * pageIndex;
//print(horizontalPos);
horizontalPosSmall = -deltaSmallWidth * pageIndex;
//print(horizontalPosSmall);
pages[pageIndex].GetComponent<Toggle>().isOn = true;
//smallScreens[pageIndex].GetComponent<Toggle>().isOn = true;
} // Use this for initialization
void Start () {
float scale = 3840f / Screen.width;
deltaWidth = deltaWidth / scale;
} // Update is called once per frame
void Update () {
if (!onDrag)
{
//scrollRect.GetComponent<rec = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
// horizontalPos, smoothing * Time.deltaTime);
Vector2 pos = scrollRect.GetComponent<RectTransform>().position;
float xPos = Mathf.Lerp(pos.x, horizontalPos, Time.deltaTime * smoothing);
scrollRect.GetComponent<RectTransform>().position = new Vector2(xPos, pos.y);
//print(xPos + " " + horizontalPos); //Vector2 posS = scrollRectSmall.GetComponent<RectTransform>().anchoredPosition;
//float xPosS = Mathf.Lerp(posS.x, horizontalPosSmall, Time.deltaTime * smoothing);
//scrollRectSmall.GetComponent<RectTransform>().anchoredPosition = new Vector2(xPosS, posS.y);
}
}
}

unity 基于scrollRect实现翻页显示的更多相关文章

  1. SharePoint 2013 自定义翻页显示列表项

    项目需求:自定义开发一个能分页显示列表项的小部件,允许左右翻页,能根据用户权限来显示管理链接等. 效果如下: 技术要求:使用sharepoint rest API 来获取列表项,这样性能高,能够快速响 ...

  2. 使用ScrollRect制作翻页

    1.标准的层级结构 ScrollRect->ViewPort->Content,Viewport负责显示区域的大小一般和Mask一起配合使用,Content使用Layout来布局,如果想使 ...

  3. Qt编写数据库通用翻页demo(开源)

    在Qt与数据库结合编程的过程中,记录一多,基本上都需要用到翻页查看记录,翻页有个好处就是可以减轻显示数据的表格的压力,不需要一次性将数据库表的记录全部显示,也基本上没有谁在一页上需要一次性显示所有记录 ...

  4. 基于vue实现上下滑动翻页效果

    18年年底的时候,一直在做年度报告的H5页面,因为项目需要,需要实现上下滑动翻页,并且上滑的页面比正常页面的比例要缩小一定比例. 效果类似于http://www.17sucai.com/pins/de ...

  5. 转载 vue-awesome-swiper - 基于vue实现h5滑动翻页效果

    说到h5的翻页,很定第一时间想到的是swiper.但是我当时想到的却是,vue里边怎么用swiper?! 中国有句古话叫:天塌下来有个高的顶着. 在前端圈里,总有前仆后继的仁人志士相继挥洒着热汗(这里 ...

  6. 一款基于css3的3D图片翻页切换特效

    今天给大家分享一款基于css3的3D图片翻页切换特效.单击图片下方的滑块会切换上方的图片.动起你的鼠标试试吧,效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id= ...

  7. 基于HTML5手机上下滑动翻页特效

    基于HTML5手机上下滑动翻页特效.这是一款手机移动端触屏滑动翻页代码下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <section class="u-al ...

  8. combogrid翻页后保持显示内容为配置的textField解决办法

    easyui的combogrid当配置pagination为true进行分页时,当datagrid加载其他数据页,和上一次选中的valueField不匹配时,会导致combogrid直接显示value ...

  9. 解决Hexo博客模板hexo-theme-next的翻页按钮不正常显示问题

    用Hexo搭了个Gitpage的博客,兴冲冲的发了11篇博文后发现翻页按钮不正常显示,显示为<i class="fa fa-angle-right"></i> ...

随机推荐

  1. Spring boot 梳理 - 代码结构(Main类的位置)

    Spring boot 对代码结构无特殊要求,但有个套最佳实践的推荐 不要使用没有包名的类.没有包名时,@ComponentScan, @EntityScan, or @SpringBootAppli ...

  2. 【SQL server初级】SQL索引(一)

    SQL索引[一](此文章为“数据库性能优化二:数据库表优化”附属文章之一) SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开 ...

  3. 06-border

    边框 border:边框,描述盒子的边框 边框的三要素:粗细 线性样式 颜色 例如:border:1px solid red: 如果颜色不写,默认是黑色:如果粗细不写,不显示边框:如果只写线性样式,默 ...

  4. Django之使用haystack+whoosh实现搜索功能

    为了实现项目中的搜索功能,我们使用的是全文检索框架haystack+搜索引擎whoosh+中文分词包jieba 安装和配置 安装所需包 pip install django-haystack pip ...

  5. 如何增强VR的vection/self-motion?

    上一节讲到了vection是给玩家带来“移动感”的因素,它提供良好VR体验的关键之一.那么VR中我们一般用哪些方式来提供vection呢?1 首先来简单了解一下人体和空间相关的感知机制. 视觉线索 v ...

  6. Spring系列(六):Spring事务源码解析

    一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...

  7. python编程基础之二十九

    栈和队列: 栈:先进后出,其他没多少特别之处了,一般可以用列表模拟栈,也可以用双端队列,封死一端. 队列:先进先出,也可以用列表模拟,但是一般用库函数,需要导collections 包:主要是为了解决 ...

  8. java集合类之ArrayList详解

    一.ArrayList源码分析 1.全局变量 (1)默认容量(主要是通过无参构造函数创建ArrayList时第一次add执行扩容操作时指定的elementData的数组容量为10) private s ...

  9. Cocos2d-x入门之旅[1]场景

    在游戏开发过程中,你可能需要一个主菜单,几个关卡和一个END的界面,如何组织管理这些东西呢? 和其他游戏引擎类似,Cocos也使用了场景(Scene) 这个概念 试想象一部电影或是番剧,你不难发现它是 ...

  10. nextjs:如何将静态资源发布到 CDN

    nextjs 是基于 react 的服务端同构指出框架,在使用的过程中也多多少少遇到过几个问题,其中最大的问题就是静态资源的发布了. 1. 如何基于文件内容进行 hash 命名 Next.js use ...