使用ScrollRect制作翻页
1.标准的层级结构 ScrollRect->ViewPort->Content,Viewport负责显示区域的大小一般和Mask一起配合使用,Content使用Layout来布局,如果想使用代码来自动定位显示位置需要在Content加上Content size filter.
2.ScrollRectHelper
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using System; public class ScrollRectHelper : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
// 滑动速度
public float smooting = ; // 每页显示的项目
[SerializeField]
private int countPerPage = ; ScrollRect srect;
// 总页数
float totalPages;
// 是否拖拽结束
bool isDrag = false;
// 总页数索引比列 0-1
List<float> listPageValue = new List<float> { };
// 滑动的目标位置
public float targetPos = ;
// 当前位置索引
float nowindex = ; void Awake()
{
srect = GetComponent<ScrollRect>();
} public string PageText()
{
return (nowindex + ) + "/" + (totalPages + );
} // 计算每页比例
public void CalcListPageValue<T>() where T : MonoBehaviour
{
T[] items = srect.content.GetComponentsInChildren<T>();
srect.content.rect.Set(srect.content.rect.width / , srect.content.rect.y, srect.content.rect.width, srect.content.rect.height);
totalPages = (int)(Math.Ceiling((float)items.Length / countPerPage) - );
if (items.Length != )
{
for (float i = ; i <= totalPages; i++)
{
//Debug.Log(i / totalPages);
listPageValue.Add((i / totalPages));
}
}
} void Update()
{
if (!isDrag)
{
srect.horizontalNormalizedPosition = Mathf.Lerp(srect.horizontalNormalizedPosition, targetPos,
Time.deltaTime * smooting);
} // Debug
if (Input.GetKeyDown(KeyCode.LeftArrow)) PressLeft();
if (Input.GetKeyDown(KeyCode.RightArrow)) PressRight();
} /// <summary>
/// 拖动开始
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
} /// <summary>
/// 拖拽结束
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
isDrag = false;
var tempPos = srect.horizontalNormalizedPosition; //获取拖动的值
var index = ;
float offset = Mathf.Abs(listPageValue[index] - tempPos); //拖动的绝对值
for (int i = ; i < listPageValue.Count; i++)
{
float temp = Mathf.Abs(tempPos - listPageValue[i]);
if (temp < offset)
{
index = i;
offset = temp;
}
}
targetPos = listPageValue[index];
nowindex = index;
} public void PressLeft()
{
nowindex = Mathf.Clamp(nowindex - , , totalPages);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
} public void PressRight()
{
nowindex = Mathf.Clamp(nowindex + , , totalPages);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
}
}
使用ScrollRect制作翻页的更多相关文章
- unity 基于scrollRect实现翻页显示
unity 基于scrollRect实现翻页显示,并定为到某一页,而不是某一页的中间方法(第二个脚本采用实际位置计算,并在update里实现平滑过渡): 组场景时,经常需要获取鼠标(或者点击)开始结束 ...
- css 制作翻页布局
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- [Qt] CFlip 翻页功能实现
由于需要给table制作翻页功能,所以写了一个翻页的类. 看上去总体效果感觉还是不错的,哈哈. //flip.h #ifndef CFLIP_H #define CFLIP_H #include &l ...
- 简单3D翻页相册制作教程
3D效果看起来总是要比平面的图形看起来视觉效果要好的多,今天来教大家制作简单的3D翻页效果的视频. 视频预览链接:https://v.youku.com/v_show/id_XMzgxOTY5NzQz ...
- Android电子书翻页效果实现
这篇文章是在参考了别人的博客基础上,修改了其中一个翻页bug,并且加了详细注释 先看效果 其中使用了贝赛尔曲线原理,关于贝赛尔曲线的知识,推荐大家看下http://blog.csdn.net/hmg2 ...
- HTML5开发的翻页效果实例
简介2010年F-i.com和Google Chrome团队合力致力于主题为<20 Things I Learned about Browsers and the Web>(www.20t ...
- ios 视图切换翻页效果
本文写的是视图切换,涉及到的内容有 1.实现代码添加Navigation Bar Toolbal: 2.实现在Navigation Bar和Toolbar上用代码添加Bar Button Item: ...
- Laya List翻页滚动方案 & List滚动源码解析
Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...
- 简易数据分析 10 | Web Scraper 翻页——抓取「滚动加载」类型网页
这是简易数据分析系列的第 10 篇文章. 友情提示:这一篇文章的内容较多,信息量比较大,希望大家学习的时候多看几遍. 我们在刷朋友圈刷微博的时候,总会强调一个『刷』字,因为看动态的时候,当把内容拉到屏 ...
随机推荐
- Python——捕获异常
一.什么是异常 """异常:错误,bug处理异常:尝试执行某句可能出现异常的语句, 若出错则用正确的代码去替代. try: 可能发生错误的代码except: 如果出现异常 ...
- 记一个开发是遇到的坑之Oralce 字符串排序
简单描述一下情况,就是存储过程中用一个字符串类型的字段作为患者就诊的排序号,结果莫名发现叫完1号后叫了11.12等患者.用户的反馈不一定准确,自己加了日志的,赶紧拷贝日志来观察一下.结果发现实际情况就 ...
- 浅谈 vue-loader---合格前端
什么是 vue-loader? vue-loader 是一个 webpack 的 loader,它允许你以一种名为单文件组件的格式撰写 Vue 组件. 如何使用? 1. 安装 npm install ...
- P2918 [USACO08NOV]买干草Buying Hay
链接:Miku ---------------- 这就是一个完全背包的板子题 ---------------- 我们把重量当作重量,开销当作价值,那么这个题就是个求价值最小的完全背包 然而题目上说了是 ...
- 简单聊聊CSS中的3D技术之“立方体”
简单聊聊CSS中的3D技术之“立方体” 大家好,我是今天的男一号,我叫小博主. 今天来聊一下我在前端“逆战班”学习中遇到的颇为有趣的3D知识.前端学习3周,见识稀疏,在下面的分享中如有不对的地方请大家 ...
- Git常用命令 - 随时更新
1. 配置用户信息 git config --global user.name <name> git config --global user.email <email_addres ...
- 安装MongoDB到CentOS(YUM)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:mongodb-org-4.0.8 硬件要求:无 安装过程 1.配置YUM-Mongodb存储库 ...
- Bazinga HDU - 5510【技巧暴力+字符串】
题目:https://vjudge.net/problem/HDU-5510 $2015ACM/ICPC$ 亚洲区沈阳站 题目大意: 输入$t$(表示样例个数) 如何每个样例一个 $n$,表示字符串的 ...
- 视频格式转换mp4
第一步:https://ffmpeg.zeranoe.com/builds/下载ffmpeg 或者:百度云下载: 链接:https://pan.baidu.com/s/1x_QogbV8xFjkYTe ...
- 提取 Microsoft.ReportViewer等dll
ReportViewer 在开发环境没问题 发布以后可能会提示找不到 Microsoft.ReportViewer 下的几个dll 可以用用下面脚本在开发服务器上提取 相应的dll @SET dest ...