You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIDragableObj : MonoBehaviour, IDragHandler, IPointerDownHandler, IPointerUpHandler
{
[AutoUI]
public Vector2 position;
protected Vector2 error_pos;
protected RectTransform rectransfrom;
protected void Awake()
{
rectransfrom = GetComponent<RectTransform>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
protected void Update () {
rectransfrom.anchoredPosition = position;
}
public void OnPointerDown(PointerEventData eventData)
{
print("OnPointerDown");
Vector2 n_position = eventData.position - new Vector2(Screen.width, Screen.height) / 2;
error_pos = n_position - position;
}
public void OnDrag(PointerEventData eventData)
{
Vector2 n_position = eventData.position;
position = n_position - new Vector2(Screen.width, Screen.height) / 2 - error_pos;
}
public void OnPointerUp(PointerEventData eventData)
{
error_pos = Vector2.zero;
}
}