

代码:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.SceneManagement;
-
- public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
- {
- public float stepDistance = 10f; // 设置单步间隔长度
-
- private bool isDragging;// 是否正在拖动
- private RectTransform rectTransform;// 当前组件的 RectTransform
- private RectTransform parentRectTransform;// 父组件的 RectTransform
- private Vector2 accumulatedDelta; // 累积的偏移量
- private Vector2 lastPosition;// 上次记录的位置
-
-
- private void Awake()
- {
- // 获取当前组件的 RectTransform
- rectTransform = GetComponent
(); - // 获取父组件的 RectTransform
- parentRectTransform = transform.parent as RectTransform;
- }
-
-
-
- void Update()
- {
- // 如果正在拖动
- if (isDragging)
- {
- // 获取鼠标滚轮输入
- float scroll = Input.GetAxis("Mouse ScrollWheel");
-
- if (scroll != 0)
- {
- // 根据滚轮方向计算旋转角度
- float rotation = Mathf.Sign(scroll) * 90f;
- // 对父组件执行旋转操作
- parentRectTransform.Rotate(Vector3.forward, rotation);
- }
- // 如果点击了鼠标右键
- if (Input.GetMouseButtonDown(1))
- {
- // 对父组件执行翻转操作
- parentRectTransform.localScale = new Vector3(-1f * parentRectTransform.localScale.x, 1f,1f);
- }
- }
- }
- //事件回调
- public void OnBeginDrag(PointerEventData eventData)
- {
- // 标记开始拖动
- isDragging = true;
- // 将父组件置于同级别组件的最前显示
- parentRectTransform.SetAsLastSibling();
- // 重置累积的偏移量
- accumulatedDelta = Vector2.zero;
- // 记录开始拖动的初始位置
- //lastPosition = eventData.position;
-
- }
-
- public void OnDrag(PointerEventData eventData)
- {
- // 根据拖动的偏移量移动父组件的位置
- parentRectTransform.anchoredPosition += eventData.delta;
-
-
- /* Vector2 delta = eventData.position - lastPosition;
- accumulatedDelta += delta;
- //print($"eventData.position:{eventData.position},delta:{delta},accumulatedDelta:{accumulatedDelta}");
- if (Mathf.Abs(accumulatedDelta.x) >= stepDistance)
- {
- float sign = Mathf.Sign(accumulatedDelta.x);//(new Vector2(1, 0) * accumulatedDelta.x).normalized.x;//获取方向,并且需要让取值在1或者-1这两个数
- float moveValue = accumulatedDelta.x - (Mathf.Abs(accumulatedDelta.x) % stepDistance) * sign;
- print("moveValueX:" + moveValue);
- parentRectTransform.anchoredPosition += new Vector2(moveValue, 0);
- accumulatedDelta = new Vector2(accumulatedDelta.x - moveValue, accumulatedDelta.y);
- lastPosition = new Vector2(lastPosition.x + moveValue, lastPosition.y);
- }
- if (Mathf.Abs(accumulatedDelta.y) >= stepDistance)
- {
- float sign = Mathf.Sign(accumulatedDelta.y);
- float moveValue = accumulatedDelta.y - (Mathf.Abs(accumulatedDelta.y) % stepDistance) * sign;
- parentRectTransform.anchoredPosition += new Vector2(0, moveValue);
- accumulatedDelta = new Vector2(accumulatedDelta.x, accumulatedDelta.y - moveValue);
- lastPosition = new Vector2(lastPosition.x, lastPosition.y + moveValue);
- print("moveValueY:" + moveValue);
- }*/
-
- }
-
- public void OnEndDrag(PointerEventData eventData)
- {
- isDragging = false;// 标记结束拖动
-
- //manager.OnEndDrag(this);
- }
-
- public void Close(){
- Application.Quit();// 关闭应用程序
- SceneManager.LoadScene("Suntail Village");
- }
- }
-
优化:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.SceneManagement;
-
- public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
- {
- public float stepDistance = 10f; // 设置单步间隔长度
-
- private bool isDragging;// 是否正在拖动
- private RectTransform rectTransform;// 当前组件的 RectTransform
- private RectTransform parentRectTransform;// 父组件的 RectTransform
- private Vector2 accumulatedDelta; // 累积的偏移量
- private Vector2 lastPosition;// 上次记录的位置
-
-
- private void Awake()
- {
- // 获取当前组件的 RectTransform
- rectTransform = GetComponent
(); - // 获取父组件的 RectTransform
- parentRectTransform = transform.parent as RectTransform;
- }
-
-
-
- void Update()
- {
- // 如果正在拖动
- if (isDragging)
- {
- // 获取鼠标滚轮输入
- float scroll = Input.GetAxis("Mouse ScrollWheel");
-
- if (scroll != 0)
- {
- // 根据滚轮方向计算旋转角度
- float rotation = Mathf.Sign(scroll) * 90f;
- // 对父组件执行旋转操作
- parentRectTransform.Rotate(Vector3.forward, rotation);
- }
- // 如果点击了鼠标右键
- if (Input.GetMouseButtonDown(1))
- {
- // 对父组件执行翻转操作
- parentRectTransform.localScale = new Vector3(-1f * parentRectTransform.localScale.x, 1f,1f);
- }
- }
- }
- //事件回调
- public void OnBeginDrag(PointerEventData eventData)
- {
- // 标记开始拖动
- isDragging = true;
- // 将父组件置于同级别组件的最前显示
- parentRectTransform.SetAsLastSibling();
- // 重置累积的偏移量
- accumulatedDelta = Vector2.zero;
- // 记录开始拖动的初始位置
- //lastPosition = eventData.position;
-
- }
-
- public void OnDrag(PointerEventData eventData)
- {
- // 根据拖动的偏移量移动父组件的位置
- // parentRectTransform.anchoredPosition += eventData.delta;
-
-
- Vector2 delta = eventData.position - lastPosition;
- accumulatedDelta += delta;
- //print($"eventData.position:{eventData.position},delta:{delta},accumulatedDelta:{accumulatedDelta}");
-
- if (Mathf.Abs(accumulatedDelta.x) >= stepDistance)
- {
- float sign = Mathf.Sign(accumulatedDelta.x);//(new Vector2(1, 0) * accumulatedDelta.x).normalized.x;//获取方向,并且需要让取值在1或者-1这两个数
- float moveValue = accumulatedDelta.x - (Mathf.Abs(accumulatedDelta.x) % stepDistance) * sign;
- print("moveValueX:" + moveValue);
- parentRectTransform.anchoredPosition += new Vector2(moveValue, 0);
- accumulatedDelta = new Vector2(accumulatedDelta.x - moveValue, accumulatedDelta.y);
-
- lastPosition = new Vector2(lastPosition.x + moveValue, lastPosition.y);
- }
- if (Mathf.Abs(accumulatedDelta.y) >= stepDistance)
- {
- float sign = Mathf.Sign(accumulatedDelta.y);
- float moveValue = accumulatedDelta.y - (Mathf.Abs(accumulatedDelta.y) % stepDistance) * sign;
- parentRectTransform.anchoredPosition += new Vector2(0, moveValue);
- accumulatedDelta = new Vector2(accumulatedDelta.x, accumulatedDelta.y - moveValue);
-
- lastPosition = new Vector2(lastPosition.x, lastPosition.y + moveValue);
- print("moveValueY:" + moveValue);
- }
-
- }
-
- public void OnEndDrag(PointerEventData eventData)
- {
- isDragging = false;// 标记结束拖动
-
- //manager.OnEndDrag(this);
- }
-
- public void Close(){
- Application.Quit();// 关闭应用程序
- SceneManager.LoadScene("Suntail Village");
- }
- }
-