效果图

用的是UGUI

我先说思路

通过判断元素的位置信息来改变Hierarchy的顺序 实现无限滚动

改变位置的同时也要不断的调整Content的位置防止乱跳

元素锁定就是直接锁死的元素的移动范围 当只有拖动大于一定程度时才会发生改变

然后是面板设置

整体结构是这样子的

需要注意的是Content需要的两个组件

Content的爸爸只需要一个脚本

大小改变曲线(大致就行)

颜色渐变曲线

最后是脚本

 using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; public class DateControl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public enum ItemType { _year, _month, _day } public ItemType _itemtype; RectTransform conentRect; RectTransform targetRec; Vector3 oldDragPos; Vector3 newDragPos; public AnimationCurve curve_scale;//改变大小曲线
public AnimationCurve curve_color;//渐变效果曲线 List<Text> textList = new List<Text>(); Button testBtn; float
itemHeight, //子项item的高
contentParentHeight, //Content爸爸的高
itemNum, //子项数量
itemHeight_min, //子项最小发生改变位置
itemHeight_max, //子项最大发生改变位置
conentLimit, //Conent纠正位置
conentSpacing; //子项间隔大小 float deltaX, deltaY; [HideInInspector]
public static int _year, _month, _day; [HideInInspector]
int dateItemNum; Color itemColor_hig = new Color32(, , , ); void Awake() {
conentRect = transform.FindChild("Content").GetComponent<RectTransform>();
targetRec = transform.parent.FindChild("HighlightTarget").GetComponent<RectTransform>(); } void OnEnable() {
ItemList();
} void Start() {
switch (_itemtype) {
case ItemType._year: InstantiateData(, ); break;
case ItemType._month: InstantiateData(, ); break;
case ItemType._day: InstantiateData(, ); break;
} itemNum = transform.FindChild("Content").childCount - ; contentParentHeight = conentRect.parent.GetComponent<RectTransform>().sizeDelta.y; conentSpacing = conentRect.GetComponent<VerticalLayoutGroup>().spacing / ; itemHeight = textList[].rectTransform.sizeDelta.y + conentSpacing; if (itemNum % == ) conentLimit = (itemHeight + ) / ; else conentLimit = ; conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentLimit); deltaX = textList[].GetComponent<RectTransform>().sizeDelta.x;
deltaY = textList[].GetComponent<RectTransform>().sizeDelta.y; Invoke("ItemList", 0.05f); } /// <summary>
/// 生成子项item
/// </summary>
/// <param name="itemNum">子项数量</param>
/// <param name="dat">子项最大值</param>
void InstantiateData(int itemNum, int dat) {
GameObject go;
Text testObj = conentRect.FindChild("Text").GetComponent<Text>();
for (int i = dat - itemNum + ; i <= dat; i++) {
go = Instantiate(testObj.gameObject, conentRect);
go.GetComponent<Text>().text = i.ToString();
go.name = i.ToString();
textList.Add(go.GetComponent<Text>());
ShowItem(true);
}
Destroy(conentRect.FindChild("Text").gameObject);
} /// <summary>
/// 是增加或减少
/// </summary>
/// <param name="isIncreaseOrdecrease"></param>
void ShowItem(bool isIncreaseOrdecrease) {
itemHeight_min = -itemHeight; if (_itemtype == ItemType._day) itemHeight_max = -itemHeight * itemNum - ;
else itemHeight_max = -itemHeight * itemNum; if (isIncreaseOrdecrease) {
foreach (Text rectItem in textList) {
if (rectItem.GetComponent<RectTransform>().anchoredPosition.y > itemHeight_min) {
print("+");
rectItem.transform.SetSiblingIndex((int)itemNum);
}
}
print(itemHeight_min);
} else {
foreach (Text rectItem in textList) {
if (rectItem.GetComponent<RectTransform>().anchoredPosition.y < itemHeight_max) {
print("-");
rectItem.transform.SetSiblingIndex();
}
}
print(itemHeight_max); }
} /// <summary>
/// 渐变效果,改变大小,高亮显示
/// </summary>
void ItemList() {
foreach (Text item in textList) {
float indexA = Mathf.Abs(item.GetComponent<RectTransform>().position.y - targetRec.position.y);
float indexSc_scale = Mathf.Abs(curve_scale.Evaluate(indexA / contentParentHeight));
float indexSc_color = Mathf.Abs(curve_color.Evaluate(indexA / contentParentHeight));
if (indexA < ) {
item.color = itemColor_hig;
switch (_itemtype) {
case ItemType._year: _year = int.Parse(item.text); break;
case ItemType._month: _month = int.Parse(item.text); break;
case ItemType._day: _day = int.Parse(item.text); break;
}
} else item.color = new Color(, , , - indexSc_color); item.GetComponent<RectTransform>().localScale = new Vector3( - indexSc_scale, - indexSc_scale * , - indexSc_scale);
//item.GetComponent<RectTransform>().sizeDelta = new Vector2(deltaX - (deltaX * indexSc), deltaY - (deltaY * indexSc));
} } /// <summary>
/// 获取int类型日期,并转换为指定格式
/// </summary>
/// <returns></returns>
public static string GetDateInfo() { return _year + "-" + _month + "-" + _day; } /// <summary>
/// 纠正Conent位置
/// </summary>
void UpdateEx() {
if (conentRect.anchoredPosition.y > conentLimit) {
ShowItem(true);
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
}
if (conentRect.anchoredPosition.y < conentLimit) {
ShowItem(false);
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
}
} /// <summary>
/// 获取拖拽信息并改变Conent位置
/// </summary>
/// <param name="eventData"></param>
void SetDraggedPosition(PointerEventData eventData) {
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(conentRect, eventData.position, eventData.pressEventCamera, out newDragPos)) {
newDragPos = eventData.position;
if (Mathf.Abs(newDragPos.y - oldDragPos.y) >= itemHeight) {
if (newDragPos.y > oldDragPos.y) {
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y + itemHeight);
oldDragPos += new Vector3(, itemHeight, );
ItemList();
} else {
conentRect.anchoredPosition = new Vector2(conentRect.anchoredPosition.x, conentRect.anchoredPosition.y - itemHeight);
oldDragPos -= new Vector3(, itemHeight, );
ItemList();
}
}
}
} /// <summary>
/// 当开始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData) {
oldDragPos = eventData.position;
} public void OnDrag(PointerEventData eventData) {
SetDraggedPosition(eventData);
UpdateEx();
} public void OnEndDrag(PointerEventData eventData) {
SetDraggedPosition(eventData);
UpdateEx();
}
}

照着来的话基本没什么问题

因为赶时间所以很多地方写的简单粗暴请谅解

如果调整元素大小或者间隙大小 需要改变itemHeight_min 和 itemHeight_max 的值

他们分别为

itemHeight_min

itemHeight_max 

也就是元素的最顶层和最底层的Y值

以上就是年月日选择器的具体步骤

Unity3d—做一个年月日选择器(Scroll Rect拖动效果优化)— 无限滚动 + 锁定元素的更多相关文章

  1. 用Django做一个省份选择器

    做一个省份选择器 使用django做后端, mysql数据库, jQuery 列出结构主要的文件, 其它配置比较简单 models.py 因为所有数据的结构基本一致, 把所有省份, 市和区全部存储一张 ...

  2. 关于longPressGesture做一个长按连加的效果(原创)

    关于longPressGesture做一个长按连加的效果 解释一下什么意思呢?就是一个button长按之后其数字的一直累加.朋友们可能看起来很简单,无非就是加一个长按手势(longPressGestu ...

  3. WPF 自己做一个颜色选择器

    程序开发过程中,经常会遇到需要支持动态配置主题颜色的问题,通常,一个程序会有多种不同的颜色风格主题供选 有时候,更细致一些的地方,会需要支持自己配置颜色,这样我们就需要一个颜色选择器啦,下面是我自己开 ...

  4. 做一个360度看车的效果玩玩(web)

    前几天在 Lexus 官网看到有这样的一个效果:http://www.lexus.com.cn/models/es/360 于是顺手打开控制台看了下他们是怎么做的,发现使用的技术还是比较简单的,通过背 ...

  5. css3加js做一个简单的3D行星运转效果

    前几天在园子里看到一篇关于CSS3D行星运转的文章,原文在这里,感觉这个效果也太酷炫了,于是自己也就心血来潮的来尝试的做了一下.因为懒得去用什么插件了,于是就原生的JS写,效果有点粗超,还有一些地方处 ...

  6. 做一个阅读管理APP

    背景 由于最近在看的书有点多,所以一直想找一个能够管理阅读进度的书(鄙人记性不是很好,两天不看就忘了)可惜Android平台上一直找不到合适的APP: 有没有读书进度管理的网站或软件啊? 有没有记录读 ...

  7. Infinite Scroll - jQuery & WP 无限滚动插件

    无限滚动(Infinite Scroll)也称为自动分页.滚动分页和无限分页.常用在图片.文章或其它列表形式的网页中,用来在滚动网页的时候自动加载下一页的内容.Infinite Scroll  这款  ...

  8. 用Jquery做一个时间日期选择器

    今天我们就用Jquery做一个时间日期选择器,当打开网页时,文本框里面显示的是当前的日期,点击文本框可以出现年.月.日的下拉菜单,并且可以选择,会根据年份的选择判断是否是闰年,从而改变二月的天数,闰年 ...

  9. 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统

    对于AI,我的初始想法非常easy,首先他要能动,而且是在地图里面动. 懂得撞墙后转弯,然后懂得射击,其它的没有了,基于这个想法,我首先创建了一个MyTank类,用于管理玩家的坦克的活动,然后创建AI ...

随机推荐

  1. 简谈-Python的注释、变量类型、标识符及关键字

    在Python程序中,要想支持中文输出,则要在代码前面添加 标识符:开发人员在程序中自定义的一些符号和名称 标示符是自己定义的,如变量名 .函数名等 标识符的规则:  标示符由字目.下划线和数字组成, ...

  2. 规范模式-------From ABP Document

    介绍 规范模式是一种特定的软件设计模式,通过使用布尔逻辑 (维基百科)将业务规则链接在一起,可以重新组合业务规则. 在实际中,它主要用于 为实体或其他业务对象定义可重用的过滤器. 例 在本节中,我们将 ...

  3. 个人VIM配置实例

    用户 vimrc 文件: "$HOME/.vimrc" " vimrc by lewiyon@hotmail.com " last update 2013-10 ...

  4. Hadoop集群

    你可以用以下三种支持的模式中的一种启动Hadoop集群: 单机模式 伪分布式模式 完全分布式模式 单机模式的操作方法 默认情况下,Hadoop被配置成以非分布式模式运行的一个独立Java进程.这对调试 ...

  5. angularjs jsonp跨域

    <script> (function(angular){ "use strict" var app= angular.module('appController',[] ...

  6. C++中发声函数Beep详解

    By zhcs 以前,我听过一个神犇用C++函数做的音乐,当时的心里就十分激动:哇,好厉害啊,好神啊. 这次,我终于通过自己无助的盲目的摸索.研究,写出了这篇文章(此时我的内心是鸡冻的233) 下面是 ...

  7. 吕鑫VC6.0-VS2015 全套C/C++、MFC新手实战入门教程、Linux视频教程 最好的基础入门教程没有之一

    本课程包括:[1]C语言(1个月)[2]C++语法与数据结构(1个月)) [3]MFC项目开发(1个月)[4]Linux项目开发(1个月)往届的授课视频都已经上传到百度网盘,请同学们按照视频教程提前掌 ...

  8. GreenDao 使用二

    1.创建一个实体类 1 Entity note = schema.addEntity("Note"); 默认表名就是类名,也可以自定义表名 1.dao.setTableName(& ...

  9. angular.js的表格指令

    html div.col-sm-12 table.table.table-bordered.table-condensed.table-hover.table-striped.dataTable.no ...

  10. Linux主机通过代理服务器进行网络连接

    公司的不允许访问外网,但是访问外网需要配置代理,window配置均好配置,在Linux的命令行底下,一般的程序都是使用http_proxy和ftp_proxy这两个环境变量来获得代理设置的.1.在/e ...