mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/MI-38.git
synced 2026-01-23 23:55:38 +03:00
74 lines
2.3 KiB
C#
74 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Drag : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
protected Vector3 mousePosition;
|
|
protected Vector2 a; // Vector2 deltaPosition
|
|
public string typeDrag = null;
|
|
public float dragMin, dragMax;
|
|
public bool clamp=false;
|
|
|
|
[HideInInspector] public Vector2 dd;//для передачи в connection
|
|
[HideInInspector] public bool _delta = false;//для передачи в connection
|
|
|
|
private void Start()
|
|
{
|
|
dd = new Vector2(transform.localPosition.x, transform.localPosition.y);
|
|
}
|
|
public virtual void OnBeginDrag(PointerEventData data)
|
|
{
|
|
calculationBegin();
|
|
}
|
|
public virtual void OnDrag(PointerEventData data)
|
|
{
|
|
calculationDrag();
|
|
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData data)
|
|
{
|
|
dd = new Vector2(transform.localPosition.x, transform.localPosition.y);
|
|
if (typeDrag == "x" && (dd.x == dragMin || dd.x == dragMax))
|
|
{
|
|
_delta = true;
|
|
}
|
|
else if (typeDrag == "y" && (dd.y == dragMin || dd.y == dragMax))
|
|
{
|
|
_delta = true;
|
|
}
|
|
else
|
|
{
|
|
_delta = false;
|
|
}
|
|
}
|
|
public void calculationBegin()
|
|
{
|
|
a = new Vector2(Input.mousePosition.x - transform.localPosition.x, Input.mousePosition.y - transform.localPosition.y);
|
|
}
|
|
public void calculationDrag()
|
|
{
|
|
mousePosition = Input.mousePosition;
|
|
Vector2 q = new Vector2(mousePosition.x, mousePosition.y);
|
|
Vector3 b = new Vector3(q.x - a.x, q.y - a.y, transform.localPosition.z);
|
|
|
|
if (typeDrag == "x")
|
|
{
|
|
b.x = (clamp) ? Mathf.Clamp(b.x, dragMin, dragMax) : b.x;
|
|
transform.localPosition = new Vector3(b.x, transform.localPosition.y, transform.localPosition.z);
|
|
}
|
|
else if (typeDrag == "y")
|
|
{
|
|
b.y = (clamp) ? Mathf.Clamp(b.y, dragMin, dragMax) : b.y;
|
|
transform.localPosition = new Vector3(transform.localPosition.x, b.y, transform.localPosition.z);
|
|
}
|
|
else
|
|
{
|
|
transform.localPosition = b;
|
|
}
|
|
}
|
|
}
|