zoukankan      html  css  js  c++  java
  • 鼠标拖动2D物体(图片)

    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    [RequireComponent(typeof(Image))]
    public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler,IEndDragHandler
    {
        public bool dragOnSurfaces = true;
    
        public GameObject m_DraggingIcon;
        private RectTransform m_DraggingPlane;
    
        public void OnBeginDrag(PointerEventData eventData)
        {
            var canvas = FindInParents<Canvas>(gameObject);
            if (canvas == null)
                return;
    
            // We have clicked something that can be dragged.
            // What we want to do is create an icon for this.
            m_DraggingIcon = new GameObject("icon");
    
            m_DraggingIcon.transform.SetParent(canvas.transform, false);
            m_DraggingIcon.transform.SetAsLastSibling();
    
            var image = m_DraggingIcon.AddComponent<Image>();
            // The icon will be under the cursor.
            // We want it to be ignored by the event system.
            CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();
            group.blocksRaycasts = false;
    
            image.sprite = GetComponent<Image>().sprite;
            image.SetNativeSize();
    
            if (dragOnSurfaces)
            {
                m_DraggingPlane = transform as RectTransform;
            }
            else
            {
                m_DraggingPlane = canvas.transform as RectTransform;
            }
            SetDraggedPosition(eventData);
        }
    
        public void OnDrag(PointerEventData data)
        {
            if (m_DraggingIcon != null)
            {
                SetDraggedPosition(data);
            }
            transform.position = m_DraggingIcon.transform.position;
            m_DraggingIcon.SetActive(false);
        }
    
        private void SetDraggedPosition(PointerEventData data)
        {
            if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)
            {
                m_DraggingPlane = data.pointerEnter.transform as RectTransform;
            }
            var rt = m_DraggingIcon.GetComponent<RectTransform>();
            Vector3 globalMousePos;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos))
            {
                rt.position = globalMousePos;
                rt.rotation = m_DraggingPlane.rotation;
            }
        }
    
        public void OnEndDrag(PointerEventData eventData)
        {
            if (m_DraggingIcon != null)
                Destroy(m_DraggingIcon);
            Debug.Log("万用表的位置"+transform.position);
        }
        static public T FindInParents<T>(GameObject go) where T : Component
        {
            if (go == null)
            {
                return null;
            }
    
            var comp = go.GetComponent<T>();
    
            if (comp != null)
            {
                return comp;
            }
    
            Transform t = go.transform.parent;
            while (t != null && comp == null)
            {
                comp = t.gameObject.GetComponent<T>();
                t = t.parent;
            }
            return comp;
        }
    
        
    }

  • 相关阅读:
    求1977!
    三进制小数
    回文数C语言
    JAVA知识点必看
    servlet HttpServletRequest
    为什么web工程要输入localhost或者是127.0.0.1
    service $sce or ng-bind-html
    jQuery的deferred对象详解
    理解promise
    理解Angular中的$apply()以及$digest()
  • 原文地址:https://www.cnblogs.com/Cocomo/p/5753345.html
Copyright © 2011-2022 走看看