代码:
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;// 当前组件的 RectTransformprivate RectTransform parentRectTransform;// 父组件的 RectTransformprivate Vector2 accumulatedDelta; // 累积的偏移量private Vector2 lastPosition;// 上次记录的位置private void Awake(){// 获取当前组件的 RectTransformrectTransform = GetComponent<RectTransform>();// 获取父组件的 RectTransformparentRectTransform = 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;// 当前组件的 RectTransformprivate RectTransform parentRectTransform;// 父组件的 RectTransformprivate Vector2 accumulatedDelta; // 累积的偏移量private Vector2 lastPosition;// 上次记录的位置private void Awake(){// 获取当前组件的 RectTransformrectTransform = GetComponent<RectTransform>();// 获取父组件的 RectTransformparentRectTransform = 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");}
}