Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4.  
  5. internal class ScrapbookStickerDragProxy : MonoBehaviour
  6. {
  7. [Serializable]
  8. private class Components
  9. {
  10. [SerializeField]
  11. internal RectTransform rectTransform;
  12.  
  13. internal ScrapbookSticker scrapbookSticker;
  14. }
  15. [SerializeField]
  16. private Components _components;
  17.  
  18. internal void Drag(ScrapbookSticker scrapbookSticker)
  19. {
  20. _components.scrapbookSticker = scrapbookSticker;
  21. base.gameObject.SetActive(true);
  22. _components.rectTransform.position = scrapbookSticker.RectTransform.position;
  23. }
  24.  
  25. private void Update()
  26. {
  27. UpdateDrag();
  28. UpdateContainment();
  29. }
  30.  
  31. private void UpdateDrag()
  32. {
  33. _components.rectTransform.position = Input.mousePosition;
  34. }
  35.  
  36. private void UpdateContainment()
  37. {
  38. bool contained = false;
  39. Touch[] touches = Input.touches;
  40. foreach (Touch touch in touches)
  41. {
  42. bool contains = Contains(touch.position);
  43. if (contains)
  44. {
  45. contains = true;
  46. break;
  47. }
  48. }
  49.  
  50. if(!contained)
  51. contained = Input.GetMouseButton(0) && Contains(Input.mousePosition);
  52.  
  53. if (!contained)
  54. Drop();
  55. }
  56.  
  57. private void Drop()
  58. {
  59. _components.scrapbookSticker.OnDrop();
  60. _components.scrapbookSticker = null;
  61. base.gameObject.SetActive(false);
  62. }
  63.  
  64. private bool Contains(Vector2 position)
  65. {
  66. return RectTransformUtility.RectangleContainsScreenPoint(_components.rectTransform, position);
  67. }
  68. }
  69.  
  70.  
  71. using System.Collections;
  72. using System.Collections.Generic;
  73. using UnityEngine;
  74. using UnityEngine.EventSystems;
  75. using System;
  76. using UnityEngine.UI;
  77.  
  78. internal class ScrapbookSticker : MonoBehaviour, IDragHandler
  79. {
  80. [Serializable]
  81. private class Components
  82. {
  83. [SerializeField]
  84. internal RectTransform rectTransform;
  85.  
  86. [SerializeField]
  87. internal ScrapbookStickerDragProxy scrapbookStickerDragProxy;
  88. }
  89. [SerializeField]
  90. private Components _components;
  91.  
  92. internal RectTransform RectTransform => _components.rectTransform;
  93.  
  94. [Serializable]
  95. private class Data
  96. {
  97. [SerializeField]
  98. internal bool dragging;
  99. [SerializeField]
  100. internal Vector3 startingPosition;
  101. }
  102. [SerializeField]
  103. private Data _data;
  104.  
  105. private void OnDisable()
  106. {
  107. _data.dragging = false;
  108. }
  109.  
  110. public void OnDrag(PointerEventData eventData)
  111. {
  112. if (!_data.dragging)
  113. _components.scrapbookStickerDragProxy.Drag(this);
  114. _data.dragging = true;
  115.  
  116. }
  117.  
  118. internal void OnDrop()
  119. {
  120. _data.dragging = false;
  121. }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement