1.新建脚本,并将脚本拖到你想要拖动的物体上即可
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class text : MonoBehaviour
{private Vector3 offset;public int x = 1;void OnMouseDown()//鼠标按下{offset = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);}void OnMouseDrag()//鼠标持续按下{Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) + offset;transform.position = new Vector3(newPosition.x, newPosition.y, transform.position.z);}
}