把这个脚本挂载到需要操作的物体上即可
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObj : MonoBehaviour
{
private Vector3 lastMousePosition = Vector3.zero;
private bool isMouseDown = false;
void Update()
{
if (isMouseDown)
{
if (lastMousePosition != Vector3.zero)
{
Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;
this.transform.position += offset;
}
lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
}
public void OnMouseDown()
{
isMouseDown = true;
}
public void OnMouseUp()
{
isMouseDown = false;
lastMousePosition = Vector3.zero;
}
}
原博地址
https://blog.csdn.net/weixin_43673589