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


    代码:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.SceneManagement;
    6. public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    7. {
    8. public float stepDistance = 10f; // 设置单步间隔长度
    9. private bool isDragging;// 是否正在拖动
    10. private RectTransform rectTransform;// 当前组件的 RectTransform
    11. private RectTransform parentRectTransform;// 父组件的 RectTransform
    12. private Vector2 accumulatedDelta; // 累积的偏移量
    13. private Vector2 lastPosition;// 上次记录的位置
    14. private void Awake()
    15. {
    16. // 获取当前组件的 RectTransform
    17. rectTransform = GetComponent();
    18. // 获取父组件的 RectTransform
    19. parentRectTransform = transform.parent as RectTransform;
    20. }
    21. void Update()
    22. {
    23. // 如果正在拖动
    24. if (isDragging)
    25. {
    26. // 获取鼠标滚轮输入
    27. float scroll = Input.GetAxis("Mouse ScrollWheel");
    28. if (scroll != 0)
    29. {
    30. // 根据滚轮方向计算旋转角度
    31. float rotation = Mathf.Sign(scroll) * 90f;
    32. // 对父组件执行旋转操作
    33. parentRectTransform.Rotate(Vector3.forward, rotation);
    34. }
    35. // 如果点击了鼠标右键
    36. if (Input.GetMouseButtonDown(1))
    37. {
    38. // 对父组件执行翻转操作
    39. parentRectTransform.localScale = new Vector3(-1f * parentRectTransform.localScale.x, 1f,1f);
    40. }
    41. }
    42. }
    43. //事件回调
    44. public void OnBeginDrag(PointerEventData eventData)
    45. {
    46. // 标记开始拖动
    47. isDragging = true;
    48. // 将父组件置于同级别组件的最前显示
    49. parentRectTransform.SetAsLastSibling();
    50. // 重置累积的偏移量
    51. accumulatedDelta = Vector2.zero;
    52. // 记录开始拖动的初始位置
    53. //lastPosition = eventData.position;
    54. }
    55. public void OnDrag(PointerEventData eventData)
    56. {
    57. // 根据拖动的偏移量移动父组件的位置
    58. parentRectTransform.anchoredPosition += eventData.delta;
    59. /* Vector2 delta = eventData.position - lastPosition;
    60. accumulatedDelta += delta;
    61. //print($"eventData.position:{eventData.position},delta:{delta},accumulatedDelta:{accumulatedDelta}");
    62. if (Mathf.Abs(accumulatedDelta.x) >= stepDistance)
    63. {
    64. float sign = Mathf.Sign(accumulatedDelta.x);//(new Vector2(1, 0) * accumulatedDelta.x).normalized.x;//获取方向,并且需要让取值在1或者-1这两个数
    65. float moveValue = accumulatedDelta.x - (Mathf.Abs(accumulatedDelta.x) % stepDistance) * sign;
    66. print("moveValueX:" + moveValue);
    67. parentRectTransform.anchoredPosition += new Vector2(moveValue, 0);
    68. accumulatedDelta = new Vector2(accumulatedDelta.x - moveValue, accumulatedDelta.y);
    69. lastPosition = new Vector2(lastPosition.x + moveValue, lastPosition.y);
    70. }
    71. if (Mathf.Abs(accumulatedDelta.y) >= stepDistance)
    72. {
    73. float sign = Mathf.Sign(accumulatedDelta.y);
    74. float moveValue = accumulatedDelta.y - (Mathf.Abs(accumulatedDelta.y) % stepDistance) * sign;
    75. parentRectTransform.anchoredPosition += new Vector2(0, moveValue);
    76. accumulatedDelta = new Vector2(accumulatedDelta.x, accumulatedDelta.y - moveValue);
    77. lastPosition = new Vector2(lastPosition.x, lastPosition.y + moveValue);
    78. print("moveValueY:" + moveValue);
    79. }*/
    80. }
    81. public void OnEndDrag(PointerEventData eventData)
    82. {
    83. isDragging = false;// 标记结束拖动
    84. //manager.OnEndDrag(this);
    85. }
    86. public void Close(){
    87. Application.Quit();// 关闭应用程序
    88. SceneManager.LoadScene("Suntail Village");
    89. }
    90. }

    优化:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.SceneManagement;
    6. public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    7. {
    8. public float stepDistance = 10f; // 设置单步间隔长度
    9. private bool isDragging;// 是否正在拖动
    10. private RectTransform rectTransform;// 当前组件的 RectTransform
    11. private RectTransform parentRectTransform;// 父组件的 RectTransform
    12. private Vector2 accumulatedDelta; // 累积的偏移量
    13. private Vector2 lastPosition;// 上次记录的位置
    14. private void Awake()
    15. {
    16. // 获取当前组件的 RectTransform
    17. rectTransform = GetComponent();
    18. // 获取父组件的 RectTransform
    19. parentRectTransform = transform.parent as RectTransform;
    20. }
    21. void Update()
    22. {
    23. // 如果正在拖动
    24. if (isDragging)
    25. {
    26. // 获取鼠标滚轮输入
    27. float scroll = Input.GetAxis("Mouse ScrollWheel");
    28. if (scroll != 0)
    29. {
    30. // 根据滚轮方向计算旋转角度
    31. float rotation = Mathf.Sign(scroll) * 90f;
    32. // 对父组件执行旋转操作
    33. parentRectTransform.Rotate(Vector3.forward, rotation);
    34. }
    35. // 如果点击了鼠标右键
    36. if (Input.GetMouseButtonDown(1))
    37. {
    38. // 对父组件执行翻转操作
    39. parentRectTransform.localScale = new Vector3(-1f * parentRectTransform.localScale.x, 1f,1f);
    40. }
    41. }
    42. }
    43. //事件回调
    44. public void OnBeginDrag(PointerEventData eventData)
    45. {
    46. // 标记开始拖动
    47. isDragging = true;
    48. // 将父组件置于同级别组件的最前显示
    49. parentRectTransform.SetAsLastSibling();
    50. // 重置累积的偏移量
    51. accumulatedDelta = Vector2.zero;
    52. // 记录开始拖动的初始位置
    53. //lastPosition = eventData.position;
    54. }
    55. public void OnDrag(PointerEventData eventData)
    56. {
    57. // 根据拖动的偏移量移动父组件的位置
    58. // parentRectTransform.anchoredPosition += eventData.delta;
    59. Vector2 delta = eventData.position - lastPosition;
    60. accumulatedDelta += delta;
    61. //print($"eventData.position:{eventData.position},delta:{delta},accumulatedDelta:{accumulatedDelta}");
    62. if (Mathf.Abs(accumulatedDelta.x) >= stepDistance)
    63. {
    64. float sign = Mathf.Sign(accumulatedDelta.x);//(new Vector2(1, 0) * accumulatedDelta.x).normalized.x;//获取方向,并且需要让取值在1或者-1这两个数
    65. float moveValue = accumulatedDelta.x - (Mathf.Abs(accumulatedDelta.x) % stepDistance) * sign;
    66. print("moveValueX:" + moveValue);
    67. parentRectTransform.anchoredPosition += new Vector2(moveValue, 0);
    68. accumulatedDelta = new Vector2(accumulatedDelta.x - moveValue, accumulatedDelta.y);
    69. lastPosition = new Vector2(lastPosition.x + moveValue, lastPosition.y);
    70. }
    71. if (Mathf.Abs(accumulatedDelta.y) >= stepDistance)
    72. {
    73. float sign = Mathf.Sign(accumulatedDelta.y);
    74. float moveValue = accumulatedDelta.y - (Mathf.Abs(accumulatedDelta.y) % stepDistance) * sign;
    75. parentRectTransform.anchoredPosition += new Vector2(0, moveValue);
    76. accumulatedDelta = new Vector2(accumulatedDelta.x, accumulatedDelta.y - moveValue);
    77. lastPosition = new Vector2(lastPosition.x, lastPosition.y + moveValue);
    78. print("moveValueY:" + moveValue);
    79. }
    80. }
    81. public void OnEndDrag(PointerEventData eventData)
    82. {
    83. isDragging = false;// 标记结束拖动
    84. //manager.OnEndDrag(this);
    85. }
    86. public void Close(){
    87. Application.Quit();// 关闭应用程序
    88. SceneManager.LoadScene("Suntail Village");
    89. }
    90. }

  • 相关阅读:
    广告学概论重点复习资料-完整版
    将 Spring Boot 应用程序部署为 WAR
    AOP结合注解实现项目中接口调用情况监控
    IT 冷知识:全球第一个“Bug”被发现
    Java中的常见的设计模式总结
    Java/JDK 21正式发布!15个特性一览
    怎么用蜂邮EDM和Outlook批量发送邮件带附件
    FPGA工程师职业发展道路
    figma插件都有哪些好用的,分享11个提效插件
    Essential Macleod中的吸收工具
  • 原文地址:https://blog.csdn.net/m0_74289471/article/details/139701236