unity 基于scrollRect实现翻页显示
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实现翻页显示的更多相关文章
- SharePoint 2013 自定义翻页显示列表项
项目需求:自定义开发一个能分页显示列表项的小部件,允许左右翻页,能根据用户权限来显示管理链接等. 效果如下: 技术要求:使用sharepoint rest API 来获取列表项,这样性能高,能够快速响 ...
- 使用ScrollRect制作翻页
1.标准的层级结构 ScrollRect->ViewPort->Content,Viewport负责显示区域的大小一般和Mask一起配合使用,Content使用Layout来布局,如果想使 ...
- Qt编写数据库通用翻页demo(开源)
在Qt与数据库结合编程的过程中,记录一多,基本上都需要用到翻页查看记录,翻页有个好处就是可以减轻显示数据的表格的压力,不需要一次性将数据库表的记录全部显示,也基本上没有谁在一页上需要一次性显示所有记录 ...
- 基于vue实现上下滑动翻页效果
18年年底的时候,一直在做年度报告的H5页面,因为项目需要,需要实现上下滑动翻页,并且上滑的页面比正常页面的比例要缩小一定比例. 效果类似于http://www.17sucai.com/pins/de ...
- 转载 vue-awesome-swiper - 基于vue实现h5滑动翻页效果
说到h5的翻页,很定第一时间想到的是swiper.但是我当时想到的却是,vue里边怎么用swiper?! 中国有句古话叫:天塌下来有个高的顶着. 在前端圈里,总有前仆后继的仁人志士相继挥洒着热汗(这里 ...
- 一款基于css3的3D图片翻页切换特效
今天给大家分享一款基于css3的3D图片翻页切换特效.单击图片下方的滑块会切换上方的图片.动起你的鼠标试试吧,效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id= ...
- 基于HTML5手机上下滑动翻页特效
基于HTML5手机上下滑动翻页特效.这是一款手机移动端触屏滑动翻页代码下载.效果图如下: 在线预览 源码下载 实现的代码. html代码: <section class="u-al ...
- combogrid翻页后保持显示内容为配置的textField解决办法
easyui的combogrid当配置pagination为true进行分页时,当datagrid加载其他数据页,和上一次选中的valueField不匹配时,会导致combogrid直接显示value ...
- 解决Hexo博客模板hexo-theme-next的翻页按钮不正常显示问题
用Hexo搭了个Gitpage的博客,兴冲冲的发了11篇博文后发现翻页按钮不正常显示,显示为<i class="fa fa-angle-right"></i> ...
随机推荐
- spring系列常用注解
常见注解使用 - @SpringBootApplication,springboot的核心注解,用于开启自动配置,等效于@Configuraion.@ComponentScan和@EnableAuto ...
- 如何增强VR的vection/self-motion?
上一节讲到了vection是给玩家带来“移动感”的因素,它提供良好VR体验的关键之一.那么VR中我们一般用哪些方式来提供vection呢?1 首先来简单了解一下人体和空间相关的感知机制. 视觉线索 v ...
- python的__name__ == \'__main__\' 意义
转自http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...
- 踩坑踩坑之Flask+ uWSGI + Tensorflow的Web服务部署
一.简介 作为算法开发人员,在算法模块完成后,拟部署Web服务以对外提供服务,从而将算法模型落地应用.本文针对首次基于Flask + uWSGI + Tensorflow + Nginx部署Web服务 ...
- 关于CSS的书写规范和顺序
关于CSS的书写规范和顺序,是大部分前端er都必须要攻克的一门关卡,如果没有按照良好的CSS书写规范来写CSS代码,会影响代码的阅读体验.这里总结了一个CSS书写规范.CSS书写顺序供大家参考,这些是 ...
- c++ 对特定进程的内存监控
在工具实现的过程中,遇到了内存爆了的问题,部分模型的规模可以达到10的100次方方甚至1000次方.(工具的主要算法涉及到了递归,递归深度会很深,所以也用到了ulimit修改栈空间来缓解爆栈的问题,治 ...
- Java-Thread00之多线程知识准备
------  ...
- [开源]如何使用goapp写你的后台管理系统_golang
简析 基于 Gin + GORM + Casbin + vue-element-admin 实现的权限管理系统. 基于Casbin 实现RBAC权限管理. 前端实现: vue-element-admi ...
- 清除SQL Server Management Studio的最近服务器列表
C:\Users\dell\AppData\Roaming\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin
- session,cookie,sessionStorage,localStorage的相关设置以及获取删除
一.cookie 什么是 Cookie? "cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用 JavaScrip ...