UGUI精准拖拽
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; [RequireComponent(typeof(RectTransform))] public class TuoDongImage : MonoBehaviour,IBeginDragHandler,IDragHandler ,IEndDragHandler { private bool isDrag = false; //偏移量 private Vector3 offset = Vector3.zero; public void OnBeginDrag(PointerEventData eventData) { isDrag = false; SetDragObjPostion(eventData); } public void OnDrag(PointerEventData eventData) { isDrag = true; SetDragObjPostion(eventData); } public void OnEndDrag(PointerEventData eventData) { SetDragObjPostion(eventData); } void SetDragObjPostion(PointerEventData eventData) { RectTransform rect = this.GetComponent<RectTransform>(); Vector3 mouseWorldPosition; //判斷是否點到UI圖片上的時候 if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData.pressEventCamera, out mouseWorldPosition)) { if (isDrag) { rect.position = mouseWorldPosition + offset; } else { //計算偏移量 offset = rect.position - mouseWorldPosition; } //直接賦予position點到的時候回跳動 //rect.position = mouseWorldPosition; } } }
UI精准拖拽移動
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /// <summary> /// 掛在要拖拽的ui上即可 /// </summary> public class TuoDongImage : MonoBehaviour,IBeginDragHandler ,IDragHandler,IEndDragHandler { public bool isPrecision=true; //精准拖拽為true ,鼠標一直在UI中心可以為false //存儲圖片中心點與鼠標點擊點的偏移量 private Vector3 offect; //存儲當前拖拽圖片的RectTransform組件 private RectTransform m_rt; void Start() { m_rt = this.transform.GetComponent<RectTransform>(); } public void OnBeginDrag(PointerEventData eventData) { //如果是精確拖拽則進行計算偏移量操作 if (isPrecision) { // 存儲點擊時的鼠標坐標 Vector3 tWorldPos; //UI屏幕坐標轉換為世界坐標 RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out tWorldPos); //計算偏移量 offect = transform.position - tWorldPos; } //否則,默認偏移量為0 else { offect = Vector3.zero; } //m_rt.position = Input.mousePosition + offect; SetDraggedPosition(eventData); } //拖拽過程中觸發 public void OnDrag(PointerEventData eventData) { //m_rt.position = Input.mousePosition + offect; SetDraggedPosition(eventData); } //結束拖拽觸發 public void OnEndDrag(PointerEventData eventData) { //m_rt.position = Input.mousePosition + offect; SetDraggedPosition(eventData); } private void SetDraggedPosition(PointerEventData eventData) { //存儲當前鼠標所在位置 Vector3 globalMousePos; //UI屏幕坐標轉換為世界坐標 if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_rt, eventData.position, eventData.pressEventCamera, out globalMousePos)) { //設置位置及偏移量 m_rt.position = globalMousePos + offect; } } }
第二種 克隆拖拽 把需要克隆的圖片 錨點設置一下 (如下圖)
在把腳本掛在需要克隆的圖片上
/****************************************** * 項目名稱:UGUI通用 * 腳本功能:UI圖片拖拽功能(將腳本掛載在需要拖放的圖片上) *******************************************/ using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Clonephoto : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { private GameObject objj; private RectTransform m_rt;//canvas下面的一個ui private Vector3 offect; private RectTransform rect; void Start() { //查找這個ui(前提是必須得有這個UI) m_rt = GameObject.Find("Canvas").GetComponent<RectTransform>(); rect = this.transform.GetComponent<RectTransform>(); } //開始拖拽 public void OnBeginDrag(PointerEventData eventData) { //ui屏幕坐標轉換為世界坐標 RectTransformUtility.ScreenPointToWorldPointInRectangle(rect, eventData.position, eventData. pressEventCamera, out offect); //克隆自己在m_rt上面顯示 objj = Instantiate(transform.gameObject,m_rt); //objj.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(80, 80); //設置ui大小 //objj.transform.localScale = new Vector3(0.7f, 0.7f, 0.7f); //設置一下拖的時候大小,顯得好看一點點,可要可不要 objj.transform.GetComponent<RectTransform>().anchoredPosition = Input.mousePosition -offect; //計算偏移量 } //拖拽中 public void OnDrag(PointerEventData eventData) { if (objj !=null ) { objj.transform.GetComponent <RectTransform>().anchoredPosition = Input.mousePosition-offect; } } //結束拖拽 public void OnEndDrag(PointerEventData eventData) { if (objj!=null) { Accuracy_testing.name = objj.transform.name; objj.SetActive(false); Invoke("ShanChu", 0.1f); } } void ShanChu() { Destroy(objj.gameObject); } }