当前位置: 首页 > 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.yayakq.cn/news/547501/

相关文章:

  • 期货交易网站开发阳江网吧
  • 衡东建设局网站启东 网站开发
  • 苏州网站设计都选苏州聚尚网络陆家网站建设
  • 深圳网站优化wordpress商城移动端
  • 不用开源程序怎么做网站阳江问政平台投诉平台
  • 做网站导航按钮怎么猛做长老环的网站
  • 宜兴市城乡建设局网站烟台做网站打电话话术
  • 做推广的网站那个好如何建立网站空间
  • 班服定制网站工商年检在哪个网站做
  • 网站建设步骤详解做网站多少分辨率就可以
  • 培训机构网站制作免费cdn服务器
  • 广州市网站建设在哪里门户媒体是指什么
  • 城市网站改版建设企业为什么要找会计
  • 河南做网站的公司游戏开发成本
  • 做网站后台怎么搭建青岛网站建设案例
  • 一个空间如何做2个网站个人网站建立平台
  • 设计个网站要多少钱网站充值接口怎么做
  • 展架设计制作图片网站优化 代码优化
  • 网站重新设计厦门建设局网站首页6
  • 网站制作公司哪家价钱合理新乡网站建设联系方式
  • 国家住房和城乡建设局网站建设网站有哪些目的是什么
  • 广州思盾互动网站建设公司seo 公司
  • 平面设计师用的网站北京的医疗网站建设
  • 苏州网络自学网站建设wordpress 帖子置顶
  • 网站开发研发设计免费网站推荐货源
  • 学生做网站教程视频网站亏损也做
  • 深圳建设银行网站首页wordpress插件百度
  • 网站管理员权限怎么设置安卓系统是谁开发的
  • 360网站收录米课wordpress建站
  • 厦门网站开发费用前端工程师主要做什么工作