Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3.  
  4. public class UI_ElementMoving : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  5. {
  6. public bool moving;
  7. private Canvas parentCanvas;
  8.  
  9. private Vector2 clickedPivot;
  10. public bool isButton;
  11. private Vector2 startPos;
  12.  
  13. public void Start()
  14. {
  15. startPos = transform.localPosition;
  16. parentCanvas = transform.root.GetComponent<Canvas>();
  17. Vector2 pos;
  18.  
  19. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  20. parentCanvas.transform as RectTransform, Input.mousePosition,
  21. parentCanvas.worldCamera,
  22. out pos);
  23. }
  24.  
  25. public void Update()
  26. {
  27. if (!moving)
  28. {
  29. if (isButton)
  30. {
  31. transform.localPosition = startPos;
  32. }
  33. return;
  34. }
  35.  
  36. Vector2 movePos;
  37. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  38. parentCanvas.transform as RectTransform,
  39. Input.mousePosition, parentCanvas.worldCamera,
  40. out movePos);
  41.  
  42. transform.position = parentCanvas.transform.TransformPoint(movePos - (!isButton ? clickedPivot : Vector2.zero)) + (isButton ? Vector3.forward : Vector3.zero);
  43. }
  44.  
  45. public void OnPointerDown(PointerEventData eventData)
  46. {
  47. if (eventData.button == PointerEventData.InputButton.Left)
  48. {
  49. if(!GetComponent<InventoryButton>() || !GetComponent<InventoryButton>().isFast)
  50. moving = true;
  51. RectTransformUtility.ScreenPointToLocalPointInRectangle(
  52. parentCanvas.transform as RectTransform,
  53. Input.mousePosition, parentCanvas.worldCamera,
  54. out clickedPivot);
  55. clickedPivot -= new Vector2(transform.localPosition.x, transform.localPosition.y);
  56.  
  57. var invSys = FindObjectOfType<InventorySystem>();
  58. for (byte x = 0; x < invSys.inv.Count; x++)
  59. {
  60. transform.parent.SetSiblingIndex(invSys.inv[x].ui_image.transform == transform ? invSys.inv.Count : x);
  61. }
  62. }
  63. }
  64.  
  65. public void OnPointerUp(PointerEventData eventData)
  66. {
  67. if (eventData.button == PointerEventData.InputButton.Left)
  68. {
  69. moving = false;
  70. clickedPivot = Vector2.zero;
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement