其他分享
首页 > 其他分享> > Unity 鼠标拖动图片

Unity 鼠标拖动图片

作者:互联网

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class ShiYuanOnDrag : UIBehaviour, IDragHandler, IPointerDownHandler, IEndDragHandler, IBeginDragHandler
{
    RectTransform _rectTransform;
    private RectTransform _parent;
    private float _parentWidtth;
    private float _parentHeight;

    protected override void Awake()
    {
        base.Awake();
        _parent = transform.parent as RectTransform;
        _parentWidtth = ((float)(_parent.rect.width - 50)) / 2;
        _parentHeight = (float)(_parent.rect.height - 50) / 2;
        _rectTransform = GetComponent<RectTransform>();
    }

    public void OnDrag(PointerEventData eventData)
    {
        // if (_rectTransform.anchoredPosition.x < _parentWidtth &&
        //     _rectTransform.anchoredPosition.x > -_parentWidtth &&
        //     _rectTransform.anchoredPosition.y < _parentHeight &&
        //     _rectTransform.anchoredPosition.y > -_parentHeight
        // )
        // {
        _rectTransform.anchoredPosition += eventData.delta;
        // }
    }


    public void OnPointerDown(PointerEventData eventData)
    {
        transform.SetAsLastSibling();

    }

    public void OnEndDrag(PointerEventData eventData)
    {
        // if (_rectTransform.anchoredPosition.x < _parentWidtth &&
        //            _rectTransform.anchoredPosition.x > -_parentWidtth &&
        //            _rectTransform.anchoredPosition.y < _parentHeight &&
        //            _rectTransform.anchoredPosition.y > -_parentHeight
        //        )
        // {
        //     _rectTransform.anchoredPosition += eventData.delta;
        // }
        if (_rectTransform.anchoredPosition.x < -_parentWidtth)
        {
            _rectTransform.SetLocalPosX(-_parentWidtth);
        }
        if (_rectTransform.anchoredPosition.x > _parentWidtth)
        {
            _rectTransform.SetLocalPosX(_parentWidtth);
        }
        if (_rectTransform.anchoredPosition.y < -_parentHeight)
        {
            _rectTransform.SetLocalPosY(-_parentHeight);

        }
        if (_rectTransform.anchoredPosition.y > _parentHeight)
        {
            _rectTransform.SetLocalPosY(_parentHeight);

        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (_rectTransform.anchoredPosition.x < -_parentWidtth)
        {
            _rectTransform.SetLocalPosX(-_parentWidtth);
        }
        if (_rectTransform.anchoredPosition.x > _parentWidtth)
        {
            _rectTransform.SetLocalPosX(_parentWidtth);
        }
        if (_rectTransform.anchoredPosition.y < -_parentHeight)
        {
            _rectTransform.SetLocalPosY(-_parentHeight);

        }
        if (_rectTransform.anchoredPosition.y > _parentHeight)
        {
            _rectTransform.SetLocalPosY(_parentHeight);

        }
    }
}

 

标签:parentHeight,拖动,anchoredPosition,rectTransform,Unity,&&,-_,parentWidtth,
来源: https://blog.csdn.net/qq_36848370/article/details/113650365