博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UI图像拖动更换
阅读量:6508 次
发布时间:2019-06-24

本文共 4349 字,大约阅读时间需要 14 分钟。

今天做一个拖动图像的demo,效果图如下:

 

1 拖动的类需要实现 IDropHandler, IPointerEnterHandler, IPointerExitHandler 这三个接口;

2 接受的类需要实现 IBeginDragHandler,IDragHandler,IEndDragHandler 这三个接口:

下面贴代码

Drag.cs

using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;//拖动要集成三个接口:IBeginDragHandler,IDragHandler,IEndDragHandler[RequireComponent(typeof(Image))]public class Drag : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler {    //是否与面板保持平行    public bool dragOnSurfaces = true;    private GameObject m_DragIcon;    private RectTransform m_RectPlane;    public void OnBeginDrag(PointerEventData eventData)    {        var canvas = FindInParent
(gameObject); if (canvas == null) return; //创建临时拖动对象名为“Icon” m_DragIcon = new GameObject("Icon"); //为拖动对象设置画布 m_DragIcon.transform.SetParent(canvas.transform, false); //设置为同级最后 m_DragIcon.transform.SetAsLastSibling(); var image = m_DragIcon.AddComponent
(); //拖动对象忽略事件响应 m_DragIcon.AddComponent
(); image.sprite = gameObject.GetComponent
().sprite; image.SetNativeSize(); if (dragOnSurfaces) m_RectPlane = transform as RectTransform; else m_RectPlane = canvas.transform as RectTransform; //拖动对象跟随光标 SetDraggedPosition(eventData); } public void OnDrag(PointerEventData eventData) { if (m_DragIcon != null) SetDraggedPosition(eventData); } private void SetDraggedPosition(PointerEventData data) { if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null) m_RectPlane = data.pointerEnter.transform as RectTransform; var rt = m_DragIcon.GetComponent
(); // 屏幕点在矩形内的世界坐标 Vector3 globalMousePos; if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_RectPlane,data.position,data.pressEventCamera,out globalMousePos)) { rt.position = globalMousePos; rt.rotation = m_RectPlane.rotation; } } public void OnEndDrag(PointerEventData eventData) { //释放删除临时对象 if (m_DragIcon != null) Destroy(m_DragIcon); } static public T FindInParent
(GameObject go) where T:Component { if (go == null) return null; var comp = go.GetComponent
(); if (comp != null) return comp; Transform temp = go.transform.parent; while (temp != null && comp == null) { comp = temp.gameObject.GetComponent
(); if (comp != null) return comp; temp = temp.parent; } return comp; }}

 

Drop.cs

using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;//接收拖动的类需要实现的三个接口:IDropHandler, IPointerEnterHandler, IPointerExitHandlerpublic class Drop : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler{    public Image containerImage;    public Image receivingImage;    private Color normalColor;    public Color highLightColor = Color.blue;    public void OnEnable()    {        if (containerImage != null)            normalColor = containerImage.color;    }    //释放对象    public void OnDrop(PointerEventData eventData)    {        containerImage.color = normalColor;        //没有图像直接返回        if (receivingImage == null)            return;        Sprite dropSprite = GetDropSprite(eventData);        if (dropSprite != null)            receivingImage.sprite = dropSprite;    }    //对象移到接受面板,面板高亮    public void OnPointerEnter(PointerEventData eventData)    {        if (containerImage == null)            return;        Sprite dropSprite = GetDropSprite(eventData);        if (dropSprite != null)            containerImage.color = highLightColor;    }    public void OnPointerExit(PointerEventData eventData)    {        if (containerImage == null)            return;        containerImage.color = normalColor;    }    //获取图像数据    private Sprite GetDropSprite(PointerEventData data)    {        var originalObj = data.pointerDrag;        if (originalObj == null)            return null;        var srcImage = originalObj.GetComponent();        if (srcImage == null)            return null;        return srcImage.sprite;    }}

 

demo源文件

 

转载于:https://www.cnblogs.com/lu-xi/p/6706751.html

你可能感兴趣的文章
《中国人工智能学会通讯》——6.6 实体消歧技术研究
查看>>
如何在Windows查看端口占用情况及查杀进程
查看>>
云存储应用Upthere获7700万美元股权债务融资
查看>>
国家互联网应急中心何世平博士主题演讲
查看>>
洗茶,你误会了多少年?
查看>>
贵阳高新区力争打造“千亿级大数据园区”
查看>>
安防众筹不止于卖产品 思维拓展刺激消费
查看>>
OpenSSH曝高危漏洞 会泄露私钥
查看>>
艾特网能获2016APCA用户满意品牌大奖
查看>>
《CCNP TSHOOT 300-135学习指南》——第2章 结构化故障检测与排除进程
查看>>
《Java EE 7精粹》—— 2.5 非阻塞I/O
查看>>
《Python数据科学实践指南》一2.2 字符串
查看>>
《R数据可视化手册》——1.1 安装包
查看>>
《iOS创意程序设计家》——导读
查看>>
spring-aop
查看>>
android RecycleView Adapter简单封装
查看>>
PgSQL · 案例分享 · 递归收敛优化
查看>>
Dart的数据库操作
查看>>
Codeforces 591 B Rebranding【Codeforces Round #327 (Div. 2)】
查看>>
命名难,难于上青天
查看>>