mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/MI-38.git
synced 2026-01-24 05:15:37 +03:00
04.07.2022
This commit is contained in:
73
Heli_with_panels/Assets/Scripts/Panel2D/Drag.cs
Normal file
73
Heli_with_panels/Assets/Scripts/Panel2D/Drag.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user