using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TestDrag : MonoBehaviour ,IDragHandler{
public Image map;
float map_width;
float map_heigh;
float stansrdPosX;
float stansrdPosY;
void Start()
{
// 被拖动物体的宽和高
map_width = gameObject.GetComponent<RectTransform>().sizeDelta.x;
map_heigh = gameObject.GetComponent<RectTransform>().sizeDelta.y;
Debug.Log(map_width);
Debug.Log(map_heigh);
stansrdPosX = map_width / 2;
stansrdPosY = map_heigh / 2;
}
public void OnDrag(PointerEventData eventData)
{
// 鼠标位置
float posX = Input.mousePosition.x;
float posY = Input.mousePosition.y;
if (posX > stansrdPosX && posX < (1366- stansrdPosX) && posY > stansrdPosY && posY < (768- stansrdPosY))
{
gameObject.transform.position = Input.mousePosition;
}
if (posX <= stansrdPosX)
{
posX = stansrdPosX + 5;
}
if (posX >= (1366 - stansrdPosX))
{
posX = (1366 - stansrdPosX) - 5;
}
if (posY <= stansrdPosY)
{
posY = stansrdPosY + 5;
}
if (posY >= (768 - stansrdPosY))
{
posY = (768 - stansrdPosY) - 5;
}
gameObject.transform.position = new Vector2(posX, posY);
}
}