Unity实现2D物体拖拽效果

Unity实现2D物体拖拽效果

方法1:简单粗暴,给物体上挂上一个拖拽脚本,实现OnMouseDrag()方法,上图在这里插入图片描述
方法2:给物体上挂上一个拖拽脚本,实现接口IDragHandler,上图经过新的测试,好像只能用于UI对象实现接口
实现接口的方法
方法3:直接上代码

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isMouseDown = true;
        }
        if (Input.GetMouseButtonUp(0))
        {
            isMouseDown = false;
            lastMousePosition = Vector3.zero;
            isClickObj = false;
        }
        if (isMouseDown)
        {
            RaycastHit2D raycastHit2D =
                Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 100);
            Debug.Log(raycastHit2D.collider);
            if (raycastHit2D.collider != null)
            {
                if (raycastHit2D.collider.transform == transform)
                {
                    isClickObj = true;
                }
            }
            if (isClickObj)
            {
                Move();
            }
        }
    }
    private void Move()
    {
        if (lastMousePosition != Vector3.zero)
        {
            Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
            this.transform.position += offset;
        }
        lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

方法4:使用FingerGestures插件来实现拖拽在这里插入图片描述
注意,场景中要拖入FingerGestures手势检测预制体,物体上挂DragRecognizer和ScreenRaycaster脚本

方法5:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Collider2D))]
public class DragObject : MonoBehaviour
{
    public Vector2 _DragScale = Vector2.one;
    [HideInInspector]
    public Vector3 _CurrentPos = Vector3.zero;
    protected Transform m_Transform;

    protected Plane m_Plane;
    protected bool m_Started = false;
    protected bool m_Pressed = false;
    protected Vector3 m_LastPos;
    protected int m_TouchID = 0;

    protected virtual void OnEnable()
    {
        m_Transform = transform;
    }

    protected virtual void OnDisable()
    {
        m_Started = false;
    }

    protected virtual void OnPress(bool isDown)
    {
        DebugTool.Log("OnPress:     " + isDown);
        if (isDown)
        {
            if (!m_Pressed)
            {
                m_TouchID = UICamera.currentTouchID;
                m_Pressed = true;
                m_Started = false;
                Transform trans = UICamera.currentCamera.transform;
                m_Plane = new Plane(trans.rotation * Vector3.back, UICamera.lastWorldPosition);
            }
        }
        else if (m_Pressed && m_TouchID == UICamera.currentTouchID)
        {
            m_Pressed = false;
        }
    }

    protected virtual void OnDrag(Vector2 delta)
    {
        if (m_Pressed && m_TouchID == UICamera.currentTouchID && enabled)
        {
            UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
            Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
            float dist = 0f;
            if (m_Plane.Raycast(ray, out dist))
            {
                _CurrentPos = ray.GetPoint(dist);
                Vector3 offset = _CurrentPos - m_LastPos;
                m_LastPos = _CurrentPos;
                if (!m_Started)
                {
                    m_Started = true;
                    offset = Vector3.zero;
                }
                if (offset.x != 0f || offset.y != 0f)
                {
                    offset = m_Transform.InverseTransformDirection(offset);
                    offset.Scale(_DragScale);
                    offset = m_Transform.TransformDirection(offset);
                }
                m_Transform.position += offset;
            }
        }
    }
}

该方法需要NGUI相机搭配使用,不推荐,下图是NGUI相机配置
uicamera参数
注意:这里的UICamera必须选择2D类型

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity实现2D物体拖拽到区域触发更换不同的物品,可以通过以下步骤实现: 1. 创建一个区域作为触发器,可以使用Unity的Collider2D组件,例如Box Collider 2D或Circle Collider 2D。 2. 创建多个2D物体,并将它们放置在场景中的不同位置。 3. 为每个2D物体添加一个Collider2D和一个Rigidbody2D组件,以使它们可以被拖拽。 4. 编写一个脚本,使得当拖拽2D物体进入触发器区域时,触发器区域中的物体发生更换。可以使用OnTriggerEnter2D()函数来检测2D物体进入触发器区域。 下面是一个简单的示例代码: ```c# using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangeObject : MonoBehaviour { public GameObject[] objectsToChange; // 存储要更换的物体 private void OnTriggerEnter2D(Collider2D collision) { // 检测是否为2D物体 if (collision.GetComponent<Rigidbody2D>() != null) { // 随机选择一个要更换的物体 int randomIndex = Random.Range(0, objectsToChange.Length); GameObject objectToChange = objectsToChange[randomIndex]; // 将当前2D物体替换为新的物体 Transform currentObjectTransform = collision.transform; Instantiate(objectToChange, currentObjectTransform.position, currentObjectTransform.rotation); Destroy(currentObjectTransform.gameObject); } } } ``` 将此脚本附加到触发器区域的GameObject上,并将要更换的物体添加到objectsToChange数组中。当2D物体进入触发器区域时,它将被替换为随机选择的新物体
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值