资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Unity如何使用ScrollRect制作翻页-创新互联

这篇文章主要讲解了Unity如何使用ScrollRect制作翻页,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

创新互联-专业网站定制、快速模板网站建设、高性价比漾濞网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式漾濞网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖漾濞地区。费用合理售后完善,十年实体公司更值得信赖。

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 = 5;

  // 每页显示的项目
  [SerializeField]
  private int countPerPage = 10;

  ScrollRect srect;
  // 总页数
  float totalPages;
  // 是否拖拽结束
  bool isDrag = false;
  // 总页数索引比列 0-1
  List listPageValue = new List { 0 };
  // 滑动的目标位置
  public float targetPos = 0;
  // 当前位置索引
  float nowindex = 0;                 

  void Awake()
  {
    srect = GetComponent();
  }

  public string PageText()
  {
    return (nowindex + 1) + "/" + (totalPages + 1);
  }

  // 计算每页比例
  public void CalcListPageValue() where T : MonoBehaviour
  {
    T[] items = srect.content.GetComponentsInChildren();
    srect.content.rect.Set(srect.content.rect.width / 2, srect.content.rect.y, srect.content.rect.width, srect.content.rect.height);
    totalPages = (int)(Math.Ceiling((float)items.Length / countPerPage) - 1);
    if (items.Length != 0)
    {
      for (float i = 1; 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();
  }

  /// 
  /// 拖动开始
  /// 
  /// 
  public void OnBeginDrag(PointerEventData eventData)
  {
    isDrag = true;
  }

  /// 
  /// 拖拽结束
  /// 
  /// 
  public void OnEndDrag(PointerEventData eventData)
  {
    isDrag = false;
    var tempPos = srect.horizontalNormalizedPosition; //获取拖动的值
    var index = 0;
    float offset = Mathf.Abs(listPageValue[index] - tempPos);  //拖动的绝对值
    for (int i = 1; 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 - 1, 0, totalPages);
    targetPos = listPageValue[Convert.ToInt32(nowindex)];
  }

  public void PressRight()
  {
    nowindex = Mathf.Clamp(nowindex + 1, 0, totalPages);
    targetPos = listPageValue[Convert.ToInt32(nowindex)];
  }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:Unity如何使用ScrollRect制作翻页-创新互联
本文来源:http://cdkjz.cn/article/cocpdj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220