unity简单数字拼图小游戏(源码)

代码:

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>();
        // 获取父组件的 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>();
        // 获取父组件的 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");
    }
}

相关推荐

  1. Python游戏(1)附带

    2024-06-17 05:22:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 05:22:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 05:22:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 05:22:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 05:22:01       18 阅读

热门阅读

  1. Linux更改默认python版本

    2024-06-17 05:22:01       5 阅读
  2. 力扣上的经典问题:接雨水

    2024-06-17 05:22:01       6 阅读
  3. C++文本文件的读与写

    2024-06-17 05:22:01       6 阅读
  4. C++回溯算法

    2024-06-17 05:22:01       6 阅读
  5. 杂谈-Android和Ios的对比

    2024-06-17 05:22:01       10 阅读
  6. MySQL 保姆级教程(六):用通配符进行过滤

    2024-06-17 05:22:01       7 阅读
  7. 人工智能和印度大选

    2024-06-17 05:22:01       6 阅读
  8. 烧结银赋“芯”生,引领半导体革命

    2024-06-17 05:22:01       8 阅读
  9. ffmpeg转换视频格式

    2024-06-17 05:22:01       5 阅读