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> ...
随机推荐
- 9 个基于JavaScript 和 CSS 的 Web 图表框架
COMSHARP CMS 写道:jQuery, MooTools, Prototype 等优秀的 JavaScript 框架拥有各种强大的功能,包括绘制 Web图表,使用这些框架以及相应插件,我们可以 ...
- 4.7 if else-if
c#中的if else-if if else-if 中最后的"else":如果用户输入的不等于上面的else if(xxx)表达式,则输出这行代码. **不参与运算的数值不用转换 ...
- mybatis <=或这个>=提示错误Tag name expecte问题解决
解决方案: 1.将<号或者>号进行转义 DATE_SUB(CURDATE(), INTERVAL 31 DAY) <= DATE(created) 2.使用<![CDATA[ ...
- CSS3相关编码规范
一.CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin) ...
- eShopOnContainers学习系列(二):数据库连接健康检查
项目里使用数据库的时候,我们有时候需要知道数据库当前的健康状态,特别是当数据库连接不上的时候能够立马获悉.eShopOnContainers里存在着大量的服务健康.连接健康的检查,数据库连接是其中之一 ...
- (七十九)c#Winform自定义控件-导航菜单
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- [转] Julia 高性能动态编程语言入门
本文共 851字,阅读大约需要 3分钟 ! 概 述 Julia 是一个 "全新"的高性能动态编程语言,前两天迎来了其 1.0 正式版的重大更新.Julia集 Python.C.R. ...
- iOS 设备数据管理工具 iMazing v2.10.3 绿色便携版
iMazing 是一款可以帮助用户管理 iOS 设备的软件,功能远远超出 iTunes.iMazing 连接你的 iOS 设备(iPhone. iPad 或 iPod)相连,使用起来也非常的方便.你可 ...
- POJ - 2393Yogurt factory
The cows have purchased a yogurt factory that makes world-famous Yucky Yogurt. Over the next N (1 &l ...
- Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)
介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...