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. 使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示

    使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示 写在前面: 有的小伙伴,会吐槽啦,你这个标题有点长的啊.哈哈 ,好像是的!不过,这个也是本次案例中使用到的关键技术 ...

  2. Hadoop核心组件之HDFS

    HDFS:分布式文件系统 一句话总结 一个文件先被拆分为多个Block块(会有Block-ID:方便读取数据),以及每个Block是有几个副本的形式存储 1个文件会被拆分成多个Block blocks ...

  3. python实现感知机线性分类模型

    前言 感知器是分类的线性分类模型,其中输入为实例的特征向量,输出为实例的类别,取+1或-1的值作为正类或负类.感知器对应于输入空间中对输入特征进行分类的超平面,属于判别模型. 通过梯度下降使误分类的损 ...

  4. frp 端口映射

    简介 frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp 协议,为 http 和 https 应用协议提供了额外的能力,且尝试性支持了点对点穿透. 场景 利用处于内网或防火墙后 ...

  5. 死磕 java同步系列之zookeeper分布式锁

    问题 (1)zookeeper如何实现分布式锁? (2)zookeeper分布式锁有哪些优点? (3)zookeeper分布式锁有哪些缺点? 简介 zooKeeper是一个分布式的,开放源码的分布式应 ...

  6. Spring5源码解析-前奏:本地构建Spring5源码

    构建环境 macOS 10.13.6 JDK1.8 IntelliJ IDEA 2018.3.6 (Ultimate Edition) Spring v5.1.9.RELEASE Gradle 5.5 ...

  7. [LeetCode] 470. Implement Rand10() Using Rand7()

    Given a function rand7 which generates a uniform random integer in the range 1 to 7, write a functio ...

  8. shell命令大全笔记

    ## -print 将匹配的文件输出到标准输出## -exec 将匹配的文件执行该参数所给出的shell命令## -ok 将匹配的文件执行该参数所给出的shell命令,每次执行命令有提示 #----- ...

  9. Java8 Period.between方法坑及注意事项

    在使用Java8 新特性中关于Period.between的方法时需注意该方法获取日期的区间问题. @Test public void test1(){ LocalDate from = LocalD ...

  10. e课表项目第二次冲刺周期第九天

    昨天完成了什么? 昨天,我查找了相关的资料,将数据库根据我们的课程信息进行了重新的设计,并将数据能够连上数据库,即在添加课程的界面,可以将添加的课程的信息,存储到数据库中,并且存储到课程表中,并注明是 ...