Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.EventSystems;
- public class UI_ElementMoving : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
- {
- public bool moving;
- private Canvas parentCanvas;
- private Vector2 clickedPivot;
- public bool isButton;
- private Vector2 startPos;
- public void Start()
- {
- startPos = transform.localPosition;
- parentCanvas = transform.root.GetComponent<Canvas>();
- Vector2 pos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(
- parentCanvas.transform as RectTransform, Input.mousePosition,
- parentCanvas.worldCamera,
- out pos);
- }
- public void Update()
- {
- if (!moving)
- {
- if (isButton)
- {
- transform.localPosition = startPos;
- }
- return;
- }
- Vector2 movePos;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(
- parentCanvas.transform as RectTransform,
- Input.mousePosition, parentCanvas.worldCamera,
- out movePos);
- transform.position = parentCanvas.transform.TransformPoint(movePos - (!isButton ? clickedPivot : Vector2.zero)) + (isButton ? Vector3.forward : Vector3.zero);
- }
- public void OnPointerDown(PointerEventData eventData)
- {
- if (eventData.button == PointerEventData.InputButton.Left)
- {
- if(!GetComponent<InventoryButton>() || !GetComponent<InventoryButton>().isFast)
- moving = true;
- RectTransformUtility.ScreenPointToLocalPointInRectangle(
- parentCanvas.transform as RectTransform,
- Input.mousePosition, parentCanvas.worldCamera,
- out clickedPivot);
- clickedPivot -= new Vector2(transform.localPosition.x, transform.localPosition.y);
- var invSys = FindObjectOfType<InventorySystem>();
- for (byte x = 0; x < invSys.inv.Count; x++)
- {
- transform.parent.SetSiblingIndex(invSys.inv[x].ui_image.transform == transform ? invSys.inv.Count : x);
- }
- }
- }
- public void OnPointerUp(PointerEventData eventData)
- {
- if (eventData.button == PointerEventData.InputButton.Left)
- {
- moving = false;
- clickedPivot = Vector2.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement