栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity在滚动视图下实现水平翻页

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity在滚动视图下实现水平翻页

滚动视图的MovementType属性设置为Clamped,然后去除Inertia的勾选

其中用到GameManager代码在:

Unity学习笔记:在GameManager里记录手游操作框架_努力长头发的程序猿的博客-CSDN博客

其中用到UnitManager代码在:Unity:利用UnitManager类实现一些复杂的功能(长期更新)_努力长头发的程序猿的博客-CSDN博客

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

public class ScrollPage : MonoBehaviour , IPointerDownHandler , IPointerUpHandler
{
    private RectTransform content;

    private float lastClickPosX;

    private Vector2 lastContentPos;

    private float clickTime;

    float screenSize;

    void Start()
    {
        content = transform.Find("Viewport/Content") as RectTransform;
        screenSize = (transform as RectTransform).sizeDelta.x + transform.Find("Viewport/Content").GetComponent().spacing;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        lastClickPosX = GameManager.Main.GetNumPos().x;
        Vector3 minPos = new Vector3(-(int)((Math.Abs(content.anchoredPosition.x) / screenSize)) * screenSize, content.localPosition.y, 0);
        Vector3 maxPos = new Vector3(-(int)((Math.Abs(content.anchoredPosition.x) / screenSize) + 1) * screenSize, content.localPosition.y, 0);
        lastContentPos = Vector3.Distance(content.anchoredPosition, minPos) < Vector3.Distance(content.anchoredPosition, maxPos) ? minPos : maxPos;
        clickTime = Time.time + 0.5f;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (Time.time <= clickTime)
        {
            if (GameManager.Main.GetNumPos().x > lastClickPosX + 150.0f)
            {
                UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / screenSize)) * screenSize, content.anchoredPosition.y), 0.1f);
            }
            else if (GameManager.Main.GetNumPos().x < lastClickPosX - 150.0f)
            {
                UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / screenSize) + 1) * screenSize, content.anchoredPosition.y), 0.1f);
            }
            else
            {
                UnitManager.Main.Domove(content, lastContentPos, 0.1f);
            }
        }
        else
        {
            if (Math.Abs(content.anchoredPosition.x) % screenSize > screenSize * 0.5f)
            {
                UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / screenSize) + 1) * screenSize, content.anchoredPosition.y), 0.1f);
            }
            else
            {
                UnitManager.Main.Domove(content, new Vector2(-((int)(Math.Abs(content.anchoredPosition.x) / screenSize)) * screenSize, content.anchoredPosition.y), 0.1f);
            }
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/900935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号