Unity UGUI实现鼠标拖动图片



using
System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; /// /// **************************UGUI拖动图片,脚本挂在Image上即可********************************* /// public class TuoDongWuPin : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { //需要被实例化的与制体 //public GameObject YuSheWu; //实例化后的对象 private Image img; Vector3 offPos;//存储按下鼠标时的图片-鼠标位置差 Vector3 arragedPos; //保存经过整理后的向量,用于图片移动 /// /// 开始拖拽的时候 /// /// public void OnBeginDrag(PointerEventData eventData) { if (RectTransformUtility.ScreenPointToWorldPointInRectangle(transform.GetComponent(), Input.mousePosition , eventData.enterEventCamera, out arragedPos)) { offPos = transform.position - arragedPos; } } /// /// 拖拽中 /// /// public void OnDrag(PointerEventData eventData) { transform.position = offPos + Input.mousePosition; } /// /// 拖拽结束 /// /// public void OnEndDrag(PointerEventData eventData) { transform.position = transform.parent.transform.position; } }

 

转载于:https://www.cnblogs.com/qq2351194611/p/10484191.html

你可能感兴趣的:(Unity UGUI实现鼠标拖动图片)