Unity 基础 之 IDragHanlder 简单实现 UGUI 元素随着鼠标移动,拖动的效果
?
?
Unity 基礎 之 IDragHanlder 多種方法簡單實現(xiàn)? UGUI 元素隨著鼠標移動,拖動的效果
?
目錄
Unity 基礎 之 IDragHanlder 多種方法簡單實現(xiàn)? UGUI 元素隨著鼠標移動,拖動的效果
一、簡單介紹
二、實現(xiàn)原理
三、注意實現(xiàn)
四、效果預覽
五、實現(xiàn)步驟
六、多種方法實現(xiàn)拖拽 UI
方法一:RectTransformUtility.ScreenPointToWorldPointInRectangle
方法二 :RectTransformUtility.ScreenPointToWorldPointInRectangle 并帶位移 offset
方法三:RectTransformUtility.ScreenPointToLocalPointInRectangle 并帶位移 Offset
方法四:Camera.WorldToScreenPoint 和? Camera.WorldToScreenPoint? 并帶位移 Offset
?
一、簡單介紹
Unity中的一些基礎知識點。
本節(jié)介紹,使用 IDraHandler ,簡單的就實現(xiàn) UGUI 元素,隨著鼠標的移動而移動的效果。
?
二、實現(xiàn)原理
1、IBeginDragHandler, IDragHandler, IEndDragHandler 三個接口,進行實現(xiàn)拖拽的功能
2、RectTransformUtility.ScreenPointToWorldPointInRectangle 或者? RectTransformUtility.ScreenPointToLocalPointInRectangle
進行坐標轉(zhuǎn)化,實現(xiàn)拖拽
3、必要的再結合 Camera.WorldToScreenPoint 和 Camera.ScreenToWorldPoint 一起實現(xiàn)拖拽移動效果
?
三、注意實現(xiàn)
1、根據(jù) Canvas 的 Render Mode 不同的拖拽方法,移動的效果可能會略有不同,擇需使用即可
?
四、效果預覽
?
五、實現(xiàn)步驟
1、打開 Unity,新建一個空工程
?
2、在場景中搭建UI,布局如下
?
3、新建腳本,編輯方法移動 UI,把腳本對應賦值
?
六、多種方法實現(xiàn)拖拽 UI
方法一:RectTransformUtility.ScreenPointToWorldPointInRectangle
1、代碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {private RectTransform rectTransform;// Start is called before the first frame updatevoid Start(){rectTransform = GetComponent<RectTransform>();}public void OnBeginDrag(PointerEventData eventData){Debug.Log("開始拖拽");}public void OnDrag(PointerEventData eventData){Vector3 pos;RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.enterEventCamera, out pos);rectTransform.position = pos;}public void OnEndDrag(PointerEventData eventData){Debug.Log("結束拖拽");}}2、效果如下
?
方法二 :RectTransformUtility.ScreenPointToWorldPointInRectangle 并帶位移 offset
1、代碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {private RectTransform rectTransform;private Vector3 pos; //控件初始位置private Vector3 mousePos; //鼠標初始位置private void Start(){rectTransform = GetComponent<RectTransform>();}public void OnBeginDrag(PointerEventData eventData){Debug.Log("開始拖拽"); pos = this.GetComponent<RectTransform>().position; RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out mousePos);}public void OnDrag(PointerEventData eventData){Vector3 newVec; RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out newVec);Vector3 offset = new Vector3(newVec.x - mousePos.x, newVec.y - mousePos.y, 0);rectTransform.position = pos + offset;}public void OnEndDrag(PointerEventData eventData){Debug.Log("結束拖拽");}}?
2、效果如下
?
方法三:RectTransformUtility.ScreenPointToLocalPointInRectangle 并帶位移 Offset
1、代碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {private Vector3 pos; //控件初始位置private Vector2 mousePos; //鼠標初始位置private RectTransform canvasRec; //控件所在畫布private void Start(){canvasRec = this.GetComponentInParent<Canvas>().transform as RectTransform;}public void OnBeginDrag(PointerEventData eventData){Debug.Log("開始拖拽");//控件所在畫布空間的初始位置pos = this.GetComponent<RectTransform>().anchoredPosition; //將屏幕空間鼠標位置eventData.position轉(zhuǎn)換為鼠標在畫布空間的鼠標位置RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRec, eventData.position, eventData.pressEventCamera, out mousePos);}public void OnDrag(PointerEventData eventData){Vector2 newVec; RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRec, eventData.position, eventData.pressEventCamera, out newVec);//鼠標移動在畫布空間的位置增量Vector3 offset = new Vector3(newVec.x - mousePos.x, newVec.y - mousePos.y, 0);//原始位置增加位置增量即為現(xiàn)在位置(this.transform as RectTransform).anchoredPosition = pos + offset;}public void OnEndDrag(PointerEventData eventData){Debug.Log("結束拖拽");} }?
2、效果如下
?
方法四:Camera.WorldToScreenPoint 和? Camera.WorldToScreenPoint? 并帶位移 Offset
1、代碼如下
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;public class DragHandlerUGUI : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {Vector3 uiScreenPosition;Vector3 mouseScreenPosition;Vector3 mScreenPosition;private Vector3 offset;Camera mCamera;// Start is called before the first frame updatevoid Start(){mCamera = Camera.main;}public void OnBeginDrag(PointerEventData eventData){Debug.Log("開始拖拽");//轉(zhuǎn)換對象到當前屏幕位置uiScreenPosition = mCamera.WorldToScreenPoint(transform.position);mouseScreenPosition = mCamera.WorldToScreenPoint(eventData.position);//鼠標屏幕坐標mScreenPosition = new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, uiScreenPosition.z);//獲得鼠標和對象之間的偏移量,拖拽時相機應該保持不動offset = transform.position - mCamera.ScreenToWorldPoint(mScreenPosition);}public void OnDrag(PointerEventData eventData){mouseScreenPosition = mCamera.WorldToScreenPoint(eventData.position);//鼠標屏幕上新位置mScreenPosition = new Vector3(mouseScreenPosition.x, mouseScreenPosition.y, uiScreenPosition.z);// 對象新坐標transform.position = offset + mCamera.ScreenToWorldPoint(mScreenPosition);}public void OnEndDrag(PointerEventData eventData){Debug.Log("結束拖拽");} }?
2、效果如下
?
總結
以上是生活随笔為你收集整理的Unity 基础 之 IDragHanlder 简单实现 UGUI 元素随着鼠标移动,拖动的效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity基础知识—Transform
- 下一篇: Unity 基础 之 Camera摄像机