Unity3D通过鼠标滑动改变数值

Unity3D通过鼠标滑动改变数值_第1张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class MyChangdeIndexBySlider : MonoBehaviour
{
    public int minIndex;
    public int maxIndex;
    public int curindex;
    [Header("======差值======")]
    public int changeSize=1;
    [Header("======循环======")]
    public bool isLoop = false;
    [Header("======滑动距离======")]
    public float maxDistance = 100;

    //鼠标位置
    private Vector2 beginPos = new Vector2(0, 0);
    private Vector2 endPos = new Vector2(0, 0);
    private bool isSilder = false;


    private void Update()
    {
        if (Mathf.Abs(changeSize) > maxIndex - minIndex) {
            return;
        }
        //获取鼠标滑动起始坐标和终点坐标
        if (Input.GetMouseButtonDown(0))
        {
            beginPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
        }
        if (Input.GetMouseButtonUp(0))
        {
            endPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            SlipDirection();
        }
    }
    //判断滑的方向
    public void SlipDirection()
    {
        if (Mathf.Abs(beginPos.y - endPos.y) < (Mathf.Abs(beginPos.x - endPos.x)))
        {
            if (Mathf.Abs(beginPos.x - endPos.x) > maxDistance)
            {
                if (beginPos.x > endPos.x)
                {
                    Next();
                }
                else
                {
                    Prev();
                }
            }
        }
    }
    //向左滑
    private void Next()
    {
        if (!isSilder){
            isSilder = true;
            if (isLoop)
            {
                curindex+=changeSize;
                if (curindex > maxIndex)
                {
                    curindex = minIndex;
                }
                //某件事完成后
                isSilder = false;
            }
            else
            {
                if (curindex+changeSize <= maxIndex)
                {
                    curindex+=changeSize;
                }
                //某件事完成后
                isSilder = false;
            }
        }
    }
    //向右滑
    private void Prev()
    {
        if (!isSilder) {
            isSilder = true;
            if (isLoop)
            {
                curindex-=changeSize;
                if (curindex < minIndex)
                {
                    curindex = maxIndex;
                }
                //某件事完成后
                isSilder = false;
            }
            else
            {
                if (curindex-changeSize >= minIndex)
                {
                    curindex-=changeSize;
                }
                //某件事完成后
                isSilder = false;
            }
        }
    }
}

 

你可能感兴趣的:(Unity3D,Unity3D开发)