当前位置: 首页 > news >正文

知名的网站开发公司设计个人网站

知名的网站开发公司,设计个人网站,俄语好网站设计,深圳知名网站建设展示效果 原理: 当停止滑动时 判断Contet的horizontalNormalizedPosition 与子Item的缓存值 相减,并得到最小值,然后将Content horizontalNormalizedPosition滚动过去 使用方式: 直接将脚本挂到ScrollRect上 注意:在创建Content子物体时…

展示效果


原理:

当停止滑动时

判断Contet的horizontalNormalizedPosition

与子Item的缓存值 相减,并得到最小值,然后将Content  horizontalNormalizedPosition滚动过去

使用方式:

直接将脚本挂到ScrollRect上

注意:在创建Content子物体时 或子物体数量变更,需要调用Refresh

代码:

namespace ShangShangQian.Component
{using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;using System.Linq;using UnityEngine.Events;[RequireComponent(typeof(ScrollRect))]public class SnapScrollRect : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler{private RectTransform content;private ScrollRect rect;/// <summary>/// content 的位置应该滚到什么/// </summary>public float target = 1;/// <summary>/// 回滚的速度 0-1 越小越快/// </summary>public float smooting = 0.25f;public float currentVelocity;public float horizontalNormalizedPosition;public List<float> values = new List<float>();public List<float> distances = new List<float>();public List<RectTransform> items = new List<RectTransform>();/// <summary>/// 是否拖拽中/// </summary>public bool isDrag = false;/// <summary>/// 到最近item的距离/// </summary>public float distanceMin;/// <summary>/// 到最近item的索引/// </summary>public int selectIndex;/// <summary>/// 是否匹配滑动中/// </summary>public bool Snaping = false;/// <summary>/// 回滚完毕后调用/// </summary>public UnityEvent<int> OnSnap;void Start(){rect = GetComponent<ScrollRect>();content = rect.content;}/// <summary>/// 刷新数据,content的子物体数量变更时需要调用此函数/// </summary>public void Refresh(){items.Clear();values.Clear();for (int i = 0; i < content.childCount; i++){if (content.GetChild(i).gameObject.activeInHierarchy){items.Add(content.GetChild(i).GetComponent<RectTransform>());}}//累加的变量values.Add(0);//每一个格子的所占比多少  float v = 1f / (items.Count - 1);for (int i = 1; i < items.Count; i++){values.Add(i * v);}//不同子元素数量 item 对应 的content  horizontalNormalizedPosition 数值//1  0  //2  0 1   //3  0 0.5 1//4  0 0.33 0.66 1}void Update(){
#if UNITY_EDITORif (Input.GetKeyDown(KeyCode.A)){Refresh();}
#endif}void FixedUpdate(){horizontalNormalizedPosition = rect.horizontalNormalizedPosition;if (isDrag){return;}if (Snaping){rect.horizontalNormalizedPosition = Mathf.SmoothDamp(rect.horizontalNormalizedPosition, target, ref currentVelocity, smooting);if (Mathf.Abs(rect.horizontalNormalizedPosition - values[selectIndex]) < 0.001f){Snaping = false;Debug.Log("回滚:" + selectIndex);OnSnap.Invoke(selectIndex);}}}public void OnBeginDrag(PointerEventData eventData){isDrag = true;}public void OnDrag(PointerEventData eventData){isDrag = true;}public void OnEndDrag(PointerEventData eventData){isDrag = false;Snaping = true;distances.Clear();//当松手后判断那个距离最近for (int i = 0; i < values.Count; i++){distances.Add(Mathf.Abs(rect.horizontalNormalizedPosition - values[i]));}distanceMin = distances.Min();selectIndex = distances.FindIndex(b => b == distanceMin);target = values[selectIndex];rect.StopMovement();}/// <summary>/// 滚到到指定位置/// </summary>/// <param name="index"></param>public void SnapToIndex(int index){Snaping = true;selectIndex = index;target = values[selectIndex];}}
}

http://www.ds6.com.cn/news/86404.html

相关文章:

  • 杭州网站建设杭州沃迩夫品牌推广外包公司
  • 绿色配色的网站设计模板网站
  • 日本特色小镇建设网站武汉推广系统
  • 制造业外贸营销网站建设德州seo优化
  • 阿里云ecs搭建web网站全是广告的网站
  • 服务网站策划书百度seo培训课程
  • 太原网站开发哪家好无锡营销型网站建设
  • 网站建设战略伙伴seo人才网
  • 备案号怎么放置到网站免费推广网站排行榜
  • 网站开发 c权威发布
  • 南宁网站推广策略百度推广登录平台登录
  • 网站源码下载教程seo运营学校
  • 佛山网站建设公司排行排名优化方法
  • 做视频网站每部电影都要版权连接友谊
  • 我自己的网站怎么做关键词优化关键词歌词任然
  • 重庆忠县网站建设公司哪家好上海推广网站
  • 网上做批发有哪些网站有哪些地推团队去哪里找
  • 成都六度网站建设网站如何提交百度收录
  • 推广公司的套路杭州seo全网营销
  • 贵阳哪里可以做网站交换友情链接是什么意思
  • 有人用wordpress默认主题上海站群优化
  • 用什么做公司宣传网站天琥设计培训学校官网
  • 武汉手机网站设计如何竞价专员是做什么的
  • 毕业设计论文代做网站厦门人才网个人会员登录
  • vs做的网站如何律师网络推广
  • 哪家做网站的好营销型网站重要特点是
  • 祖庙高明网站建设西安专业网络推广公司
  • 凡科网电脑版怎么做网站下拉框关键词软件
  • 做网站还能赚钱免费网络推广软件有哪些
  • 一流的嘉兴网站建设爱站工具包的模块