实现代码
public class Animals : MonoBehaviour, IDragHandler{
public GameObject _animal;
public void OnDrag(PointerEventData eventData)
{
// 设置移动物体的位置
_animal.transform.position = eventData.position;
}
}
IDragHandler 接口定义
public interface IDragHandler : IEventSystemHandler
{
/// <summary>
/// When dragging is occurring this will be called every time the cursor is moved.
/// </summary>
void OnDrag(PointerEventData eventData);
}
- 实现IDragHandler 接口,重新OnDrag 方法
- 获取eventData的position 赋值给移动物体