赞
踩
应用方法:将下面脚本挂载在需要拖拽功能的UI图片上即可
两种拖拽选择:A.中心拖拽(图片中心跟随鼠标位置)m_isPrecision为false;
B.精准拖拽(图片被鼠标点击的位置跟随鼠标位置)m_isPrecision为true;
常用的代码和主要实现方式都贴在下面了;
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using DG.Tweening; //UI图片拖拽功能类 public class UIDragBySSW : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler,IPointerEnterHandler, IPointerExitHandler { public ModelCon MC; [Header("是否精准拖拽")] public bool m_isPrecision; //存储图片中心点与鼠标点击点的偏移量 private Vector3 m_offset; //存储当前拖拽图片的RectTransform组件 private RectTransform m_rt; private Vector3 imgPos; private Vector3 imgscale; void Start() { //初始化 m_rt = gameObject.GetComponent<RectTransform>(); imgPos = m_rt.anchoredPosition3D; imgscale = m_rt.localScale; } //开始拖拽触发 public void OnBeginDrag(PointerEventData eventData) { //如果精确拖拽则进行计算偏移量操作 if (m_isPrecision) { // 存储点击时的鼠标坐标 Vector3 tWorldPos; //UI屏幕坐标转换为世界坐标 RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos); //计算偏移量 m_offset = transform.position - tWorldPos; } //否则,默认偏移量为0 else { m_offset = Vector3.zero; } SetDraggedPosition(eventData); } //拖拽过程中触发 public void OnDrag(PointerEventData eventData) { SetDraggedPosition(eventData); } //结束拖拽触发 public void OnEndDrag(PointerEventData eventData) { SetDraggedPosition(eventData); //采用DoTween的方式 松开鼠标自动复位 m_rt.DOAnchorPos3D(imgPos,0.4f);![请添加图片描述](https://img-blog.csdnimg.cn/direct/808c85df37544843bed7b1a6ae4ac79f.gif) } /// <summary> /// 设置图片位置方法 /// </summary> /// <param name="eventData"></param> private void SetDraggedPosition(PointerEventData eventData) { //存储当前鼠标所在位置 Vector3 globalMousePos; //UI屏幕坐标转换为世界坐标 if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out globalMousePos)) { //设置位置及偏移量 m_rt.position = globalMousePos + m_offset; } } //在这里做了一个鼠标进入时的动态效果 public void OnPointerEnter(PointerEventData eventData) { m_rt.localScale = new Vector3(imgscale.x*1.1f, imgscale.y * 1.1f, 1); } public void OnPointerExit(PointerEventData eventData) { m_rt.localScale = imgscale; } }
通过鼠标拖拽小按钮 配合其他方法可以做一些动态的变换效果;
EventTriggerListener.Get(UiPanel).onClick+= (go) =>
{
Debug.Log("UI按钮按下========="+go.name);
};
EventTriggerListener.Get(UiPanel).onUp+= (go) =>
{
//中间大图标效果变换
Debug.Log("UI按钮抬起========="+go.name);.
};
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger { public delegate void VoidDelegate(GameObject go); public VoidDelegate onClick; public VoidDelegate onDown; public VoidDelegate onEnter; public VoidDelegate onExit; public VoidDelegate onUp; public VoidDelegate onSelect; public VoidDelegate onUpdateSelect; public VoidDelegate onEndDrag; static public EventTriggerListener Get(GameObject go) { EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); if (listener == null) listener = go.AddComponent<EventTriggerListener>(); return listener; } public override void OnPointerClick(PointerEventData eventData) { if (onClick != null) onClick(gameObject); } public override void OnPointerDown(PointerEventData eventData) { if (onDown != null) onDown(gameObject); } public override void OnPointerEnter(PointerEventData eventData) { if (onEnter != null) onEnter(gameObject); } public override void OnPointerExit(PointerEventData eventData) { if (onExit != null) onExit(gameObject); } public override void OnPointerUp(PointerEventData eventData) { if (onUp != null) onUp(gameObject); } public override void OnSelect(BaseEventData eventData) { if (onSelect != null) onSelect(gameObject); } public override void OnUpdateSelected(BaseEventData eventData) { if (onUpdateSelect != null) onUpdateSelect(gameObject); } public override void OnEndDrag(PointerEventData eventData) { base.OnEndDrag(eventData); if (onEndDrag != null) onEndDrag(gameObject); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。