unity中拖拽生成物体,并移动物体到最终位置

实现该功能之前 ,准备好要拖动生成的物体 ,做成预制件,这样可以不断地生成。
首先,创建一个按钮,当点击按钮拖动后会生成对应的预制件,预制件的修改在最后一个函数中,大家自己替换名字就可以,然后这个脚本放在创建的按钮上就可以了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class DragSpan : MonoBehaviour, IPointerDownHandler
{
    //拖拽的物体
    private GameObject _objDragSpawning;

    //是否正在拖拽
    private bool _isDragSpawning = false;

    // Update is called once per frame
    void Update()
    {
        if (_isDragSpawning)
        {
            //刷新位置
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            _objDragSpawning.transform.position = ray.GetPoint(10);

            //结束拖拽
            if (Input.GetMouseButtonUp(0))
            {
                _isDragSpawning = false;
                _objDragSpawning = null;
            }
        }
    }
    //拖拽功能的实现,该函数可应用到任何脚本中
    IEnumerator OnMouseDown()
    {
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标
                                                                                 //将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
        var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        while (Input.GetMouseButton(0))
        {
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            yield return new WaitForFixedUpdate();
        }
    }

    //按下鼠标时开始生成实体预制件
    public void OnPointerDown(PointerEventData eventData)
    {
        GameObject prefab = Resources.Load<GameObject>("plane");
        if (prefab != null)
        {
            _objDragSpawning = Instantiate(prefab);
            _isDragSpawning = true;
        }
    }
}

接下来对生成的物体进行拖拽,到达最终的位置

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MouseMove : MonoBehaviour
{
    public IEnumerator OnMouseDown()
    {
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三维物体坐标转屏幕坐标
        //将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
        var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        while (Input.GetMouseButton(0))
        {
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            yield return new WaitForFixedUpdate();
        }
    }
}

最终效果是这样的
在这里插入图片描述

  • 7
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天里的草

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值