unity3D技术分享

unity圆圈自动吸附屏幕边缘

2019-02-28  本文已影响14人  好怕怕

效果:


GIF.gif

设置如下:


image.png
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class AutoAdsorptiontoEdge : MonoBehaviour, IPointerClickHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{

    private RectTransform popupTransform;

    private bool isPopupBeingDragged = false;
    private IEnumerator moveToPosCoroutine = null;

    private Vector2 halfSize;


    void Awake()
    {
        popupTransform = (RectTransform)transform;

    }
    void Start()
    {
        halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
        Init();
    }



    public void Init()
    {
        halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
        OnEndDrag(null);
    }

    public void OnPointerClick(PointerEventData data)
    {
        if (!isPopupBeingDragged)
        {

        }
    }


    public void OnBeginDrag(PointerEventData data)
    {
        isPopupBeingDragged = true;

        if (moveToPosCoroutine != null)
        {
            StopCoroutine(moveToPosCoroutine);
            moveToPosCoroutine = null;
        }
    }

    public void OnDrag(PointerEventData data)
    {
        popupTransform.position = data.position;
    }

    public void OnEndDrag(PointerEventData data)
    {
        int screenWidth = Screen.width;
        int screenHeight = Screen.height;

        Vector3 pos = popupTransform.position;

        float distToLeft = pos.x;
        float distToRight = Mathf.Abs(pos.x - screenWidth);

        float distToBottom = Mathf.Abs(pos.y);
        float distToTop = Mathf.Abs(pos.y - screenHeight);

        float horDistance = Mathf.Min(distToLeft, distToRight);
        float vertDistance = Mathf.Min(distToBottom, distToTop);

        if (horDistance < vertDistance)
        {
            if (distToLeft < distToRight)
                pos = new Vector3(halfSize.x, pos.y, 0f);
            else
                pos = new Vector3(screenWidth - halfSize.x, pos.y, 0f);

            pos.y = Mathf.Clamp(pos.y, halfSize.y, screenHeight - halfSize.y);
        }
        else
        {
            if (distToBottom < distToTop)
                pos = new Vector3(pos.x, halfSize.y, 0f);
            else
                pos = new Vector3(pos.x, screenHeight - halfSize.y, 0f);

            pos.x = Mathf.Clamp(pos.x, halfSize.x, screenWidth - halfSize.x);
        }

        if (moveToPosCoroutine != null)
            StopCoroutine(moveToPosCoroutine);

        moveToPosCoroutine = MoveToPosAnimation(pos);
        StartCoroutine(moveToPosCoroutine);

        isPopupBeingDragged = false;
    }

    private IEnumerator MoveToPosAnimation(Vector3 targetPos)
    {
        float modifier = 0f;
        Vector3 initialPos = popupTransform.position;

        while (modifier < 1f)
        {
            modifier += 4f * Time.unscaledDeltaTime;
            popupTransform.position = Vector3.Lerp(initialPos, targetPos, modifier);

            yield return null;
        }
    }


}

上一篇下一篇

猜你喜欢

热点阅读