圓形拖拽滑動條

圓形拖拽滑動條

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class CircularDrag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerDownHandler
{
    public void OnBeginDrag(PointerEventData eventData)
    {
       
    }

    public void OnDrag(PointerEventData eventData)
    {
        Drag(eventData);
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        //throw new System.NotImplementedException();
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        Drag(eventData);
    }
    [SerializeField]
    private RectTransform m_Hander;
    public RectTransform Hander
    {
        get
        {
           return m_Hander;
        }
        set
        {
            m_Hander = value;
        }
      
    }
    private float _Radius;
    public float Radius
    {
        get
        {
            if (rectTransform)
            {
                return rectTransform.rect.width / 2;
            }
            return 1;
        }
        set
        {
            _Radius = value;
        }
    }
    public void SetDragHander(Transform hander)
    {


    }
    public RectTransform rectTransform
    {
        get
        {
            return (RectTransform)transform;
        }
    }
    public Canvas canvas;
    public void Drag(PointerEventData eventData)
    {
        setPos(eventData);
    }
    public Vector2 lastPos;
    public Action<Vector2> OnChange
    {
        get;set;
    }
    [SerializeField]
    private float m_Sensitivity = 2;
    public float Sensitivity
    {
        get;set;
    }
    private Vector2 _Value;
    public Vector2 Value
    {
        get
        {
            return GetValue();
        }
        set
        {
            _Value = value;
            SetValue(_Value);
        }
    }
    public void setPos(PointerEventData eventData)
    {
        if (Hander)
        {
         
            if (Vector2.Distance(Hander.anchoredPosition, lastPos) < m_Sensitivity)
            {
                //return;
            }
            Vector2 pos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, canvas.worldCamera, out pos);
            if (Vector2.Distance(rectTransform.anchoredPosition, pos) >= Radius)
            {
                Vector2 normal =  (pos - rectTransform.anchoredPosition).normalized;
                pos = rectTransform.anchoredPosition + Vector2.Scale(normal, new Vector2(Radius,Radius) );
            }
            Hander.anchoredPosition = pos;
            lastPos = pos;
            if (OnChange != null)
            {
                OnChange.Invoke(GetValue());
            }
        }
    }
    public void SetValue(Vector2 value)
    {
        float bagePow = Mathf.Pow(Radius, 2) / 2;
        float bage = Mathf.Sqrt(bagePow);
        value = Vector2.Scale(value, new Vector2(2,2));
        Vector2 curVector = Vector2.Scale(new Vector2(bage, bage),value);
        Hander.anchoredPosition = curVector;
    }
    private Vector2 GetValue()
    {
        float bagePow = Mathf.Pow(Radius, 2) / 2;
        float bage = Mathf.Sqrt(bagePow);
        Vector2 curPos = (Hander.anchoredPosition - rectTransform.anchoredPosition);


        curPos = Vector2.Scale(curPos, new Vector2(1 / bage, 1 / bage));
        curPos.x = Mathf.Clamp(curPos.x, -1, 1);
        curPos.y = Mathf.Clamp(curPos.y, -1, 1);
        curPos = Vector2.Scale(curPos, new Vector2(0.5f, 0.5f));
        return curPos;
    }
    // Use this for initialization
    void Start () {
        OnChange = (v) => print(v);

    }
    
    // Update is called once per frame
    void Update () {
        
    }

 
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章