UnityUI在屏幕范圍內(nèi)拖拽

一、單個Image拖拽

using UnityEngine;

using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler

{

? ? private RectTransform rectTransform;

? ? private RectTransform canvasRectTransform;

? ? private Vector2 canvasSize;

? ? void Start()

? ? {

? ? ? ? rectTransform = GetComponent<RectTransform>();

? ? ? ? canvasRectTransform = rectTransform.parent.GetComponent<RectTransform>();

? ? ? ? canvasSize = canvasRectTransform.rect.size;

? ? }

? ? public void OnBeginDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以記錄開始拖拽時的位置,如果需要的話

? ? }

? ? public void OnDrag(PointerEventData eventData)

? ? {

? ? ? ? // 計算新的位置

? ? ? ? Vector2 newPosition = rectTransform.anchoredPosition + eventData.delta;

? ? ? ? // 獲取Image的尺寸

? ? ? ? Vector2 imageSize = rectTransform.rect.size;

? ? ? ? // 限制拖拽在Canvas范圍內(nèi)

? ? ? ? // 限制x軸

? ? ? ? newPosition.x = Mathf.Clamp(newPosition.x, -canvasSize.x / 2 + imageSize.x / 2, canvasSize.x / 2 - imageSize.x / 2);

? ? ? ? // 限制y軸

? ? ? ? newPosition.y = Mathf.Clamp(newPosition.y, -canvasSize.y / 2 + imageSize.y / 2, canvasSize.y / 2 - imageSize.y / 2);

? ? ? ? // 設(shè)置新的位置

? ? ? ? rectTransform.anchoredPosition = newPosition;

? ? }

? ? public void OnEndDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以在這里處理拖拽結(jié)束的邏輯

? ? }

}

單個Image拖拽

二、拖拽子Image

using UnityEngine;

using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler

{

? ? public RectTransform smallImageRect; // 小Image的RectTransform

? ? private RectTransform largeImageRect; // 大Image的RectTransform,即腳本所在的RectTransform

? ? private RectTransform canvasRect; // Canvas的RectTransform

? ? void Start()

? ? {

? ? ? ? largeImageRect = GetComponent<RectTransform>();

? ? ? ? canvasRect = largeImageRect.parent as RectTransform; // 假設(shè)大Image直接在Canvas下

? ? }

? ? public void OnBeginDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以在這里添加開始拖拽的邏輯

? ? }

? ? public void OnDrag(PointerEventData eventData)

? ? {

? ? ? ? // 計算小Image相對于大Image的拖拽增量

? ? ? ? Vector2 delta = eventData.delta / canvasRect.localScale.x; // 考慮Canvas的縮放

? ? ? ? // 更新大Image的位置,使得小Image跟隨拖拽

? ? ? ? Vector2 newPosition = largeImageRect.anchoredPosition + delta;

? ? ? ? largeImageRect.anchoredPosition = ClampPositionToBounds(newPosition);

? ? }

? ? public void OnEndDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以在這里添加結(jié)束拖拽的邏輯

? ? }

? ? private Vector2 ClampPositionToBounds(Vector2 newPosition)

? ? {

? ? ? ? // 計算大Image的邊界

? ? ? ? Vector2 minPosition = new Vector2(

? ? ? ? ? ? -canvasRect.rect.width / 2 + largeImageRect.rect.width / 2,

? ? ? ? ? ? -canvasRect.rect.height / 2 + largeImageRect.rect.height / 2

? ? ? ? );

? ? ? ? Vector2 maxPosition = new Vector2(

? ? ? ? ? ? canvasRect.rect.width / 2 - largeImageRect.rect.width / 2,

? ? ? ? ? ? canvasRect.rect.height / 2 - largeImageRect.rect.height / 2

? ? ? ? );

? ? ? ? // 限制大Image的位置,確保它不會超出Canvas的范圍

? ? ? ? newPosition.x = Mathf.Clamp(newPosition.x, minPosition.x, maxPosition.x);

? ? ? ? newPosition.y = Mathf.Clamp(newPosition.y, minPosition.y, maxPosition.y);

? ? ? ? return newPosition;

? ? }

}

拖拽子Image

三、拖拽吸附邊緣

using UnityEngine;

using UnityEngine.EventSystems;

public class Draggable : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler

{

? ? public RectTransform smallImageRect; // 小Image的RectTransform

? ? private RectTransform largeImageRect; // 大Image的RectTransform,即腳本所在的RectTransform

? ? private RectTransform canvasRect; // Canvas的RectTransform

? ? void Start()

? ? {

? ? ? ? largeImageRect = GetComponent<RectTransform>();

? ? ? ? canvasRect = largeImageRect.parent as RectTransform; // 假設(shè)大Image直接在Canvas下

? ? }

? ? public void OnBeginDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以在這里添加開始拖拽的邏輯

? ? }

? ? public void OnDrag(PointerEventData eventData)

? ? {

? ? ? ? // 計算小Image相對于大Image的拖拽增量

? ? ? ? Vector2 delta = eventData.delta / canvasRect.localScale.x; // 考慮Canvas的縮放

? ? ? ? // 更新大Image的位置,使得小Image跟隨拖拽

? ? ? ? Vector2 newPosition = largeImageRect.anchoredPosition + delta;

? ? ? ? largeImageRect.anchoredPosition = ClampPositionToBounds(newPosition);

? ? }

? ? public void OnEndDrag(PointerEventData eventData)

? ? {

? ? ? ? // 可以在這里添加結(jié)束拖拽的邏輯

? ? ? ? if (largeImageRect.anchoredPosition.x < 0)

? ? ? ? {

? ? ? ? ? ? // 吸附到左側(cè)邊緣

? ? ? ? ? ? largeImageRect.anchoredPosition = new Vector2(-canvasRect.rect.width / 2 + largeImageRect.rect.width / 2, largeImageRect.anchoredPosition.y);

? ? ? ? }

? ? ? ? else

? ? ? ? {

? ? ? ? ? ? // 吸附到右側(cè)邊緣

? ? ? ? ? ? largeImageRect.anchoredPosition = new Vector2(canvasRect.rect.width / 2 - largeImageRect.rect.width / 2, largeImageRect.anchoredPosition.y);

? ? ? ? }

}

? ? private Vector2 ClampPositionToBounds(Vector2 newPosition)

? ? {

? ? ? ? // 計算大Image的邊界

? ? ? ? Vector2 minPosition = new Vector2(

? ? ? ? ? ? -canvasRect.rect.width / 2 + largeImageRect.rect.width / 2,

? ? ? ? ? ? -canvasRect.rect.height / 2 + largeImageRect.rect.height / 2

? ? ? ? );

? ? ? ? Vector2 maxPosition = new Vector2(

? ? ? ? ? ? canvasRect.rect.width / 2 - largeImageRect.rect.width / 2,

? ? ? ? ? ? canvasRect.rect.height / 2 - largeImageRect.rect.height / 2

? ? ? ? );

? ? ? ? // 限制大Image的位置,確保它不會超出Canvas的范圍

? ? ? ? newPosition.x = Mathf.Clamp(newPosition.x, minPosition.x, maxPosition.x);

? ? ? ? newPosition.y = Mathf.Clamp(newPosition.y, minPosition.y, maxPosition.y);

? ? ? ? return newPosition;

? ? }

}

拖拽吸附邊緣
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Unity簡介 1. Unity界面 Shift + Space : 放大界面 Scene界面按鈕渲染模式2D...
    MYves閱讀 8,677評論 0 22
  • UGUI做一個類似Windows的框選功能: 效果: 主要用到的知識點,就是2D世界的碰撞檢測(BoxCollid...
    runinnn閱讀 712評論 2 3
  • 使用ugui做一個可以吸取屏幕上圖片的顏色的功能。 然后主要用到的知識點就是 這個方法了:Texture2D.Ge...
    runinnn閱讀 2,168評論 0 3
  • 在Unity中,UI系統(tǒng)是通過UGUI實現(xiàn)的。UGUI是一種基于Unity的用戶界面系統(tǒng),它可以用來創(chuàng)建和管理UI...
    游戲程序猿閱讀 601評論 0 1
  • RectTransformUtility 是關(guān)于RectTransform的工具類,有幾個靜態(tài)方法,這里記錄一下每...
    迷途小路閱讀 3,364評論 0 0

友情鏈接更多精彩內(nèi)容