当前位置:   article > 正文

c#实现unity UI拖动自动吸附_unity 自动吸附

unity 自动吸附

c#实现unity UI拖动自动吸附


以下是使用C#实现Unity UI拖动自动吸附的示例代码:

using UnityEngine;
using UnityEngine.EventSystems;

public class SnapToGrid : MonoBehaviour, IBeginDragHandler, IDragHandler {
    public float gridSize = 10f;

    public void OnBeginDrag(PointerEventData eventData) {
        // Disable layout element to allow free movement
        var layoutElement = GetComponent<LayoutElement>();
        if (layoutElement != null) {
            layoutElement.ignoreLayout = true;
        }
    }

    public void OnDrag(PointerEventData eventData) {
        // Snap to grid during drag
        transform.position = new Vector3(
            Mathf.Round(eventData.position.x / gridSize) * gridSize,
            Mathf.Round(eventData.position.y / gridSize) * gridSize,
            transform.position.z
        );
    }

    public void OnEndDrag(PointerEventData eventData) {
        // Enable layout element to restore original position
        var layoutElement = GetComponent<LayoutElement>();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/704270
推荐阅读
相关标签
  

闽ICP备14008679号