先来看实现最终的效果
先给camera赋给三个脚本Physics Raycaster,EventSystem,Standalone Input Module(通过AddComponent来添加)
然后新建脚本DragObject.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
//拖拽物体脚本
public class DragObject : MonoBehaviour,IDragHandler {
public void OnDrag(PointerEventData eventData)
{
Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
transform.position =Camera.main.ScreenToWorldPoint( new Vector3(eventData.position.x, eventData.position.y, screenPosition.z));
}
}
将该代码赋给cube上,就可以了