Unity UGUI 滑动按钮(仿ios按钮)

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

[AddComponentMenu("UI/SliderButton", 27)]
public class SliderButton : MonoBehaviour, IPointerDownHandler, IPointerClickHandler, IPointerUpHandler, IDragHandler
{
    public Image BgImg;
    public Image handleImg;
    public Sprite BgHighlightedSprite;
    public Sprite BgPressedSprite;
    public Sprite handleHighlightedSprite;
    public Sprite handlePressedSprite;
    public RectTransform handleParent;
    public RectTransform handle;
    private float handleParentWidth;
    private bool isMove;
    private float handlePosX;
    private float handlePosY;
    private Color color = new Color(255 / 255f, 255 / 255f, 255 / 255f);
    [Range(0, 1)]
    public float moveSpeed = 0.1f;

    [Range(0, 1)]
    public float value = 0;
    public bool lucency = false;
    private enum handlerState
    {
        left,
        right,
    }

   private handlerState nowState = handlerState.left;
    public UnityEvent OnChange = new UnityEvent();
    public void OnPointerClick(PointerEventData eventData)
    {
        OnClick();
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (lucency)
        {
            color.a = 0.9f;
            BgImg.color = color;
            color.a = 0.95f;
            handleImg.color = color;
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (lucency)
        {
            color.a = 1;
            BgImg.color = color;
            handleImg.color = color;
        }
    }

    void Start()
    {
        this.isMove = false;
        if (handleParent != null)
        { handleParentWidth = transform.GetComponent().sizeDelta.x + handleParent.sizeDelta.x; }
        handlePosX = handle.sizeDelta.x;
        handlePosY = handle.sizeDelta.y;
    }

    void Update()
    {
        if (isMove)
        {
            StartMove();
        }
        limitValue();
        handle.anchoredPosition = new Vector2(handleParentWidth * value, 0);
    }

    void limitValue()
    {
        if (isMove)
        {
            if (value <= 0 || value >= 1)
            {
                if (value <= 0)
                {
                    value = 0;
                    this.isMove = false;
                    changeState((int)value);
                }
                else if (value >= 1)
                {
                    value = 1;
                    this.isMove = false;
                    changeState((int)value);
                }
                OnChange.Invoke();
            }
        }
    }

    void StartMove()
    {
        switch (nowState)
        {
            case handlerState.left:
                value += moveSpeed;
                break;
            case handlerState.right:
                value -= moveSpeed;
                break;
            default:
                break;
        }
    }


    void OnClick()
    {
        isMove = true;
    }

    //对外开放 可以设置按钮状态,0:左 1:右
    public void changeState(int v)
    {
        switch (v)
        {
            case 1: //常态
                BgImg.sprite = BgPressedSprite;
                handleImg.sprite = handlePressedSprite;
                this.nowState = handlerState.right;
                break;
            case 0: //点中
                BgImg.sprite = BgHighlightedSprite;
                handleImg.sprite = handleHighlightedSprite;
                this.nowState = handlerState.left;
                break;
            default:
                break;
        }
        value = v;
    }

    public void OnDrag(PointerEventData eventData)
    {
    }
}

你可能感兴趣的:(Unity)