zoukankan      html  css  js  c++  java
  • unity_UGUI养成之路03

    关卡分页设计

    功能1:通过直接滑动

    添加自动排序组件

    设置通过添加组件设置内容的滑动,多余内容的隐藏

      功能2:通过点击下面的圆圈滑动

    上述代码实现:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    public class LevelButtonScrollRect : MonoBehaviour ,IBeginDragHandler,IEndDragHandler
    {
        private ScrollRect scrollRect;

        public float smoothing = 4; //插值的速度
        private float[] pageArray=new float[]{ 0,0.33333f,0.66666f,1 }; //每个页面X轴坐标

        
        private float targetHorizontalPosition=0; //距离最近页面的X坐标
        private bool isDraging = false; //是否拖拽的标志位

        public Toggle[] toggleArray; //圆圈数组
        
        void Start ()
        {
            scrollRect = GetComponent<ScrollRect>();
        }
        
        void Update ()
        {
            if(isDraging==false)
                scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition,
                    targetHorizontalPosition, Time.deltaTime*smoothing);
        }


        public void OnBeginDrag(PointerEventData eventData)
        {
            isDraging = true;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            isDraging = false;
            //找到当前位置与每页距离最小的页面,并切换到该页面
            float posX = scrollRect.horizontalNormalizedPosition;
            int index = 0;
            float offset = Mathf.Abs(pageArray[index] - posX);
            for (int i = 1; i < pageArray.Length; i++)
            {
                float offsetTemp = Mathf.Abs(pageArray[i] - posX);
                if (offsetTemp < offset)
                {
                    index = i;
                    offset = offsetTemp;
                }
            }
            targetHorizontalPosition = pageArray[index];
            toggleArray[index].isOn = true;
            //scrollRect.horizontalNormalizedPosition = pageArray[index];
        }
        public void MoveToPage1(bool isOn) {
            if (isOn)
            {
                targetHorizontalPosition = pageArray[0];
            }
        }
        public void MoveToPage2(bool isOn) {
            if (isOn) {
                targetHorizontalPosition = pageArray[1];
            }

        }
        public void MoveToPage3(bool isOn) {
            if (isOn)
            {
                targetHorizontalPosition = pageArray[2];
            }

        }
        public void MoveToPage4(bool isOn) {

            if (isOn)
            {
                targetHorizontalPosition = pageArray[3];
            }
        }
    }
     

    点击加群 704621321,可下载官方文档,demo,学习资料
    我的个人博客:https://www.mmzsblog.cn/category/game
    转载请注明出处和署名,谢谢!
  • 相关阅读:
    确认端口占用
    [转]自建Syncthing中继服务器(私密传输或造福大众)
    【转】Syncthing – 数据同步利器---自己的网盘,详细安装配置指南,内网使用,发现服务器配置
    【转】搭建和配置Syncthing发现和中继服务器
    【转】Syncthing的安装与使用
    pycrypto安装出错的问题 intmax_t C:Program Files (x86)Windows Kits10include10.0.10240.0ucrtinttypes.
    Git从库中移除已删除大文件
    词云图
    [转].NET 性能测试工具 -- 事件跟踪器(ETW)
    [转]ANTS Performance Profiler和ANTS Memory Profiler 使用
  • 原文地址:https://www.cnblogs.com/shirln/p/7746936.html
Copyright © 2011-2022 走看看