Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using UnityEngine.EventSystems;
- internal class ScrapbookStickerDragProxy : MonoBehaviour
- {
- [Serializable]
- private class Components
- {
- [SerializeField]
- internal RectTransform rectTransform;
- internal ScrapbookSticker scrapbookSticker;
- }
- [SerializeField]
- private Components _components;
- internal void Drag(ScrapbookSticker scrapbookSticker)
- {
- _components.scrapbookSticker = scrapbookSticker;
- base.gameObject.SetActive(true);
- _components.rectTransform.position = scrapbookSticker.RectTransform.position;
- }
- private void Update()
- {
- UpdateDrag();
- UpdateContainment();
- }
- private void UpdateDrag()
- {
- _components.rectTransform.position = Input.mousePosition;
- }
- private void UpdateContainment()
- {
- bool contained = false;
- Touch[] touches = Input.touches;
- foreach (Touch touch in touches)
- {
- bool contains = Contains(touch.position);
- if (contains)
- {
- contains = true;
- break;
- }
- }
- if(!contained)
- contained = Input.GetMouseButton(0) && Contains(Input.mousePosition);
- if (!contained)
- Drop();
- }
- private void Drop()
- {
- _components.scrapbookSticker.OnDrop();
- _components.scrapbookSticker = null;
- base.gameObject.SetActive(false);
- }
- private bool Contains(Vector2 position)
- {
- return RectTransformUtility.RectangleContainsScreenPoint(_components.rectTransform, position);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using System;
- using UnityEngine.UI;
- internal class ScrapbookSticker : MonoBehaviour, IDragHandler
- {
- [Serializable]
- private class Components
- {
- [SerializeField]
- internal RectTransform rectTransform;
- [SerializeField]
- internal ScrapbookStickerDragProxy scrapbookStickerDragProxy;
- }
- [SerializeField]
- private Components _components;
- internal RectTransform RectTransform => _components.rectTransform;
- [Serializable]
- private class Data
- {
- [SerializeField]
- internal bool dragging;
- [SerializeField]
- internal Vector3 startingPosition;
- }
- [SerializeField]
- private Data _data;
- private void OnDisable()
- {
- _data.dragging = false;
- }
- public void OnDrag(PointerEventData eventData)
- {
- if (!_data.dragging)
- _components.scrapbookStickerDragProxy.Drag(this);
- _data.dragging = true;
- }
- internal void OnDrop()
- {
- _data.dragging = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement