SHARE
TWEET

Untitled

a guest Apr 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4.  
  5.  
  6. public class Bar : MonoBehaviour, IPointerEnterHandler, IPointerDownHandler, IPointerExitHandler, IPointerUpHandler {
  7.  
  8.     private Transform m_transform;
  9.  
  10.     private SnapPoint m_eastSnapPos;
  11.     private SnapPoint m_westSnapPos;
  12.  
  13.     private PointerEventData m_pointerEvData;
  14.  
  15.     // ===========working vars============
  16.     Vector3 offset;
  17.     private Vector3 touchPos;
  18.  
  19.  
  20.     internal void Initialize(PointerEventData _eventData) {
  21.         SnapPoint[] snapPoints = GetComponentsInChildren<SnapPoint>();
  22.         foreach (SnapPoint point in snapPoints) {
  23.             if (point.snapDir == SnapPoint.SnapDir.E) {
  24.                 if (m_eastSnapPos == null) {
  25.                     m_eastSnapPos = point;
  26.                 }
  27.             } else if (point.snapDir == SnapPoint.SnapDir.W) {
  28.                 if (m_westSnapPos == null) {
  29.                     m_westSnapPos = point;
  30.                 }
  31.             }
  32.         }
  33.         m_transform = this.transform;
  34.  
  35.         Vector3 screenPos = Input.mousePosition;
  36.         screenPos.z = Grid.instance.transform.position.z - Camera.main.transform.position.z;
  37.         touchPos = Camera.main.ScreenToWorldPoint(screenPos);
  38.         m_transform.localPosition = touchPos;
  39.  
  40.         var pointer = new PointerEventData(EventSystem.current);
  41.         ExecuteEvents.Execute(this.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
  42.         ExecuteEvents.Execute(this.gameObject, pointer, ExecuteEvents.pointerDownHandler);
  43.     }
  44.  
  45.     public void OnPointerEnter(PointerEventData eventData) {
  46.         print("Bar::OnPointerEnter()");
  47.     }
  48.  
  49.     public void OnPointerDown(PointerEventData _eventData) {
  50.         print("Bar::OnPointerDown()");
  51.  
  52.         m_pointerEvData = _eventData;
  53.         offset = m_transform.localPosition - touchPos;
  54.  
  55.     }
  56.  
  57.     public void OnPointerExit(PointerEventData eventData) {
  58.         print("Bar::OnPointerExit()");
  59.     }
  60.  
  61.     public void OnPointerUp(PointerEventData eventData) {
  62.         print("Bar::OnPointerUp()");
  63.     }
  64.  
  65.  
  66.     private void Update() {
  67.  
  68.         if (m_pointerEvData != null) {
  69.  
  70.             if (Input.GetMouseButtonUp(0)) { // BETTER FIND A WAY TO USE: public void OnPointerUp(PointerEventData eventData) {m_pointerEvData = null;}
  71.                 print("Bar::Update::GetMouseButtonUp()");
  72.                 //var pointer = new PointerEventData(EventSystem.current);
  73.                 //ExecuteEvents.Execute(this.gameObject, pointer, ExecuteEvents.pointerEnterHandler);
  74.                 //ExecuteEvents.Execute(this.gameObject, pointer, ExecuteEvents.pointerDownHandler);
  75.                 m_pointerEvData = null;
  76.                 return;
  77.             }
  78.  
  79.             Vector3 screenPos = Input.mousePosition;
  80.             screenPos.z = Grid.instance.transform.position.z - Camera.main.transform.position.z;
  81.             Vector3 newTouchPos = Camera.main.ScreenToWorldPoint(screenPos);
  82.  
  83.             Vector3 newPos = Vector3.zero;
  84.             newPos.x = newTouchPos.x + offset.x;
  85.             newPos.y = newTouchPos.y + offset.y;
  86.  
  87.  
  88.             if (m_transform != null) { // if the object was Initialized at runtime - it can be moved
  89.                 m_transform.localPosition = newPos;
  90.             }
  91.         }
  92.     }
  93.  
  94.  
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top