最近几天在跟着一个视频教程学习,其中用到了拖拽功能,我想到了官方的DEMO中有相应的例子,就去看了一下,很简单,把官方的代码拿过来,稍微作些修改就实现了拖动功能。学习
1、实现拖拽功能spa
先上代码:指针
using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using System.Collections; public class DragableCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { // begin dragging public void OnBeginDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } // during dragging public void OnDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } // end dragging public void OnEndDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } /// <summary> /// set position of the dragged game object /// </summary> /// <param name="eventData"></param> private void SetDraggedPosition(PointerEventData eventData) { var rt = gameObject.GetComponent<RectTransform>(); // transform the screen point to world point int rectangle Vector3 globalMousePos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos)) { rt.position = globalMousePos; } } }
只要继承了IBeginDragHandler、IDragHandler、和IEndDragHandler这三个接口,并实现了OnBeginDrag、OnDrag和OnEndDrag这三个方法,咱们就能够实现拖拽功能。其中,OnBeginDrag处理开始拖动时要作什么事,OnDrag处理拖动过程当中要作什么事,OnEndDrag处理拖动结束时要作什么事,是否是很easy啊!code
2、实现跟背包相似的功能orm
UGUI官方的例子实际是背包界面的雏形,恰好我要实现一个相似的功能,就是拖动卡片到指定区域。我一样把官方的代码稍作修改,运行,但是没有起做用,通过研究和实践,我发现必须保证指定区域对应的物体的渲染优先级比被拖动物体的高,即在Hierachy视图中,指定区域对应的物体必定要在被拖动物体的下面,如图视频
这样才能保证脚本中的OnDrop方法才会被调用。接下来上代码:继承
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler { public GameObject m_myCardsObj; private bool m_isIn = false; public void OnEnable() { Debug.Log("............"); } // when you release the left button of mouse public void OnDrop(PointerEventData data) { Debug.Log(string.Format(">>>>> {0}", m_isIn)); if (!m_isIn) { m_myCardsObj.GetComponent<MyCards>().UpdateShow(); } } // when the mouse moving in the game object public void OnPointerEnter(PointerEventData data) { m_isIn = true; Debug.Log(m_isIn); } // when the mouse moving out the game object public void OnPointerExit(PointerEventData data) { m_isIn = false; Debug.Log(m_isIn); } private Sprite GetDropSprite(PointerEventData data) { var originalObj = data.pointerDrag; if (originalObj == null) return null; var srcImage = originalObj.GetComponent<Image>(); if (srcImage == null) return null; return srcImage.sprite; } }
继承了IDropHandler, IPointerEnterHandler, IPointerExitHandler这三个接口,一样也实现对应的三个方法OnDrop、OnPointerEnter和OnPointerExit。其中,OnDrop处理松开鼠标左键时要作什么事,OnPointerEnter处理鼠标指针进入挂载该脚本的物体区域时要作什么事,OnPointerExit处理处理鼠标指针移出挂载该脚本的物体区域时要作什么事,也很easy!教程
在这里,个人描述太简单了,要结合实际的例子,亲自体会一下才会明白!官方的例子能够在Assets Stroe中找到,是免费的。接口